1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
2 | /* $Id$ */ |
---|
3 | |
---|
4 | %{ |
---|
5 | |
---|
6 | #include "config.h" |
---|
7 | #include "factoryconf.h" |
---|
8 | |
---|
9 | #if defined(WINNT) && ! defined(__GNUC__) |
---|
10 | # include <malloc.h> |
---|
11 | # include <memory.h> |
---|
12 | # define alloca _alloca |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <cstring> // we need this for gcc 4.3 |
---|
16 | #include <config.h> |
---|
17 | |
---|
18 | #include <ctype.h> |
---|
19 | |
---|
20 | #ifdef HAVE_IOSTREAM |
---|
21 | # include <iostream> |
---|
22 | # define ISTREAM std::istream |
---|
23 | # define CERR std::cerr |
---|
24 | #elif defined(HAVE_IOSTREAM_H) |
---|
25 | # include <iostream.h> |
---|
26 | # define ISTREAM istream |
---|
27 | # define CERR cerr |
---|
28 | #endif |
---|
29 | |
---|
30 | |
---|
31 | #include "cf_assert.h" |
---|
32 | |
---|
33 | #include "canonicalform.h" |
---|
34 | #include "cf_defs.h" |
---|
35 | #include "gfops.h" |
---|
36 | #include "parseutil.h" |
---|
37 | #include "variable.h" |
---|
38 | |
---|
39 | #ifndef BISONPP |
---|
40 | # define YYSTYPE ParseUtil |
---|
41 | #else |
---|
42 | # define YY_parse_USE_GOTO 1 |
---|
43 | # define YY_parse_STYPE ParseUtil |
---|
44 | #endif |
---|
45 | |
---|
46 | static char* readString( ISTREAM& ); |
---|
47 | |
---|
48 | #ifndef BISONPP |
---|
49 | void yyerror( char * s ); |
---|
50 | int yylex(); |
---|
51 | #endif |
---|
52 | |
---|
53 | static ISTREAM * defaultin = 0; |
---|
54 | |
---|
55 | static CanonicalForm * retvalue = 0; |
---|
56 | |
---|
57 | %} |
---|
58 | |
---|
59 | /* BISON Declarations */ |
---|
60 | |
---|
61 | %token NUM |
---|
62 | %left '-' '+' |
---|
63 | %left '*' '/' |
---|
64 | %left NEG |
---|
65 | %right '^' |
---|
66 | |
---|
67 | /* Grammar follows */ |
---|
68 | |
---|
69 | %% |
---|
70 | input: /* empty string */ |
---|
71 | | input line |
---|
72 | ; |
---|
73 | |
---|
74 | line: ';' |
---|
75 | | exp ';' { *retvalue = $1.getval(); return 0; } |
---|
76 | ; |
---|
77 | |
---|
78 | exp: NUM { $$ = $1; } |
---|
79 | | exp '+' exp { $$ = $1.getval() + $3.getval(); } |
---|
80 | | exp '-' exp { $$ = $1.getval() - $3.getval(); } |
---|
81 | | exp '*' exp { $$ = $1.getval() * $3.getval(); } |
---|
82 | | exp '/' exp { $$ = $1.getval() / $3.getval(); } |
---|
83 | | '-' exp %prec NEG { $$ = -$2.getval(); } |
---|
84 | | '+' exp %prec NEG { $$ = $2.getval(); } |
---|
85 | | exp '^' NUM { $$ = power( $1.getval(), $3.getintval() ); } |
---|
86 | | '(' exp ')' { $$ = $2.getval(); } |
---|
87 | ; |
---|
88 | |
---|
89 | %% |
---|
90 | |
---|
91 | #ifdef BISONPP |
---|
92 | void YY_parse_CLASS::yyerror( char * s ) |
---|
93 | #else |
---|
94 | void yyerror( char * s ) |
---|
95 | #endif |
---|
96 | { |
---|
97 | CERR << s << "\n"; |
---|
98 | } |
---|
99 | |
---|
100 | #ifdef BISONPP |
---|
101 | int YY_parse_CLASS::yylex() |
---|
102 | #else |
---|
103 | int yylex() |
---|
104 | #endif |
---|
105 | { |
---|
106 | int c; |
---|
107 | |
---|
108 | while ((c = defaultin->get()) == ' ' || c == '\t' || c == '\n' ) ; |
---|
109 | if ( isdigit( c ) ) { |
---|
110 | defaultin->putback( c ); |
---|
111 | yylval = ParseUtil( readString( *defaultin ) ); |
---|
112 | return NUM; |
---|
113 | } |
---|
114 | else if ( isalpha( c ) ) { |
---|
115 | // look for generators of GF(q) |
---|
116 | if ( getCharacteristic() > 0 && getGFDegree() > 1 && c == gf_name ) { |
---|
117 | #ifdef BISONPP |
---|
118 | this->yylval = getGFGenerator(); |
---|
119 | #else |
---|
120 | yylval = getGFGenerator(); |
---|
121 | #endif |
---|
122 | } |
---|
123 | else if ( c == getDefaultVarName() ) { |
---|
124 | int cc; |
---|
125 | cc = defaultin->get(); |
---|
126 | if ( cc == '_' ) { |
---|
127 | ParseUtil index( readString( *defaultin ) ); |
---|
128 | #ifdef BISONPP |
---|
129 | this->yylval = Variable( index.getintval() ); |
---|
130 | #else |
---|
131 | yylval = Variable( index.getintval() ); |
---|
132 | #endif |
---|
133 | } |
---|
134 | else { |
---|
135 | defaultin->putback( cc ); |
---|
136 | #ifdef BISONPP |
---|
137 | this->yylval = Variable( (char)c ); |
---|
138 | #else |
---|
139 | yylval = Variable( (char)c ); |
---|
140 | #endif |
---|
141 | } |
---|
142 | } |
---|
143 | else { |
---|
144 | #ifdef BISONPP |
---|
145 | this->yylval = Variable( (char)c ); |
---|
146 | #else |
---|
147 | yylval = Variable( (char)c ); |
---|
148 | #endif |
---|
149 | } |
---|
150 | return NUM; |
---|
151 | } |
---|
152 | return c; |
---|
153 | } |
---|
154 | |
---|
155 | CanonicalForm readCF( ISTREAM& str ) |
---|
156 | { |
---|
157 | CanonicalForm theRetvalue; |
---|
158 | retvalue = new CanonicalForm(); |
---|
159 | #ifdef BISONPP |
---|
160 | YY_parse_CLASS my_parser; |
---|
161 | defaultin = &str; |
---|
162 | if ( my_parser.yyparse() == 0 ) { |
---|
163 | theRetvalue = *retvalue; |
---|
164 | delete retvalue; |
---|
165 | return theRetvalue; |
---|
166 | } |
---|
167 | else { |
---|
168 | delete retvalue; |
---|
169 | return 0; |
---|
170 | } |
---|
171 | #else |
---|
172 | defaultin = &str; |
---|
173 | if ( yyparse() == 0 ) { |
---|
174 | theRetvalue = *retvalue; |
---|
175 | delete retvalue; |
---|
176 | return theRetvalue; |
---|
177 | } |
---|
178 | else { |
---|
179 | delete retvalue; |
---|
180 | return 0; |
---|
181 | } |
---|
182 | #endif |
---|
183 | } |
---|
184 | |
---|
185 | char* readString( ISTREAM& s ) |
---|
186 | { |
---|
187 | static char * buffer = 0; |
---|
188 | static int bufsize = 0; |
---|
189 | |
---|
190 | if ( buffer == 0 ) { |
---|
191 | bufsize = 10000; |
---|
192 | buffer = new char[bufsize]; |
---|
193 | } |
---|
194 | int i = 0, c, goon = 1; |
---|
195 | while ( goon ) { |
---|
196 | while ( isdigit( c = s.get() ) && i < bufsize - 2 ) { |
---|
197 | buffer[i] = c; |
---|
198 | i++; |
---|
199 | } |
---|
200 | if ( isdigit( c ) ) { |
---|
201 | bufsize += 1000; |
---|
202 | char * newbuffer = (char*)memcpy( new char[bufsize], buffer, bufsize - 1000 ); |
---|
203 | delete [] buffer; |
---|
204 | buffer = newbuffer; |
---|
205 | buffer[i] = c; |
---|
206 | i++; |
---|
207 | } |
---|
208 | else { |
---|
209 | goon = 0; |
---|
210 | buffer[i] = '\0'; |
---|
211 | s.putback( c ); |
---|
212 | } |
---|
213 | } |
---|
214 | return buffer; |
---|
215 | } |
---|
216 | |
---|