-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ypp
223 lines (183 loc) · 4.5 KB
/
parser.ypp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* $Id: parser.yy 48 2009-09-05 08:07:10Z tb $ -*- mode: c++ -*- */
/** \file parser.yy Contains the example Bison parser source */
%{ /*** C/C++ Declarations ***/
#include <stdio.h>
#include <string>
#include <vector>
#include "expression.hpp"
%}
/*** yacc/bison Declarations ***/
/* Require bison 2.3 or later */
%require "2.3"
/* add debug output code to generated parser. disable this for release
* versions. */
%debug
/* start symbol is named "start" */
%start start
/* write out a header file containing the token defines */
%defines
/* use newer C++ skeleton file */
%skeleton "lalr1.cc"
/* namespace to enclose parser in */
%name-prefix="example"
/* set the parser's class identifier */
%define "parser_class_name" "Parser"
/* keep track of the current position within the input */
%locations
%initial-action
{
// initialize the initial location object
@$.begin.filename = @$.end.filename = &driver.streamname;
};
/* The driver is passed by reference to the parser and to the scanner. This
* provides a simple but effective pure interface, not relying on global
* variables. */
%parse-param { class Driver& driver }
/* verbose error messages */
%error-verbose
/*** BEGIN EXAMPLE - Change the example grammar's tokens below ***/
%union {
int integerVal;
double doubleVal;
std::string* stringVal;
class CalcNode* calcnode;
}
%token END 0 "end of file"
%token EOL "end of line"
%token <integerVal> INTEGER "integer"
%token <doubleVal> DOUBLE "double"
%token <stringVal> STRING "string"
%type <calcnode> constant variable
%type <calcnode> atomexpr powexpr unaryexpr mulexpr addexpr expr
%destructor { delete $$; } STRING
%destructor { delete $$; } constant variable
%destructor { delete $$; } atomexpr powexpr unaryexpr mulexpr addexpr expr
/*** END EXAMPLE - Change the example grammar's tokens above ***/
%{
#include "driver.hpp"
#include "scanner.hpp"
/* this "connects" the bison parser in the driver to the flex scanner class
* object. it defines the yylex() function call to pull the next token from the
* current lexer object of the driver context. */
#undef yylex
#define yylex driver.lexer->lex
%}
%% /*** Grammar Rules ***/
/*** BEGIN EXAMPLE - Change the example grammar rules below ***/
constant : INTEGER
{
$$ = new CNConstant($1);
}
| DOUBLE
{
$$ = new CNConstant($1);
}
variable : STRING
{
if (!driver.calc.existsVariable(*$1)) {
error(yyloc, std::string("Unknown variable \"") + *$1 + "\"");
delete $1;
YYERROR;
}
else {
$$ = new CNConstant( driver.calc.getVariable(*$1) );
delete $1;
}
}
atomexpr : constant
{
$$ = $1;
}
| variable
{
$$ = $1;
}
| '(' expr ')'
{
$$ = $2;
}
powexpr : atomexpr
{
$$ = $1;
}
| atomexpr '^' powexpr
{
$$ = new CNPower($1, $3);
}
unaryexpr : powexpr
{
$$ = $1;
}
| '+' powexpr
{
$$ = $2;
}
| '-' powexpr
{
$$ = new CNNegate($2);
}
mulexpr : unaryexpr
{
$$ = $1;
}
| mulexpr '*' unaryexpr
{
$$ = new CNMultiply($1, $3);
}
| mulexpr '/' unaryexpr
{
$$ = new CNDivide($1, $3);
}
| mulexpr '%' unaryexpr
{
$$ = new CNModulo($1, $3);
}
addexpr : mulexpr
{
$$ = $1;
}
| addexpr '+' mulexpr
{
$$ = new CNAdd($1, $3);
}
| addexpr '-' mulexpr
{
$$ = new CNSubtract($1, $3);
}
expr : addexpr
{
$$ = $1;
}
assignment : STRING '=' expr
{
driver.calc.variables[*$1] = $3->evaluate();
std::cout << "Setting variable " << *$1
<< " = " << driver.calc.variables[*$1] << "\n";
delete $1;
delete $3;
}
start : /* empty */
| start ';'
| start EOL
| start assignment ';'
| start assignment EOL
| start assignment END
| start expr ';'
{
driver.calc.expressions.push_back($2);
}
| start expr EOL
{
driver.calc.expressions.push_back($2);
}
| start expr END
{
driver.calc.expressions.push_back($2);
}
/*** END EXAMPLE - Change the example grammar rules above ***/
%% /*** Additional Code ***/
void example::Parser::error(const Parser::location_type& l,
const std::string& m)
{
driver.error(l, m);
}