source: git/factory/readcf.yy @ d187ac

spielwiese
Last change on this file since d187ac was 9f7665, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
Removed HAVE_CONFIG guards fix: fixed the inclusion of configure-generated *config.h's
  • Property mode set to 100644
File size: 4.5 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3%{
4
5
6#include "config.h"
7
8#include <factory/factoryconf.h>
9
10#if defined(WINNT) && ! defined(__GNUC__)
11# include <malloc.h>
12# include <memory.h>
13# define alloca _alloca
14#endif
15
16#include <cstring> // we need this for gcc 4.3
17
18#include <config.h>
19
20
21#include <ctype.h>
22
23#ifdef HAVE_IOSTREAM
24# include <iostream>
25# define ISTREAM std::istream
26# define CERR std::cerr
27#elif defined(HAVE_IOSTREAM_H)
28# include <iostream.h>
29# define ISTREAM istream
30# define CERR cerr
31#endif
32
33
34#include "cf_assert.h"
35
36#include "canonicalform.h"
37#include "cf_defs.h"
38#include "gfops.h"
39#include "parseutil.h"
40#include "variable.h"
41
42#ifndef BISONPP
43# define YYSTYPE ParseUtil
44#else
45# define YY_parse_USE_GOTO 1
46# define YY_parse_STYPE ParseUtil
47#endif
48
49static char* readString( ISTREAM& );
50
51#ifndef BISONPP
52void yyerror( char * s );
53int yylex();
54#endif
55
56static ISTREAM * defaultin = 0;
57
58static 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%%
73input:        /* empty string */
74        | input line
75;
76
77line:        ';'
78        | exp ';' { *retvalue = $1.getval(); return 0; }
79;
80
81exp:        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(); }
90;
91
92%%
93
94#ifdef BISONPP
95void YY_parse_CLASS::yyerror( char * s )
96#else
97void yyerror( char * s )
98#endif
99{
100    CERR << s << "\n";
101}
102
103#ifdef BISONPP
104int YY_parse_CLASS::yylex()
105#else
106int yylex()
107#endif
108{
109    int c;
110
111    while ((c = defaultin->get()) == ' ' || c == '\t' || c == '\n' ) ;
112    if ( isdigit( c ) ) {
113        defaultin->putback( c );
114        yylval = ParseUtil( readString( *defaultin ) );
115        return NUM;
116    }
117    else if ( isalpha( c ) ) {
118        // look for generators of GF(q)
119        if ( getCharacteristic() > 0 && getGFDegree() > 1 && c == gf_name ) {
120#ifdef BISONPP
121            this->yylval = getGFGenerator();
122#else
123            yylval = getGFGenerator();
124#endif
125        }
126        else if ( c == getDefaultVarName() ) {
127            int cc;
128            cc = defaultin->get();
129            if ( cc == '_' ) {
130                ParseUtil index( readString( *defaultin ) );
131#ifdef BISONPP
132                this->yylval = Variable( index.getintval() );
133#else
134                yylval = Variable( index.getintval() );
135#endif
136            }
137            else {
138                defaultin->putback( cc );
139#ifdef BISONPP
140                this->yylval = Variable( (char)c );
141#else
142                yylval = Variable( (char)c );
143#endif
144            }
145        }
146        else {
147#ifdef BISONPP
148            this->yylval = Variable( (char)c );
149#else
150            yylval = Variable( (char)c );
151#endif
152        }
153        return NUM;
154    }
155    return c;
156}
157
158CanonicalForm readCF( ISTREAM& str )
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 ) {
166        theRetvalue = *retvalue;
167        delete retvalue;
168        return theRetvalue;
169    }
170    else {
171        delete retvalue;
172        return 0;
173    }
174#else
175    defaultin = &str;
176    if ( yyparse() == 0 ) {
177        theRetvalue = *retvalue;
178        delete retvalue;
179        return theRetvalue;
180    }
181    else {
182        delete retvalue;
183        return 0;
184    }
185#endif
186}
187
188char* readString( ISTREAM& s )
189{
190    static char * buffer = 0;
191    static int bufsize = 0;
192
193    if ( buffer == 0 ) {
194        bufsize = 10000;
195        buffer = new char[bufsize];
196    }
197    int i = 0, c, goon = 1;
198    while ( goon ) {
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        }
216    }
217    return buffer;
218}
219
Note: See TracBrowser for help on using the repository browser.