source: git/factory/readcf.yy @ fbb0173

spielwiese
Last change on this file since fbb0173 was 50612e2, checked in by Bradford Hovinen <hovinen@…>, 13 years ago
Rename readcf.y to readcf.yy so that it is built as a C++-file
  • Property mode set to 100644
File size: 4.4 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2/* $Id$ */
3
4%{
5
6#if defined(WINNT) && ! defined(__GNUC__)
7#include <malloc.h>
8#include <memory.h>
9#define alloca _alloca
10#endif
11
12#include <cstring> // we need this for gcc 4.3
13#include <config.h>
14
15#include <ctype.h>
16#ifdef HAVE_IOSTREAM
17#include <iostream>
18#define ISTREAM std::istream
19#define CERR std::cerr
20#elif defined(HAVE_IOSTREAM_H)
21#include <iostream.h>
22#define ISTREAM istream
23#define CERR cerr
24#endif
25
26
27#include "cf_assert.h"
28
29#include "canonicalform.h"
30#include "cf_defs.h"
31#include "gfops.h"
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
42static char* readString( ISTREAM& );
43
44#ifndef BISONPP
45void yyerror( char * s );
46int yylex();
47#endif
48
49static ISTREAM * defaultin = 0;
50
51static 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%%
66input:        /* empty string */
67        | input line
68;
69
70line:        ';'
71        | exp ';' { *retvalue = $1.getval(); return 0; }
72;
73
74exp:        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
88void YY_parse_CLASS::yyerror( char * s )
89#else
90void yyerror( char * s )
91#endif
92{
93    CERR << s << "\n";
94}
95
96#ifdef BISONPP
97int YY_parse_CLASS::yylex()
98#else
99int 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 ) ) {
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() ) {
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
151CanonicalForm readCF( ISTREAM& str )
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
181char* readString( ISTREAM& s )
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}
Note: See TracBrowser for help on using the repository browser.