-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
executable file
·410 lines (357 loc) · 16.5 KB
/
parser.y
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* File: parser.y
* --------------
* Yacc input file to generate the parser for the compiler.
*
* pp2: your job is to write a parser that will construct the parse tree
* and if no parse errors were found, print it. The parser should
* accept the language as described in specification, and as augmented
* in the pp2 handout.
*/
%{
/* Just like lex, the text within this first region delimited by %{ and %}
* is assumed to be C/C++ code and will be copied verbatim to the y.tab.c
* file ahead of the definitions of the yyparse() function. Add other header
* file inclusions or C++ variable declarations/prototypes that are needed
* by your code here.
*/
#include "scanner.h" // for yylex
#include "parser.h"
#include "errors.h"
void yyerror(char *msg); // standard error-handling routine
%}
/* The section before the first %% is the Definitions section of the yacc
* input file. Here is where you declare tokens and types, add precedence
* and associativity options, and so on.
*/
/* yylval
* ------
* Here we define the type of the yylval global variable that is used by
* the scanner to store attibute information about the token just scanned
* and thus communicate that information to the parser.
*
* pp2: You will need to add new fields to this union as you add different
* attributes to your non-terminal symbols.
*/
%union {
int integerConstant;
bool boolConstant;
char* stringConstant;
double doubleConstant;
char identifier[MaxIdentLen+1]; // +1 for terminating null
Decl* decl;
FnDecl* fnDecl;
List< Decl* >* declList;
Type* varType;
VarDecl* varDecl;
InterfaceDecl* interfaceDecl;
List< Decl* >* prototypeList;
List< VarDecl* >* varDeclList;
StmtBlock* stmtBlock;
Stmt* stmt;
List< Stmt* >* stmtList;
Expr* expr;
List< Expr* >* exprList;
Call* call;
WhileStmt* whileStmt;
ForStmt* forStmt;
ReturnStmt* returnStmt;
IfStmt* ifStmt;
PrintStmt* printStmt;
ClassDecl* classDecl;
List< NamedType* >* interfaceList;
}
/* Tokens
* ------
* Here we tell yacc about all the token types that we are using.
* Yacc will assign unique numbers to these and export the #define
* in the generated y.tab.h header file.
*/
%token T_Void T_Bool T_Int T_Double T_String T_Class
%token T_LessEqual T_GreaterEqual T_Equal T_NotEqual T_Dims
%token T_And T_Or T_Null T_Extends T_This T_Interface T_Implements
%token T_While T_For T_If T_Else T_Return T_Break
%token T_New T_NewArray T_Print T_ReadInteger T_ReadLine
%token <identifier> T_Identifier
%token <stringConstant> T_StringConstant
%token <integerConstant> T_IntConstant
%token <doubleConstant> T_DoubleConstant
%token <boolConstant> T_BoolConstant
/* Non-terminal types
* ------------------
* In order for yacc to assign/access the correct field of $$, $1, we
* must to declare which field is appropriate for the non-terminal.
* As an example, this first type declaration establishes that the DeclList
* non-terminal uses the field named "declList" in the yylval union. This
* means that when we are setting $$ for a reduction for DeclList ore reading
* $n which corresponds to a DeclList nonterminal we are accessing the field
* of the union named "declList" which is of type List<Decl*>.
* pp2: You'll need to add many of these of your own.
*/
%type <declList> DeclList
%type <decl> Decl
%type <varType> Type
%type <varDecl> VariableDecl
%type <varDecl> Variable
%type <interfaceDecl> InterfaceDecl
%type <decl> Prototype
%type <prototypeList> PrototypeList
%type <varDecl> Param
%type <varDeclList> ParamsList
%type <varDeclList> AParam
%type <fnDecl> FunctionDecl
%type <stmtBlock> StmtBlock
%type <varDeclList> VariableDeclList
%type <stmtList> StmtList
%type <stmt> Stmt
%type <expr> Expr
%type <expr> Constant
%type <expr> LValue
%type <call> Call
%type <exprList> AExpr
%type <exprList> Actuals
%type <ifStmt> IfStmt
%type <whileStmt> WhileStmt
%type <forStmt> ForStmt
%type <returnStmt> ReturnStmt
%type <printStmt> PrintStmt
%type <exprList> PrintList
%type <classDecl> ClassDecl
%type <declList> FieldList
%type <interfaceList> InterfaceList
%type <decl> Field
%type <interfaceList> AInterface
%nonassoc '='
%left T_Or
%left T_And
%left T_Equal T_NotEqual
%nonassoc '<' T_LessEqual '>' T_GreaterEqual
%left '+' '-'
%left '*' '/' '%'
%right '!' UMINUS
%left '[' '.' /* eso creo, no estoy seguro */
%%
/* Rules
* -----
* All productions and actions should be placed between the start and stop
* %% markers which delimit the Rules section.
*/
Program : DeclList {
@1;
/* pp2: The @1 is needed to convince
* yacc to set up yylloc. You can remove
* it once you have other uses of @n*/
Program *program = new Program($1);
// if no errors, advance to next phase
if(ReportError::NumErrors() == 0)
program->Print(0);
}
;
//1 o mas
DeclList : DeclList Decl { ($$=$1)->Append($2); }
| Decl { ($$ = new List<Decl*>)->Append($1); }
;
Decl : VariableDecl { $$ = $1; }
| InterfaceDecl { $$ = $1; }
| ClassDecl { $$ = $1; }
| FunctionDecl { $$ = $1; }
;
VariableDecl : Variable ';' { $$ = $1; }
;
Variable : Type T_Identifier {
Identifier *varName = new Identifier(@2, $2);
$$ = new VarDecl( varName, $1 );
}
;
Type : T_Int { $$ = Type::intType; }
| T_Double { $$ = Type::doubleType; }
| T_Bool { $$ = Type::boolType; }
| T_String { $$ = Type::stringType; }
| T_Void { $$ = Type::voidType; }
| T_Identifier {
Identifier *udfType = new Identifier(@1, $1);
$$ = new NamedType(udfType);
}
| Type T_Dims { $$ = new ArrayType(@1, $1); }
;
InterfaceDecl : T_Interface T_Identifier '{' PrototypeList '}' {
Identifier* interfaceName = new Identifier(@2, $2);
$$ = new InterfaceDecl( interfaceName, $4 );
}
;
//0 o mas
PrototypeList : Prototype PrototypeList { ($$ = $2)->InsertAt($1, 0); }
| { $$ = new List< Decl* >(); }
;
Prototype : Type T_Identifier '(' ParamsList ')' ';' {
Identifier *funcName = new Identifier(@2, $2);
$$ = new FnDecl(funcName, $1, $4);
}
;
ParamsList : Param AParam { ($$ = $2)->InsertAt($1, 0); }
| { $$ = new List< VarDecl* >(); }
;
AParam : ',' Param AParam { ($$ = $3)->InsertAt($2, 0); }
| { $$ = new List< VarDecl* >(); }
;
Param : Variable { $$ = $1; }
;
FunctionDecl : Type T_Identifier '(' ParamsList ')' StmtBlock {
Identifier* functionName = new Identifier(@2, $2);
$$ = new FnDecl(functionName, $1, $4);
$$->SetFunctionBody($6);
}
;
ClassDecl : T_Class T_Identifier '{' FieldList '}'
{
$$ = new ClassDecl(new Identifier(@2, $2),
NULL,
new List< NamedType* >(),
$4);
}
| T_Class T_Identifier T_Extends T_Identifier '{' FieldList '}'
{
$$ = new ClassDecl(new Identifier(@2, $2),
new NamedType(new Identifier(@4, $4)),
new List< NamedType* >(),
$6);
}
| T_Class T_Identifier T_Implements InterfaceList '{' FieldList '}'
{
$$ = new ClassDecl(new Identifier(@2, $2),
NULL,
$4,
$6);
}
| T_Class T_Identifier T_Extends T_Identifier T_Implements InterfaceList '{' FieldList '}'
{
$$ = new ClassDecl(new Identifier(@2, $2),
new NamedType(new Identifier(@4, $4)),
$6,
$8);
}
;
InterfaceList : T_Identifier AInterface { ($$ = $2)->InsertAt(new NamedType(new Identifier(@1, $1)), 0); }
;
AInterface : ',' T_Identifier AInterface { ($$ = $3)->InsertAt(new NamedType(new Identifier(@2, $2)), 0); }
| { $$ = new List< NamedType* >(); }
;
FieldList : Field FieldList { ($$ = $2)->InsertAt($1, 0); }
| { $$ = new List< Decl* >(); }
;
Field : VariableDecl { $$ = $1; }
| FunctionDecl { $$ = $1; }
;
StmtBlock : '{' VariableDeclList StmtList '}' {
$$ = new StmtBlock($2, $3);
}
;
VariableDeclList : VariableDeclList VariableDecl {
($$ = $1)->Append($2);
}
| { $$ = new List< VarDecl* >(); }
;
StmtList : Stmt StmtList { ($$ = $2)->InsertAt($1, 0); }
| { $$ = new List< Stmt* >(); }
;
Stmt : ';' { /* ? */ }
| Expr ';' { $$ = $1; }
| IfStmt { $$ = $1; }
| WhileStmt { $$ = $1; }
| ForStmt { $$ = $1; }
| T_Break ';' { $$ = new BreakStmt(@1); }
| ReturnStmt { $$ = $1; }
| PrintStmt { $$ = $1; }
| StmtBlock { $$ = $1; }
;
/* Dangling-else in our LALR: jUST rELAX */
IfStmt : T_If '(' Expr ')' Stmt { $$ = new IfStmt($3, $5, NULL); }
| T_If '(' Expr ')' Stmt T_Else Stmt { $$ = new IfStmt($3, $5, $7); }
WhileStmt : T_While '(' Expr ')' Stmt { $$ = new WhileStmt($3, $5); }
ForStmt : T_For '(' Expr ';' Expr ';' Expr ')' Stmt { $$ = new ForStmt($3, $5, $7, $9); }
| T_For '(' Expr ';' Expr ';' ')' Stmt { $$ = new ForStmt($3, $5, new EmptyExpr(), $8); }
| T_For '(' ';' Expr ';' Expr ')' Stmt { $$ = new ForStmt(new EmptyExpr(), $4, $6, $8); }
| T_For '(' ';' Expr ';' ')' Stmt { $$ = new ForStmt(new EmptyExpr(), $4, new EmptyExpr(), $7); }
ReturnStmt : T_Return Expr ';' { $$ = new ReturnStmt(@1, $2); }
| T_Return ';' { $$ = new ReturnStmt(@1, new EmptyExpr()); }
;
PrintStmt : T_Print '(' PrintList ')' ';' { $$ = new PrintStmt($3); }
;
PrintList : Expr AExpr { ($$ = $2)->InsertAt($1, 0); }
/* | Expr { ($$ = new List< Expr* >())->InsertAt($1, 0); } */
;
Expr : LValue '=' Expr { $$ = new AssignExpr($1, new Operator(@2, "="), $3); }
| Constant { $$ = $1; }
| LValue { $$ = $1; }
| T_This { $$ = new This(@1); }
| Call { $$ = $1; }
| '(' Expr ')' { $$ = $2; }
| Expr '+' Expr { $$ = new ArithmeticExpr($1, new Operator(@2, "+"), $3); }
| Expr '-' Expr { $$ = new ArithmeticExpr($1, new Operator(@2, "-"), $3); }
| Expr '*' Expr { $$ = new ArithmeticExpr($1, new Operator(@2, "*"), $3); }
| Expr '/' Expr { $$ = new ArithmeticExpr($1, new Operator(@2, "/"), $3); }
| Expr '%' Expr { $$ = new ArithmeticExpr($1, new Operator(@2, "%"), $3); }
| '-' Expr %prec UMINUS { $$ = new ArithmeticExpr(new Operator(@1, "-"), $2); }
| Expr '<' Expr { $$ = new RelationalExpr($1, new Operator(@2, "<"), $3); }
| Expr T_LessEqual Expr { $$ = new RelationalExpr($1, new Operator(@2, "<="), $3); }
| Expr '>' Expr { $$ = new RelationalExpr($1, new Operator(@2, ">"), $3); }
| Expr T_GreaterEqual Expr { $$ = new RelationalExpr($1, new Operator(@2, ">="), $3); }
| Expr T_Equal Expr { $$ = new EqualityExpr($1, new Operator(@2, "=="), $3); }
| Expr T_NotEqual Expr { $$ = new EqualityExpr($1, new Operator(@2, "!="), $3); }
| Expr T_And Expr { $$ = new LogicalExpr($1, new Operator(@2, "&&"), $3); }
| Expr T_Or Expr { $$ = new LogicalExpr($1, new Operator(@2, "||"), $3); }
| '!' Expr { $$ = new LogicalExpr(new Operator(@1, "!"), $2); }
| T_ReadInteger '(' ')' { $$ = new ReadIntegerExpr(@1); }
| T_ReadLine '(' ')' { $$ = new ReadLineExpr(@1); }
| T_New '(' T_Identifier ')' {
NamedType* newt = new NamedType(new Identifier(@3, $3));
$$ = new NewExpr(@1, newt);
}
| T_NewArray '(' Expr ',' Type ')' { $$ = new NewArrayExpr(@1, $3, $5); }
;
LValue : T_Identifier { $$ = new FieldAccess(NULL, new Identifier(@1, $1)); }
| Expr '.' T_Identifier { $$ = new FieldAccess($1, new Identifier(@3, $3)); }
| Expr '[' Expr ']' { $$ = new ArrayAccess(@1, $1, $3); }
;
Constant : T_IntConstant { $$ = new IntConstant(@1, $1); }
| T_DoubleConstant { $$ = new DoubleConstant(@1, $1); }
| T_BoolConstant { $$ = new BoolConstant(@1, $1); }
| T_StringConstant { $$ = new StringConstant(@1, $1); }
| T_Null { $$ = new NullConstant(@1); }
;
Call : T_Identifier '(' Actuals ')' {
$$ = new Call(@1, NULL, new Identifier(@1, $1), $3);
}
| Expr '.' T_Identifier '(' Actuals ')' {
$$ = new Call(@1, $1, new Identifier(@3, $3), $5);
}
;
Actuals : Expr AExpr { ($$ = $2)->InsertAt($1, 0); }
| { $$ = new List< Expr* >(); }
;
AExpr : ',' Expr AExpr { ($$ = $3)->InsertAt($2, 0); }
| { $$ = new List< Expr* >(); }
;
%%
/* The closing %% above marks the end of the Rules section and the beginning
* of the User Subroutines section. All text from here to the end of the
* file is copied verbatim to the end of the generated y.tab.c file.
* This section is where you put definitions of helper functions.
*/
/* Function: InitParser
* --------------------
* This function will be called before any calls to yyparse(). It is designed
* to give you an opportunity to do anything that must be done to initialize
* the parser (set global variables, configure starting state, etc.). One
* thing it already does for you is assign the value of the global variable
* yydebug that controls whether yacc prints debugging information about
* parser actions (shift/reduce) and contents of state stack during parser.
* If set to false, no information is printed. Setting it to true will give
* you a running trail that might be helpful when debugging your parser.
* Please be sure the variable is set to false when submitting your final
* version.
*/
void InitParser()
{
PrintDebug("parser", "Initializing parser");
yydebug = false ;
}