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