-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
627 lines (548 loc) · 24 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
%{
#include <cstring>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include "src/Factory.hpp"
// abstract node classes
#include "src/Node.hpp"
#include "src/ExpressionNode.hpp"
#include "src/StatementNode.hpp"
// concrete node classes
#include "src/AddNode.hpp"
#include "src/AndNode.hpp"
#include "src/AssignmentStatementNode.hpp"
#include "src/CharacterExpressionNode.hpp"
#include "src/CharacterLiteralNode.hpp"
#include "src/ConstantDeclarationNode.hpp"
#include "src/DivideNode.hpp"
#include "src/EqualExpressionNode.hpp"
#include "src/ForStatementNode.hpp"
#include "src/GreaterThanEqualNode.hpp"
#include "src/GreaterThanNode.hpp"
#include "src/IdentifierNode.hpp"
#include "src/IfStatementNode.hpp"
#include "src/IntegerLiteralNode.hpp"
#include "src/LessThanEqualNode.hpp"
#include "src/LessThanNode.hpp"
#include "src/ListNode.hpp"
#include "src/LvalueNode.hpp"
#include "src/MemberAccessNode.hpp"
#include "src/ModuloNode.hpp"
#include "src/MultiplyNode.hpp"
#include "src/NotEqualExpressionNode.hpp"
#include "src/NotNode.hpp"
#include "src/OrNode.hpp"
#include "src/OrdinalExpressionNode.hpp"
#include "src/PredecessorExpressionNode.hpp"
#include "src/ProgramNode.hpp"
#include "src/ReadStatementNode.hpp"
#include "src/RepeatStatementNode.hpp"
#include "src/StopStatementNode.hpp"
#include "src/StringLiteralNode.hpp"
#include "src/SubscriptOperatorNode.hpp"
#include "src/SubtractNode.hpp"
#include "src/SuccessorExpressionNode.hpp"
#include "src/SymbolTable.hpp"
#include "src/TypeDeclarationNode.hpp"
#include "src/TypeNode.hpp"
#include "src/UnaryMinusNode.hpp"
#include "src/VariableDeclarationNode.hpp"
#include "src/WhileStatementNode.hpp"
#include "src/WriteStatementNode.hpp"
class Type;
#define YYERROR_VERBOSE 1
#define DEBUG 1
extern "C" int yylex();
extern char * yytext;
extern std::string yylinetxt;
extern unsigned int yylineno;
extern unsigned int yycolumn;
extern std::shared_ptr<ProgramNode> programNode;
void yyerror(const char*);
%}
%define parse.trace
%union
{
int int_val;
char char_val;
char * str_val;
Field * field;
Node * node;
StatementNode * statementNode;
ExpressionNode * expressionNode;
TypeNode * type;
/*std::shared_ptr<RecordType> * recordType;*/
/*std::shared_ptr<ArrayType> * arrayType;*/
AssignmentStatementNode * assignmentNode;
ConstantDeclarationNode * constDeclNode;
IdentifierNode * identifier;
IfStatementNode * ifStatementNode;
LvalueNode * lvalue;
ReadStatementNode * readStatementNode;
StopStatementNode * stopStatementNode;
TypeDeclarationNode * typeDeclarationNode;
VariableDeclarationNode * varDeclNode;
WriteStatementNode * writeStatementNode;
ListNode<ConstantDeclarationNode> * constDelcList;
ListNode<ExpressionNode> * expressionList;
ListNode<Field> * fieldList;
ListNode<LvalueNode> * lValueList;
ListNode<StatementNode> * statementList;
ListNode<TypeDeclarationNode> * typeDeclarationList;
ListNode<VariableDeclarationNode> * varDelcList;
ListNode<std::string> * identList;
ListNode<std::pair<std::shared_ptr<ExpressionNode>, std::vector<std::shared_ptr<StatementNode>>>> * elseIfList;
}
%token ARRAY_T
%token BEGIN_T
%token CHR_T
%token CONST_T
%token DO_T
%token DOWNTO_T
%token ELSE_T
%token ELSEIF_T
%token END_T
%token FOR_T
%token FORWARD_T
%token FUNCTION_T
%token IF_T
%token OF_T
%token ORD_T
%token PRED_T
%token PROCEDURE_T
%token READ_T
%token RECORD_T
%token REF_T
%token REPEAT_T
%token RETURN_T
%token STOP_T
%token SUCC_T
%token THEN_T
%token TO_T
%token TYPE_T
%token UNTIL_T
%token VAR_T
%token WHILE_T
%token WRITE_T
%token ID_T;
%token PLUS_T
%token MINUS_T
%token UNARY_MINUS_T
%token MULTIPLY_T
%token DIVIDE_T
%token AND_T
%token OR_T
%token NOT_T
%token EQUAL_T
%token NEQUAL_T
%token LT_T
%token LTE_T
%token GT_T
%token GTE_T
%token DOT_T
%token COMMA_T
%token COLON_T
%token SEMI_COLON_T
%token OPEN_PAREN_T
%token CLOSE_PAREN_T
%token OPEN_BRACKET_T
%token CLOSE_BRACKET_T
%token ASSIGN_T
%token MOD_T
%token NUMBER_T
%token CHAR_T
%token STRING_T
%type <int_val> NUMBER_T
%type <str_val> STRING_T
%type <char_val> CHAR_T
%type <str_val> ID_T
%type <node> Program
%type <constDelcList> OptConstDecls
%type <constDelcList> ConstDeclList
%type <constDeclNode> ConstDecl
%type <node> OptProcedureAndFunctionDeclList
%type <node> ProcedureAndFunctionDeclList
%type <node> ProcedureDecl
%type <node> FunctionDecl
%type <node> FormalParameters
%type <node> FormalParameterList
%type <node> FormalParameter
%type <statementList> Block
%type <typeDeclarationList> OptTypeDecls
%type <typeDeclarationList> TypeDeclList
%type <typeDeclarationNode> TypeDecl
%type <type> Type
%type <type> SimpleType
%type <type> RecordType
/*%type <recordType> RecordType*/
%type <fieldList> OptFieldList
%type <fieldList> FieldList
%type <field> Field
%type <type> ArrayType
/*%type <arrayType> ArrayType*/
%type <identList> IdentList
%type <varDelcList> OptVariableDecls
%type <varDelcList> VariableDeclList
%type <varDeclNode> VariableDecl
%type <statementList> StatementList
%type <statementNode> Statement
%type <assignmentNode> Assignment
%type <ifStatementNode> IfStatement
%type <elseIfList> OptElseIfStatementList
%type <elseIfList> ElseIfStatementList
%type <statementList> OptElseStatement
%type <statementNode> WhileStatement
%type <statementNode> RepeatStatement
%type <node> ForStatement
%type <stopStatementNode> StopStatement
%type <node> ReturnStatement
%type <readStatementNode> ReadStatement
%type <lValueList> LValueList
%type <writeStatementNode> WriteStatement
%type <expressionList> OptExpressionList
%type <expressionList> ExpressionList
%type <node> ProcedureCall
%type <expressionNode> Expression
%type <lvalue> LValue
%left OR_T
%left AND_T
%right NOT_T
%nonassoc EQUAL_T NEQUAL_T LT_T LTE_T GT_T GTE_T
%left PLUS_T MINUS_T
%left MULTIPLY_T DIVIDE_T MOD_T
%right UNARY_MINUS_T
%%
/* Opt prefix means zero or one (?) */
/* List postfix means one or more (+) */
/* OptList means zero or more (*) */
Program : OptConstDecls
OptTypeDecls
OptVariableDecls
OptProcedureAndFunctionDeclList
Block DOT_T
{
programNode = std::make_shared<ProgramNode>($1, $2, $3, $5);
}
;
/* 3.1.1 Constant Declerations */
OptConstDecls : CONST_T ConstDeclList { $$ = $2; }
| /* λ */ { $$ = nullptr; }
;
ConstDeclList : ConstDeclList ConstDecl
{
$$ = new ListNode<ConstantDeclarationNode>($2, $1);
}
| ConstDecl
{
$$ = new ListNode<ConstantDeclarationNode>($1);
}
;
ConstDecl : ID_T EQUAL_T Expression SEMI_COLON_T
{
$$ = makeConstantDeclarationNode($1, $3);
}
;
/* 3.1.2 Procedure and Function Declarations */
OptProcedureAndFunctionDeclList : ProcedureAndFunctionDeclList { $$ = nullptr; }
| /* λ */ { $$ = nullptr; }
;
ProcedureAndFunctionDeclList : ProcedureAndFunctionDeclList ProcedureDecl {}
| ProcedureAndFunctionDeclList FunctionDecl {}
| ProcedureDecl {}
| FunctionDecl {}
;
ProcedureDecl : PROCEDURE_T ID_T OPEN_PAREN_T FormalParameters CLOSE_PAREN_T
SEMI_COLON_T FORWARD_T SEMI_COLON_T {}
| PROCEDURE_T ID_T OPEN_PAREN_T FormalParameters CLOSE_PAREN_T
SEMI_COLON_T Body SEMI_COLON_T {}
;
FunctionDecl : FUNCTION_T ID_T OPEN_PAREN_T FormalParameters CLOSE_PAREN_T
COLON_T Type SEMI_COLON_T FORWARD_T SEMI_COLON_T
{
}
| FUNCTION_T ID_T OPEN_PAREN_T FormalParameters CLOSE_PAREN_T
COLON_T Type SEMI_COLON_T Body SEMI_COLON_T
{
}
;
FormalParameters : FormalParameterList {}
| /* λ */ {}
;
FormalParameterList : FormalParameterList SEMI_COLON_T FormalParameter {}
| FormalParameter {}
;
FormalParameter : VAR_T IdentList COLON_T Type {}
| REF_T IdentList COLON_T Type {}
| IdentList COLON_T Type {}
;
Body : OptConstDecls OptTypeDecls OptVariableDecls Block {}
;
Block : BEGIN_T StatementList END_T { $$ = $2; }
;
/* 3.1.3 Type Declerations */
OptTypeDecls : TYPE_T TypeDeclList { $$ = $2; }
| /* λ */ { $$ = nullptr; }
;
TypeDeclList : TypeDeclList TypeDecl
{
$$ = new ListNode<TypeDeclarationNode>($2, $1);
}
| TypeDecl
{
$$ = new ListNode<TypeDeclarationNode>($1);
}
;
TypeDecl : ID_T EQUAL_T Type SEMI_COLON_T
{
$$ = new TypeDeclarationNode($1, $3);
}
;
Type : SimpleType { $$ = $1; }
| RecordType { $$ = $1; }
| ArrayType { $$ = $1; }
;
SimpleType : ID_T
{
$$ = new TypeNode($1);
}
;
RecordType : RECORD_T OptFieldList END_T
{
$$ = new TypeNode(std::make_shared<RecordType>($2));
}
;
OptFieldList : FieldList { $$ = $1; }
| /* λ */ { $$ = nullptr; }
;
FieldList : FieldList Field
{
$$ = new ListNode<Field>($2, $1);
}
| Field
{
$$ = new ListNode<Field>($1);
}
;
Field : IdentList COLON_T Type SEMI_COLON_T { $$ = new Field($1, $3); }
;
ArrayType : ARRAY_T
OPEN_BRACKET_T Expression COLON_T Expression CLOSE_BRACKET_T
OF_T Type
{
$$ = new TypeNode(std::make_shared<ArrayType>($3, $5, $8));
}
;
IdentList : IdentList COMMA_T ID_T
{
$$ = new ListNode<std::string>(new std::string($3), $1);
}
| ID_T
{
$$ = new ListNode<std::string>(new std::string($1));
}
;
/* 3.1.4 Variable Declerations */
OptVariableDecls : VAR_T VariableDeclList { $$ = $2; }
| /* λ */ { $$ = nullptr; }
;
VariableDeclList : VariableDeclList VariableDecl
{
$$ = new ListNode<VariableDeclarationNode>($2, $1);
}
| VariableDecl
{
$$ = new ListNode<VariableDeclarationNode>($1);
}
;
VariableDecl : IdentList COLON_T Type SEMI_COLON_T
{
$$ = new VariableDeclarationNode($1, $3);
}
;
/* 3.2 CPSL Statements */
StatementList : StatementList SEMI_COLON_T Statement
{
$$ = new ListNode<StatementNode>($3, $1);
}
| Statement
{
$$ = new ListNode<StatementNode>($1);
}
;
Statement : Assignment {}
| IfStatement {}
| WhileStatement {}
| RepeatStatement {}
| ForStatement {}
| StopStatement { $$ = $1; }
| ReturnStatement {}
| ReadStatement {}
| WriteStatement { $$ = $1; }
| ProcedureCall {}
| { $$ = nullptr; }
;
Assignment : LValue ASSIGN_T Expression
{
$$ = new AssignmentStatementNode($1, $3);
}
;
IfStatement : IF_T Expression THEN_T
StatementList
OptElseIfStatementList
OptElseStatement END_T
{
$$ = new IfStatementNode($2, $4, $5, $6);
}
;
OptElseIfStatementList : ElseIfStatementList { $$ = $1; }
| /* λ */ { $$ = nullptr; }
;
ElseIfStatementList : ElseIfStatementList ELSEIF_T Expression THEN_T StatementList
{
$$ = new ListNode<
std::pair<
std::shared_ptr<ExpressionNode>,
std::vector<std::shared_ptr<StatementNode>>
>
>(new std::pair<
std::shared_ptr<ExpressionNode>,
std::vector<std::shared_ptr<StatementNode>>
> ($3, ListNode<StatementNode>::makeVector($5)), $1);
}
| ELSEIF_T Expression THEN_T StatementList
{
$$ = new ListNode<
std::pair<
std::shared_ptr<ExpressionNode>,
std::vector<std::shared_ptr<StatementNode>>
>
>(new std::pair<
std::shared_ptr<ExpressionNode>,
std::vector<std::shared_ptr<StatementNode>>
> ($2, ListNode<StatementNode>::makeVector($4)));
}
OptElseStatement : ELSE_T StatementList { $$ = $2; }
| /* λ */ { $$ = nullptr; }
;
WhileStatement : WHILE_T Expression DO_T StatementList END_T
{
$$ = new WhileStatementNode($2, $4);
}
;
RepeatStatement : REPEAT_T StatementList UNTIL_T Expression
{
$$ = new RepeatStatementNode($2, $4);
}
;
ForStatement : FOR_T ID_T ASSIGN_T Expression TO_T Expression
DO_T StatementList END_T
{
$$ = new ForStatementNode(new IdentifierNode($2), $4, $6, $8, ForStatementNode::Type::TO);
}
| FOR_T ID_T ASSIGN_T Expression DOWNTO_T Expression
DO_T StatementList END_T
{
$$ = new ForStatementNode(new IdentifierNode($2), $4, $6, $8, ForStatementNode::Type::DOWNTO);
}
;
StopStatement : STOP_T { $$ = new StopStatementNode(); }
;
ReturnStatement : RETURN_T {}
| RETURN_T Expression {}
;
ReadStatement : READ_T OPEN_PAREN_T LValueList CLOSE_PAREN_T
{
$$ = new ReadStatementNode($3);
}
;
LValueList : LValueList COMMA_T LValue
{
$$ = new ListNode<LvalueNode>($3, $1);
}
| LValue
{
$$ = new ListNode<LvalueNode>($1);
}
;
WriteStatement : WRITE_T OPEN_PAREN_T ExpressionList CLOSE_PAREN_T
{
$$ = new WriteStatementNode($3);
}
;
ProcedureCall : ID_T OPEN_PAREN_T OptExpressionList CLOSE_PAREN_T {}
;
OptExpressionList : ExpressionList { $$ = $1; }
| /* λ */ { $$ = nullptr; }
;
ExpressionList : ExpressionList COMMA_T Expression
{
$$ = new ListNode<ExpressionNode>($3, $1);
}
| Expression
{
$$ = new ListNode<ExpressionNode>($1);
}
;
/* 3.3 Expressions */
Expression : Expression OR_T Expression { $$ = new OrNode($1, $3); }
| Expression AND_T Expression { $$ = new AndNode($1, $3); }
| Expression EQUAL_T Expression { $$ = makeEqualNode($1, $3); }
| Expression NEQUAL_T Expression { $$ = makeNotEqualNode($1, $3); }
| Expression LTE_T Expression { $$ = new LessThanEqualNode($1, $3); }
| Expression GTE_T Expression { $$ = new GreaterThanEqualNode($1, $3); }
| Expression LT_T Expression { $$ = new LessThanNode($1, $3); }
| Expression GT_T Expression { $$ = new GreaterThanNode($1, $3); }
| Expression PLUS_T Expression { $$ = makeAddNode($1, $3); }
| Expression MINUS_T Expression { $$ = makeSubtractNode($1, $3); }
| Expression MULTIPLY_T Expression { $$ = makeMultiplyNode($1, $3); }
| Expression DIVIDE_T Expression { $$ = makeDivideNode($1, $3); }
| Expression MOD_T Expression { $$ = makeModuloNode($1, $3); }
| NOT_T Expression { $$ = new NotNode($2); }
| MINUS_T Expression %prec UNARY_MINUS_T { $$ = makeUnaryMinusNode($2); }
| OPEN_PAREN_T Expression CLOSE_PAREN_T { $$ = $2; }
| ProcedureCall { }
| CHR_T OPEN_PAREN_T Expression CLOSE_PAREN_T { $$ = new CharacterExpressionNode($3); }
| ORD_T OPEN_PAREN_T Expression CLOSE_PAREN_T { $$ = new OrdinalExpressionNode($3); }
| PRED_T OPEN_PAREN_T Expression CLOSE_PAREN_T { $$ = new PredecessorExpressionNode($3); }
| SUCC_T OPEN_PAREN_T Expression CLOSE_PAREN_T { $$ = new SuccessorExpressionNode($3); }
| LValue { $$ = $1; }
| NUMBER_T { $$ = new IntegerLiteralNode($1); }
| STRING_T { $$ = new StringLiteralNode($1); }
| CHAR_T { $$ = new CharacterLiteralNode($1); }
;
LValue : LValue DOT_T ID_T
{
$$ = new MemberAccessNode($1, $3);
}
| LValue OPEN_BRACKET_T Expression CLOSE_BRACKET_T
{
$$ = new SubscriptOperatorNode($1, $3);
}
| ID_T
{
$$ = new IdentifierNode($1);
}
;
%%
void yyerror(const char* msg)
{
std::string restofline;
std::getline(std::cin, restofline);
std::cerr << msg << std::endl;
std::stringstream ss;
ss << yylineno << ":" << yycolumn - strlen(yytext) << " " ;
std::cerr << ss.str();
std::cerr << yylinetxt << restofline << std::endl;
for(auto i = 0u; i < ss.str().length(); ++i)
{
std::cout << " ";
}
for(auto i = 0u; i < yylinetxt.length()-1; ++i)
{
std::cout << "~";
}
std::cout << "^" << std::endl;
}