-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.y
722 lines (663 loc) · 16.6 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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
/*
* FILE: parser.y
* PURPOSE: bison parser -> creates abstract syntax tree or returns errors
*
* AUTHOR: SWS -> Thank you!
* STUDENTS: Yondon Fu and Matt McFarland - Delights (CS 57 - 16W)
*
* Note: LC == "Left Child"
* RS == "Right Sibling"
*
*/
%{
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "src/ast.h" // defines ast node types and functions
#include <assert.h>
#define YYSTYPE ast_node // override default node type
#define YYDEBUG 1 // turn on debugging
#define MAX_ERRORS 6 // will stop parsing after this many syntax errors
/* from .l file */
extern int yylex();
extern char *yytext;
extern int yylineno;
extern char savedIdText[];
extern char savedLiteralText[];
/* from .c main file */
extern ast_node root;
extern int parseError;
/* in this .y file */
int yyerror(char *s);
int errors; // global error count
%}
%error-verbose
/* don't change this token identifier order */
%token ID_T INT_T STRING_T TYPEINT_T IF_T ELSE_T DO_T WHILE_T RETURN_T BREAK_T CONTINUE_T FOR_T VOID_T READ_T PRINT_T SIZEOF_T '+' '-' '*' '/' '=' '<' '>' LTE_T GTE_T EQ_T NE_T INCR_T DECR_T AND_T OR_T '!' ';' ',' '(' ')' '[' ']' '{' '}' '%' COMMENT_T OTHER_T
/* from flex&bison book: how to resolve if/then/else */
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE_T
/* Lowest Precedence */
%right '='
%left OR_T
%left AND_T
%left EQ_T NE_T
%left '<' '>' GTE_T LTE_T
%left '+' '-'
%left '*' '/' '%'
%left '!' INCR_T DECR_T UMINUS_T
/* Highest Precedence */
%%
/*
* RULE 1
*
* LC of program is list of DECLARATION_N in RS's
*/
program : declaration_list {
ast_node t = create_ast_node(ROOT_N);
t->left_child = $1;
root = $$ = t; }
;
/*
* RULE 2
*
* A declaration list is composed of a DECLARATION_N with RS of DECLARATION_N nodes in a list
*/
declaration_list : declaration_list declaration {
ast_node t = $1;
if (t != NULL) {
while (t->right_sibling != NULL)
t = t->right_sibling;
t->right_sibling = $2;
$$ = $1;
}
else
$$ = $2;
}
| /* empty */ { $$ = NULL; }
;
/*
* RULE 3
*
* Node will be either VAR_DECLARATION_N or FUNC_DECLARATION_N
*/
declaration : var_declaration { $$ = $1; }
| func_declaration { $$ = $1; }
;
/*
* RULE 4
*
* LC is TYPE_SPEC_N, RSs of LC are VAR_DECL_N nodes
*/
var_declaration : type_specifier var_declaration_list ';' {
ast_node t = create_ast_node(VAR_DECLARATION_N);
t->left_child = $1;
t->left_child->right_sibling = $2;
$$ = t; }
;
/*
* RULE 5 & 9 REPLACEMENT (void and int recognizers)
*
* LC will be either VOID_N or TYPEINT_N
*/
type_specifier : TYPEINT_T {
ast_node t = create_ast_node(TYPE_SPEC_N);
ast_node int_n = create_ast_node(TYPEINT_N);
t->left_child = int_n;
$$ = t; }
| VOID_T {
ast_node t = create_ast_node(TYPE_SPEC_N);
ast_node void_n = create_ast_node(VOID_N);
t->left_child = void_n;
$$ = t; }
;
/*
* RULE 6
*
* var_list_declaration is just a VAR_DECLAR_N with VAR_DECLAR_N on RS
*/
var_declaration_list : var_declaration_list ',' var_decl {
ast_node t = $1;
if (t != NULL) {
while (t->right_sibling != NULL)
t = t->right_sibling;
t->right_sibling = $3;
$$ = $1;
}
else
$$ = $3;
}
| var_decl { $$ = $1; } // must have at least one element in list
;
/*
* RULE 7
*
* If only left child -> single variable declaration
* If right sibling is of type expression -> single variable assignment
* If right sibling is of INT_N type -> array declaration
*/
var_decl : ID_T {
ast_node t = create_ast_node(VAR_DECL_N);
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
t->left_child = id_n;
$$ = t; }
| ID_T '=' expression {
ast_node t = create_ast_node(VAR_DECL_N);
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
t->left_child = id_n;
t->left_child->right_sibling = $3;
$$ = t; }
| ID_T '[' INT_T ']' {
ast_node t = create_ast_node(VAR_DECL_N);
ast_node id_n = create_ast_node(ID_N);
ast_node int_n = create_ast_node(INT_LITERAL_N);
id_n->value_string = strdup(savedIdText);
int_n->value_int = atoi(savedLiteralText);
t->left_child = id_n;
t->left_child->right_sibling = int_n;
$$ = t; }
;
/*
* RULE 8
*
* More order weirdness! So each func_declaration MUST have a type and an identifier
* but it might not have a compound statement. If it doesn't, then the compound statement will be empty
*/
func_declaration : type_specifier ID_T {
/* embedded action to save function identifer */
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
$2 = id_n;
} '(' formal_params ')' compound_stmt {
ast_node t = create_ast_node(FUNC_DECLARATION_N);
ast_node child;
t->left_child = $1;
t->left_child->right_sibling = $2;
t->left_child->right_sibling->right_sibling = $5;
t->left_child->right_sibling->right_sibling->right_sibling = $7;
$$ = t; }
;
/*
* RULE 10
*
* Formal parameters: if list, left child is FORMAL_LIST_N
* if void, left child is VOID_N
* if unspecified, left child is a VOID_N
*/
formal_params : formal_list {
ast_node t = create_ast_node(FORMAL_PARAMS_N);
t->left_child = $1;
$$ = t; }
| VOID_T {
ast_node t = create_ast_node(FORMAL_PARAMS_N);
ast_node void_node = create_ast_node(VOID_N);
t->left_child = void_node;
$$ = t; }
| /* empty */ { // can't just return null because above productions depend on this right sibling
ast_node t = create_ast_node(FORMAL_PARAMS_N);
ast_node void_node = create_ast_node(VOID_N);
t->left_child = void_node;
$$ = t;
}
;
/*
* RULE 11
*
* left child and all right siblings are each a FORMAL_PARAM_N
*/
formal_list : formal_list ',' formal_param {
ast_node t = $1;
if (t != NULL) {
while (t->right_sibling)
t = t->right_sibling;
t->right_sibling = $3;
$$ = $1;
}
else
$$ = $3;
}
| formal_param { $$ = $1; } // must have 1 parameter in formal_list
;
/*
* RULE 12
*
* I think this is used for declaration of a parameter for a function
* Need to differentiate with array and single parameters -> two different N types
*/
formal_param : type_specifier ID_T {
ast_node t = create_ast_node(FORMAL_PARAM_N); // save ID string in ID_N at right_sibling
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
t->left_child = $1;
t->left_child->right_sibling = id_n;
$$ = t; }
| type_specifier ID_T '[' ']' {
ast_node t = create_ast_node(FORMAL_PARAM_ARR_N); // save ID string in ID_N at right_sibling
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
t->left_child = $1;
t->left_child->right_sibling = id_n;
$$ = t; }
;
/*
* RULE 13
*
* Simplify this node: there is a long string of children (left_child->right_siblings chain)
* that concanenate the statement list to end of the local declaration list. If there are no statements,
* then the children will only include declarations. Vice versa is true for null local declarations
* and statements. If this compound block is totally empty, then it's a compound statement node without any children.
*/
compound_stmt : '{' local_declarations stmt_list '}' {
ast_node t = create_ast_node(COMPOUND_STMT_N);
ast_node d = $2;
ast_node l;
if (d != NULL) {
t->left_child = d;
/* get to end of declaration list */
l = d;
while (l->right_sibling != NULL) {
l = l->right_sibling;
}
l->right_sibling = $3;
} else {
t->left_child = $3;
}
$$ = t;
}
;
/*
* RULE 14
*
* LOCAL_DECLARATION_N has a TYPE_SPEC_N as left child and then VAR_DELC_N as right siblings
* no children means no local variables are declared
*/
local_declarations : local_declarations var_declaration {
ast_node t = $1;
if (t != NULL) {
while (t->right_sibling != NULL)
t = t->right_sibling;
t->right_sibling = $2;
$$ = $1;
}
else
$$ = $2;
}
| /* empty */ { $$ = NULL; }
;
/*
* RULE 15
*
* see rule 26 for types of available statement children
* each statemnt is left child -> right siblings (x0, x1, x2 ...)
* no left child if STMT_LIST_N is NULL
*/
stmt_list : stmt_list stmt {
ast_node t = $1;
if (t != NULL) {
while (t->right_sibling != NULL)
t = t->right_sibling;
t->right_sibling = $2;
$$ = $1;
} else
$$ = $2;
}
| /* empty */ { $$ = NULL; }
;
/*
* RULE 16
*
* No "STMT_N" to reduce to. Each production has it's own node type already.
*/
stmt :
expression_stmt { $$ = $1; }
| compound_stmt { $$ = $1; }
| if_stmt { $$ = $1; }
| while_stmt { $$ = $1; }
| do_while_stmt { $$ = $1; }
| for_stmt { $$ = $1; }
| return_stmt { $$ = $1; }
| read_stmt { $$ = $1; }
| print_stmt { $$ = $1; }
| break_stmt { $$ = $1; }
| continue_stmt { $$ = $1; }
;
/*
* RULE 17
*
* If no expression before semicolon, then no children
*/
expression_stmt : expression ';' {
ast_node t = create_ast_node(EXPRESSION_STMT_N);
t->left_child = $1;
$$ = t; }
| /* empty */ ';' {
ast_node t = create_ast_node(EXPRESSION_STMT_N);
$$ = t; }
| error ';' { $$ = NULL; }
;
/*
* RULE 18
*
* Taken / Adapted from tree.5.y
* Author: SWS
*/
if_stmt : IF_T '(' expression ')' stmt %prec LOWER_THAN_ELSE {
ast_node t = create_ast_node(IF_STMT_N);
t->left_child = $3;
t->left_child->right_sibling = $5;
$$ = t; }
| IF_T '(' expression ')' stmt ELSE_T stmt {
ast_node t = create_ast_node(IF_ELSE_STMT_N);
t->left_child = $3;
t->left_child->right_sibling = $5;
t->left_child->right_sibling->right_sibling = $7;
$$ = t; }
| IF_T '(' error ')' stmt %prec LOWER_THAN_ELSE
{ $$ = NULL; }
| IF_T '(' error ')' stmt ELSE_T stmt
{ $$ = NULL; }
;
/*
* RULE 19
*/
while_stmt : WHILE_T '(' expression ')' stmt {
ast_node t = create_ast_node(WHILE_N);
t->left_child = $3;
t->left_child->right_sibling = $5;
$$ = t; }
| WHILE_T '(' error ')' stmt { $$ = NULL; }
;
/*
* RULE 20
*/
do_while_stmt : DO_T stmt WHILE_T '(' expression ')' ';' {
ast_node t = create_ast_node(DO_WHILE_N);
assert($2);
t->left_child = $2;
t->left_child->right_sibling = $5;
$$ = t; }
| DO_T stmt WHILE_T '(' error ')' ';' { $$ = NULL; }
;
/*
* RULE 21
*/
for_stmt : FOR_T '(' for_header_expr ';' for_header_expr ';' for_header_expr ')' stmt {
ast_node t = create_ast_node(FOR_STMT_N);
t->left_child = $3;
t->left_child->right_sibling = $5;
t->left_child->right_sibling->right_sibling = $7;
t->left_child->right_sibling->right_sibling->right_sibling = $9;
$$ = t; }
| FOR_T '(' error ')' error { $$ = NULL;}
;
/*
* RULE 22
*
* If FOR_HEADER_EXPR matches empty statement, returns a FOR_HEADER_N without children
*/
for_header_expr : expression {
ast_node t = create_ast_node(FOR_HEADER_N);
t->left_child = $1;
$$ = t; }
| /* empty */ {
ast_node t = create_ast_node(FOR_HEADER_N);
$$ = t; }
;
/*
* RULE 23
*
* Return expression / void_N is left child
*/
return_stmt : RETURN_T ';' {
ast_node t = create_ast_node(RETURN_N);
ast_node void_n = create_ast_node(VOID_N);
t->left_child = void_n;
$$ = t; }
| RETURN_T expression ';' {
ast_node t = create_ast_node(RETURN_N);
t->left_child = $2;
$$ = t; }
| RETURN_T error ';' { $$ = NULL; }
;
/*
* RULE 24
*/
read_stmt : READ_T var ';' {
ast_node t = create_ast_node(READ_N);
t->left_child = $2;
$$ = t; }
| READ_T error ';' { $$ = NULL; }
;
/*
* RULE 25
*
* If printing a string, then left child will be STRING_N
* else if printing expression, left child will be expression
*/
print_stmt : PRINT_T expression ';' {
ast_node t = create_ast_node(PRINT_N);
t->left_child = $2;
$$ = t; }
| PRINT_T STRING_T {
/* embedded action to grab string text */
ast_node str_n = create_ast_node(STRING_N);
str_n->value_string = strdup(yytext);
$2 = str_n;
} ';' {
ast_node t = create_ast_node(PRINT_N);
t->left_child = $2;
$$ = t; }
| PRINT_T error ';' { $$ = NULL; }
;
break_stmt : BREAK_T ';' {
ast_node t = create_ast_node(BREAK_N);
$$ = t; }
;
continue_stmt : CONTINUE_T ';' {
ast_node t = create_ast_node(CONTINUE_N);
$$ = t; }
;
/*
* RULE 26
*/
expression : var '=' expression {
ast_node t = create_ast_node(OP_ASSIGN_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| r_value {
ast_node t = create_ast_node(EXPRESSION_N);
t->left_child = $1;
$$ = t; }
;
/*
* RULE 27
*
* Left child is always an ID_N with ID string saved there
* Right sibling is null for VAR_N
* Right sibling is not null for VAR_N if referencing an array
*/
var : ID_T {
ast_node t = create_ast_node(VAR_N);
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
t->left_child = id_n;
$$ = t; }
| ID_T {
/* embedded action to catch ID_T string */
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
$1 = id_n;
} '[' expression ']' {
ast_node t = create_ast_node(VAR_N);
t->left_child = $1;
t->left_child->right_sibling = $4;
$$ = t; }
;
/*
* RULE 28
* taken from tree.5.y and modified / expanded for our grammar
*/
r_value :
expression '+' expression {
ast_node t = create_ast_node(OP_PLUS_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression '-' expression {
ast_node t = create_ast_node(OP_MINUS_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression '*' expression {
ast_node t = create_ast_node(OP_TIMES_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression '/' expression {
ast_node t = create_ast_node(OP_DIVIDE_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression '%' expression {
ast_node t = create_ast_node(OP_MOD_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression '<' expression {
ast_node t = create_ast_node(OP_LT_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression LTE_T expression {
ast_node t = create_ast_node(OP_LTE_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression '>' expression {
ast_node t = create_ast_node(OP_GT_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression GTE_T expression {
ast_node t = create_ast_node(OP_GTE_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression EQ_T expression {
ast_node t = create_ast_node(OP_EQ_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression NE_T expression {
ast_node t = create_ast_node(OP_NE_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression AND_T expression {
ast_node t = create_ast_node(OP_AND_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| expression OR_T expression {
ast_node t = create_ast_node(OP_OR_N);
t->left_child = $1;
t->left_child->right_sibling = $3;
$$ = t; }
| '!' expression {
ast_node t = create_ast_node(OP_NOT_N);
t->left_child = $2;
$$ = t; }
| '-' expression %prec UMINUS_T {
ast_node t = create_ast_node(OP_NEG_N);
t->left_child = $2;
$$ = t; }
| var { $$ = $1; } // note, set VAR to EXPR here
| INCR_T var {
ast_node t = create_ast_node(OP_PRE_INC_N);
t->left_child = $2;
$$ = t; }
| var INCR_T {
ast_node t = create_ast_node(OP_POST_INC_N);
t->left_child = $1;
$$ = t; }
| DECR_T var {
ast_node t = create_ast_node(OP_PRE_DEC_N);
t->left_child = $2;
$$ = t; }
| var DECR_T {
ast_node t = create_ast_node(OP_POST_DEC_N);
t->left_child = $1;
$$ = t; }
| call { $$ = $1; } // note, set expr = call here
| '(' expression ')' { $$ = $2; }
| INT_T {
ast_node t = create_ast_node(INT_LITERAL_N);
t->value_int = atoi(savedLiteralText);
$$ = t; }
| SIZEOF_T '(' var ')' {
ast_node t = create_ast_node(SIZEOF_N);
t->left_child = $3;
$$ = t; }
| SIZEOF_T '(' error ')' { $$ = NULL; }
| '(' error ')' { $$ = NULL; }
;
/*
* RULE 29
*
* left_child is ID_N with ID string saved in value string
* left_child->right_sibling is NULL if no arguements are given
*/
call : ID_T {
/* embedded action to save function call ID string */
ast_node id_n = create_ast_node(ID_N);
id_n->value_string = strdup(savedIdText);
$1 = id_n;
} '(' args ')' {
ast_node t = create_ast_node(CALL_N);
t->left_child = $1;
t->left_child->right_sibling = $4;
$$ = t; }
;
/*
* RULE 30
*
* if ARGS is null, then no arguments
* if ARGS is not null, then it is a ARGS_LIST_N
*/
args : arg_list { $$ = $1; }
| /* empty */ { $$ = NULL; }
;
/*
* RULE 31
*
* Each child and sibling under the ARG_LIST_N is an expression.
*/
arg_list : arg_list ',' expression {
ast_node t = $1;
if (t->left_child) {
t = t->left_child;
while (t->right_sibling)
t = t->right_sibling;
t->right_sibling = $3;
$$ = $1;
}
}
| expression {
ast_node t = create_ast_node(ARG_LIST_N);
t->left_child = $1;
$$ = t; }
| error { $$ = NULL; }
;
%%
int yyerror(char *s) {
parseError = 1;
fprintf(stderr, "%s at line %d\n", s, yylineno);
if (++errors == MAX_ERRORS) {
fprintf(stderr,"Too many syntax errors have occurred. Aborting parse attempt.\n");
exit(1);
}
return 0;
}