source: git/factory/readcf.yy @ 2537fa0

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