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