source: git/factory/readcf.y @ 1e6de6

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