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