-
Notifications
You must be signed in to change notification settings - Fork 3
/
piz.y
700 lines (623 loc) · 16.6 KB
/
piz.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
/* piz.y */
/*-----------------------------------------------------------------------
Developed by: #undef TEAMNAME
Based on template code provided by Harald Sondergard for COMP90045.
Provides full bison specification for the Wiz
programming language.
Original message included as follows:
"A bison syntax spec for Iz, a subset of Wiz.
For use in the COMP90045 project 2014.
Harald Sondergaard, March 2014."
-----------------------------------------------------------------------*/
%{
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "std.h"
#include "helper.h"
#include "missing.h"
extern Program *parsed_program;
extern void report_error_and_exit(const char *msg);
extern char *yytext;
int ln = 1;
void yyerror(const char *msg);
void *allocate(int size);
%}
%union {
int int_val;
float float_val;
char *str_val;
Decl *decl_val;
Decls *decls_val;
Expr *expr_val;
Stmts *stmts_val;
Stmt *stmt_val;
Program *prog_val;
Procs *procs_val;
Proc *proc_val;
Header *header_val;
Params *params_val;
Param *param_val;
Body *body_val;
Function *func_val;
Exprs *exprs_val;
Interval *inter_val;
Intervals *inters_val;
}
%token BOOL_TOKEN INT_TOKEN
%token '(' ')' ';' '[' ']' '*' '+' '/'
%token PROC_TOKEN END_TOKEN
%token NOT_TOKEN AND_TOKEN TRUE_TOKEN OR_TOKEN FALSE_TOKEN
%token REF_TOKEN VAL_TOKEN
%token WHILE_TOKEN THEN_TOKEN IF_TOKEN FI_TOKEN OD_TOKEN DO_TOKEN ELSE_TOKEN
%token WRITE_TOKEN ASSIGN_TOKEN READ_TOKEN
%token INVALID_TOKEN
%token LTEQ_TOKEN EQ_TOKEN LT_TOKEN GTEQ_TOKEN GT_TOKEN NOTEQ_TOKEN
%token INTERVAL_TOKEN
%token <int_val> NUMBER_TOKEN
%token <str_val> IDENT_TOKEN
%token <float_val> FLOAT_TOKEN
%token <str_val> STRING_TOKEN
/* Standard operator precedence */
/* As taken from the provided list of operators */
/* on the assignment worksheet */
%left OR_TOKEN
%left AND_TOKEN
%left NOT_TOKEN
%nonassoc LT_TOKEN GT_TOKEN LTEQ_TOKEN GTEQ_TOKEN EQ_TOKEN NOTEQ_TOKEN
%left '+' '-'
%left '*' '/'
%left UNARY_MINUS
/* The types for our values to be used in the abstract */
/* syntax tree */
%type <prog_val> program
%type <procs_val> procs
%type <proc_val> proc
%type <header_val> header
%type <body_val> body
%type <params_val> parameter_list
%type <params_val> params
%type <param_val> param
%type <decls_val> declarations
%type <decl_val> decl
%type <stmts_val> statements
%type <stmt_val> stmt
%type <expr_val> expr
%type <expr_val> identifier
%type <exprs_val> exprs
%type <exprs_val> exprs_list
%type <inter_val> interval
%type <inters_val> intervals
/* Types for identifying statement types */
%type <int_val> assign
%type <int_val> start_cond
%type <int_val> start_read
%type <int_val> start_while
%type <int_val> start_write
%type <int_val> get_lineno
%start program
%%
/*---------------------------------------------------------------------*/
program
: procs
{
parsed_program = allocate(sizeof(struct prog));
parsed_program->procedures = $1;
}
;
procs
: proc procs
{
$$ = allocate(sizeof(struct procs));
$$->first = $1;
$$->rest = $2;
}
;
| proc
{
$$ = allocate(sizeof(struct procs));
$$->first = $1;
$$->rest = NULL;
}
proc
: PROC_TOKEN header body END_TOKEN
{
$$ = allocate(sizeof(struct proc));
$$->header = $2;
$$->body = $3;
}
;
header
: IDENT_TOKEN '(' parameter_list ')' get_lineno
{
$$ = allocate(sizeof(struct header));
$$->id = $1;
$$->params = $3;
$$->line_no = $5;
}
;
parameter_list
: param params
{
$$ = allocate(sizeof(struct params));
$$->first = $1;
$$->rest = $2;
}
| /* Empty Params! */
{ $$ = NULL; }
;
params
: ',' param params
{
$$ = allocate(sizeof(struct params));
$$->first = $2;
$$->rest = $3;
}
| /* Empty which means end of list */
{ $$ = NULL; }
;
param
: VAL_TOKEN FLOAT_TOKEN IDENT_TOKEN
{
$$ = allocate(sizeof(struct param));
$$->ind = VAL_IND;
$$->type = FLOAT_TYPE;
$$->id = $3;
}
| VAL_TOKEN BOOL_TOKEN IDENT_TOKEN
{
$$ = allocate(sizeof(struct param));
$$->ind = VAL_IND;
$$->type = BOOL_TYPE;
$$->id = $3;
}
| VAL_TOKEN INT_TOKEN IDENT_TOKEN
{
$$ = allocate(sizeof(struct param));
$$->ind = VAL_IND;
$$->type = INT_TYPE;
$$->id = $3;
}
| REF_TOKEN FLOAT_TOKEN IDENT_TOKEN
{
$$ = allocate(sizeof(struct param));
$$->ind = REF_IND;
$$->type = FLOAT_TYPE;
$$->id = $3;
}
| REF_TOKEN BOOL_TOKEN IDENT_TOKEN
{
$$ = allocate(sizeof(struct param));
$$->ind = REF_IND;
$$->type = BOOL_TYPE;
$$->id = $3;
}
| REF_TOKEN INT_TOKEN IDENT_TOKEN
{
$$ = allocate(sizeof(struct param));
$$->ind = REF_IND;
$$->type = INT_TYPE;
$$->id = $3;
}
;
body
: declarations statements
{
$$= allocate(sizeof(struct body));
$$->decls = $1;
$$->statements = $2;
}
;
declarations
: decl declarations
{
$$ = allocate(sizeof(struct decls));
$$->first = $1;
$$->rest = $2;
}
| /* empty */
{ $$ = NULL; }
;
decl
: INT_TOKEN IDENT_TOKEN '[' intervals ']' ';'
{
$$ = allocate(sizeof(struct decl));
$$->lineno = ln;
$$->id = $2;
$$->array = $4;
$$->type = INT_TYPE;
}
| FLOAT_TOKEN IDENT_TOKEN '[' intervals ']' ';'
{
$$ = allocate(sizeof(struct decl));
$$->lineno = ln;
$$->id = $2;
$$->array = $4;
$$->type = FLOAT_TYPE;
}
| BOOL_TOKEN IDENT_TOKEN '[' intervals ']' ';'
{
$$ = allocate(sizeof(struct decl));
$$->lineno = ln;
$$->id = $2;
$$->array = $4;
$$->type = BOOL_TYPE;
}
| INT_TOKEN IDENT_TOKEN ';'
{
$$ = allocate(sizeof(struct decl));
$$->lineno = ln;
$$->id = $2;
$$->type = INT_TYPE;
}
| BOOL_TOKEN IDENT_TOKEN ';'
{
$$ = allocate(sizeof(struct decl));
$$->lineno = ln;
$$->id = $2;
$$->type = BOOL_TYPE;
}
| FLOAT_TOKEN IDENT_TOKEN ';'
{
$$ = allocate(sizeof(struct decl));
$$->lineno = ln;
$$->id = $2;
$$->type = FLOAT_TYPE;
}
;
intervals
: interval ',' intervals
{
$$ = allocate(sizeof(struct intervals));
$$->first = $1;
$$->rest = $3;
}
| interval
{
$$ = allocate(sizeof(struct intervals));
$$->first = $1;
$$->rest = NULL;
}
;
interval
: NUMBER_TOKEN INTERVAL_TOKEN NUMBER_TOKEN
{
$$ = allocate(sizeof(struct interval));
$$->lower = $1;
$$->upper = $3;
}
;
get_lineno
: /* empty */
{ $$ = ln; }
assign
: ASSIGN_TOKEN
{ $$ = ln; }
start_cond
: IF_TOKEN
{ $$ = ln; }
start_read
: READ_TOKEN
{ $$ = ln; }
start_while
: WHILE_TOKEN
{ $$ = ln; }
start_write
: WRITE_TOKEN
{ $$ = ln; }
statements /* non-empty list of statements */
: stmt statements
{
$$ = allocate(sizeof(struct stmts));
$$->first = $1;
$$->rest = $2;
}
| stmt
{
$$ = allocate(sizeof(struct stmts));
$$->first = $1;
$$->rest = NULL;
}
| error ';' { yyerrok; } statements
{ $$ = $4; }
;
stmt
: identifier assign expr ';' /* assignment */
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $2;
$$->kind = STMT_ASSIGN;
$$->info.assign.asg_expr = $3;
$$->info.assign.asg_ident = $1;
}
| start_read identifier ';' /* read command */
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $1;
$$->kind = STMT_READ;
$$->info.read = $2;
}
| start_write expr ';' /* write command */
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $1;
$$->kind = STMT_WRITE;
$$->info.write = $2;
}
| start_cond expr THEN_TOKEN statements FI_TOKEN
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $1;
$$->kind = STMT_COND;
$$->info.cond.cond = $2;
$$->info.cond.then_branch = $4;
$$->info.cond.else_branch = NULL;
}
| start_cond expr THEN_TOKEN statements ELSE_TOKEN statements FI_TOKEN
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $1;
$$->kind = STMT_COND;
$$->info.cond.cond = $2;
$$->info.cond.then_branch = $4;
$$->info.cond.else_branch = $6;
}
| start_while expr DO_TOKEN statements OD_TOKEN
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $1;
$$->kind = STMT_WHILE;
$$->info.loop.cond = $2;
$$->info.loop.body = $4;
}
| IDENT_TOKEN '(' exprs_list ')' ';' get_lineno
{
$$ = allocate(sizeof(struct stmt));
$$->lineno = $6;
$$->kind = STMT_FUNC;
$$->info.func = allocate(sizeof(struct func));
$$->info.func->id = $1;
$$->info.func->args = $3;
}
;
exprs_list
: expr exprs
{
$$ = allocate(sizeof(struct exprs));
$$->first = $1;
$$->rest = $2;
}
| /* empty */
{
$$ = NULL;
}
exprs
: ',' expr exprs
{
$$ = allocate(sizeof(struct exprs));
$$->first = $2;
$$->rest = $3;
}
| /* empty */
{
$$ = NULL;
}
expr
: '-' get_lineno expr %prec UNARY_MINUS
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_UNOP;
$$->unop = UNOP_MINUS;
$$->e1 = $3;
$$->e2 = NULL;
$$->lineno = $2;
}
| NOT_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_UNOP;
$$->unop = UNOP_NOT;
$$->e1 = $3;
$$->e2 = NULL;
$$->lineno = $2;
}
| expr '+' get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_ADD;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr '-' get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_SUB;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr '*' get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_MUL;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr '/' get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_DIV;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr OR_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_OR;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr AND_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_AND;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr EQ_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_EQ;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr LT_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_LT;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr NOTEQ_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_NTEQ;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr LTEQ_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_LTEQ;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr GT_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_GT;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| expr GTEQ_TOKEN get_lineno expr
{
$$ = allocate(sizeof(struct expr));
$$->kind = EXPR_BINOP;
$$->binop = BINOP_GTEQ;
$$->e1 = $1;
$$->e2 = $4;
$$->lineno = $1->lineno == $4->lineno ? $1->lineno : $3;
}
| '(' expr ')'
{ $$ = $2;}
| '(' error ')'
{ $$ = NULL; }
| FALSE_TOKEN
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_CONST;
$$->constant.val.bool_val = FALSE;
$$->constant.type = BOOL_TYPE;
$$->e1 = NULL;
$$->e2 = NULL;
}
| TRUE_TOKEN
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_CONST;
$$->constant.val.bool_val = TRUE;
$$->constant.type = BOOL_TYPE;
$$->e1 = NULL;
$$->e2 = NULL;
}
| identifier
{
$$ = $1;
}
| NUMBER_TOKEN
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_CONST;
$$->constant.val.int_val = $1;
$$->constant.type = INT_TYPE;
$$->e1 = NULL;
$$->e2 = NULL;
}
| STRING_TOKEN
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_CONST;
$$->constant.val.string = $1;
$$->constant.type = STRING_CONST;
$$->e1 = NULL;
$$->e2 = NULL;
}
| FLOAT_TOKEN
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_CONST;
$$->constant.val.float_val = $1;
$$->constant.type = FLOAT_TYPE;
$$->e1 = NULL;
$$->e2 = NULL;
}
;
identifier
: IDENT_TOKEN
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_ID;
$$->id = $1;
$$->e1 = NULL;
$$->e2 = NULL;
}
| IDENT_TOKEN '[' exprs_list ']'
{
$$ = allocate(sizeof(struct expr));
$$->lineno = ln;
$$->kind = EXPR_ARRAY;
$$->id = $1;
$$->e1 = NULL;
$$->e2 = NULL;
$$->indices = $3;
}
;
%%
/*---------------------------------------------------------------------*/
void
yyerror(const char *msg) {
fprintf(stderr, "**** Input line %d, near `%s': %s\n", ln, yytext, msg);
return;
}
void *
allocate(int size) {
return checked_malloc(size);
}
/*---------------------------------------------------------------------*/