[ba1fde] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[ba1fde] | 3 | |
---|
| 4 | %{ |
---|
[311803] | 5 | |
---|
| 6 | #include "config.h" |
---|
[ee668e] | 7 | #include <factory/factoryconf.h> |
---|
[311803] | 8 | |
---|
[049c4f] | 9 | #if defined(WINNT) && ! defined(__GNUC__) |
---|
[311803] | 10 | # include <malloc.h> |
---|
| 11 | # include <memory.h> |
---|
| 12 | # define alloca _alloca |
---|
[9c9e2a4] | 13 | #endif |
---|
| 14 | |
---|
[16466a] | 15 | #include <cstring> // we need this for gcc 4.3 |
---|
[d8d9e9] | 16 | #include <config.h> |
---|
| 17 | |
---|
[ba1fde] | 18 | #include <ctype.h> |
---|
[311803] | 19 | |
---|
[1dc616] | 20 | #ifdef HAVE_IOSTREAM |
---|
[311803] | 21 | # include <iostream> |
---|
| 22 | # define ISTREAM std::istream |
---|
| 23 | # define CERR std::cerr |
---|
[1dc616] | 24 | #elif defined(HAVE_IOSTREAM_H) |
---|
[311803] | 25 | # include <iostream.h> |
---|
| 26 | # define ISTREAM istream |
---|
| 27 | # define CERR cerr |
---|
[1dc616] | 28 | #endif |
---|
| 29 | |
---|
[ba1fde] | 30 | |
---|
[650f2d8] | 31 | #include "cf_assert.h" |
---|
[d8d9e9] | 32 | |
---|
[a472b9] | 33 | #include "canonicalform.h" |
---|
[ba1fde] | 34 | #include "cf_defs.h" |
---|
[a1b6b0f] | 35 | #include "gfops.h" |
---|
[ba1fde] | 36 | #include "parseutil.h" |
---|
| 37 | #include "variable.h" |
---|
| 38 | |
---|
| 39 | #ifndef BISONPP |
---|
[311803] | 40 | # define YYSTYPE ParseUtil |
---|
[ba1fde] | 41 | #else |
---|
[311803] | 42 | # define YY_parse_USE_GOTO 1 |
---|
| 43 | # define YY_parse_STYPE ParseUtil |
---|
[ba1fde] | 44 | #endif |
---|
| 45 | |
---|
[181148] | 46 | static char* readString( ISTREAM& ); |
---|
[ba1fde] | 47 | |
---|
| 48 | #ifndef BISONPP |
---|
| 49 | void yyerror( char * s ); |
---|
| 50 | int yylex(); |
---|
| 51 | #endif |
---|
| 52 | |
---|
[181148] | 53 | static ISTREAM * defaultin = 0; |
---|
[ba1fde] | 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 | %% |
---|
[806c18] | 70 | input: /* empty string */ |
---|
| 71 | | input line |
---|
[ba1fde] | 72 | ; |
---|
| 73 | |
---|
[806c18] | 74 | line: ';' |
---|
| 75 | | exp ';' { *retvalue = $1.getval(); return 0; } |
---|
[ba1fde] | 76 | ; |
---|
| 77 | |
---|
[806c18] | 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(); } |
---|
[ba1fde] | 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 | { |
---|
[181148] | 97 | CERR << s << "\n"; |
---|
[ba1fde] | 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 ) ) { |
---|
[806c18] | 110 | defaultin->putback( c ); |
---|
| 111 | yylval = ParseUtil( readString( *defaultin ) ); |
---|
| 112 | return NUM; |
---|
[ba1fde] | 113 | } |
---|
| 114 | else if ( isalpha( c ) ) { |
---|
[806c18] | 115 | // look for generators of GF(q) |
---|
| 116 | if ( getCharacteristic() > 0 && getGFDegree() > 1 && c == gf_name ) { |
---|
[a1b6b0f] | 117 | #ifdef BISONPP |
---|
[806c18] | 118 | this->yylval = getGFGenerator(); |
---|
[a1b6b0f] | 119 | #else |
---|
[806c18] | 120 | yylval = getGFGenerator(); |
---|
[a1b6b0f] | 121 | #endif |
---|
[806c18] | 122 | } |
---|
| 123 | else if ( c == getDefaultVarName() ) { |
---|
| 124 | int cc; |
---|
| 125 | cc = defaultin->get(); |
---|
| 126 | if ( cc == '_' ) { |
---|
| 127 | ParseUtil index( readString( *defaultin ) ); |
---|
[ba1fde] | 128 | #ifdef BISONPP |
---|
[806c18] | 129 | this->yylval = Variable( index.getintval() ); |
---|
[ba1fde] | 130 | #else |
---|
[806c18] | 131 | yylval = Variable( index.getintval() ); |
---|
[ba1fde] | 132 | #endif |
---|
[806c18] | 133 | } |
---|
| 134 | else { |
---|
| 135 | defaultin->putback( cc ); |
---|
[ba1fde] | 136 | #ifdef BISONPP |
---|
[806c18] | 137 | this->yylval = Variable( (char)c ); |
---|
[ba1fde] | 138 | #else |
---|
[806c18] | 139 | yylval = Variable( (char)c ); |
---|
[ba1fde] | 140 | #endif |
---|
[806c18] | 141 | } |
---|
| 142 | } |
---|
| 143 | else { |
---|
[ba1fde] | 144 | #ifdef BISONPP |
---|
[806c18] | 145 | this->yylval = Variable( (char)c ); |
---|
[ba1fde] | 146 | #else |
---|
[806c18] | 147 | yylval = Variable( (char)c ); |
---|
[ba1fde] | 148 | #endif |
---|
[806c18] | 149 | } |
---|
| 150 | return NUM; |
---|
[ba1fde] | 151 | } |
---|
| 152 | return c; |
---|
| 153 | } |
---|
| 154 | |
---|
[181148] | 155 | CanonicalForm readCF( ISTREAM& str ) |
---|
[ba1fde] | 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 ) { |
---|
[806c18] | 163 | theRetvalue = *retvalue; |
---|
| 164 | delete retvalue; |
---|
| 165 | return theRetvalue; |
---|
[ba1fde] | 166 | } |
---|
| 167 | else { |
---|
[806c18] | 168 | delete retvalue; |
---|
| 169 | return 0; |
---|
[ba1fde] | 170 | } |
---|
| 171 | #else |
---|
| 172 | defaultin = &str; |
---|
| 173 | if ( yyparse() == 0 ) { |
---|
[806c18] | 174 | theRetvalue = *retvalue; |
---|
| 175 | delete retvalue; |
---|
| 176 | return theRetvalue; |
---|
[ba1fde] | 177 | } |
---|
| 178 | else { |
---|
[806c18] | 179 | delete retvalue; |
---|
| 180 | return 0; |
---|
[ba1fde] | 181 | } |
---|
| 182 | #endif |
---|
| 183 | } |
---|
| 184 | |
---|
[181148] | 185 | char* readString( ISTREAM& s ) |
---|
[ba1fde] | 186 | { |
---|
| 187 | static char * buffer = 0; |
---|
| 188 | static int bufsize = 0; |
---|
| 189 | |
---|
| 190 | if ( buffer == 0 ) { |
---|
[806c18] | 191 | bufsize = 10000; |
---|
| 192 | buffer = new char[bufsize]; |
---|
[ba1fde] | 193 | } |
---|
| 194 | int i = 0, c, goon = 1; |
---|
| 195 | while ( goon ) { |
---|
[806c18] | 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 | } |
---|
[ba1fde] | 213 | } |
---|
| 214 | return buffer; |
---|
| 215 | } |
---|
[e4fe2b] | 216 | |
---|