-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompiler.y
More file actions
941 lines (869 loc) · 22.6 KB
/
compiler.y
File metadata and controls
941 lines (869 loc) · 22.6 KB
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
/* Definitions */
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "AST.h"
#include "traverseTree.h"
/* global variables which can be used in other .c .h */
struct Program *head;
FILE *fp1; // FILE pointer to write AST to file
FILE *fp2; // FILE pointer to write symbol tree to file
/* yyerror function */
void yyerror(char* text) {
fprintf(stderr, "%s\n", text);
}
%}
/* definitions of tokens and types passed by FLEX */
%union {
/* pointers for the AST struct nodes */
struct Program *Program_ptr;
struct Class *Class_ptr;
struct Member *Member_ptr;
struct VarDecl *VarDecl_ptr;
struct MethodDecl *MethodDecl_ptr;
struct MethodDef *MethodDef_ptr;
struct ClassMethodDef *ClassMethodDef_ptr;
struct MainFunc *MainFunc_ptr;
struct Param *Param_ptr;
struct Ident *Ident_ptr;
struct Type *Type_ptr;
struct CompoundStmt *CompoundStmt_ptr;
struct Stmt *Stmt_ptr;
struct ExprStmt *ExprStmt_ptr;
struct AssignStmt *AssignStmt_ptr;
struct RetStmt *RetStmt_ptr;
struct WhileStmt *WhileStmt_ptr;
struct DoStmt *DoStmt_ptr;
struct ForStmt *ForStmt_ptr;
struct IfStmt *IfStmt_ptr;
struct Expr *Expr_ptr;
struct OperExpr *OperExpr_ptr;
struct RefExpr *RefExpr_ptr;
struct RefVarExpr *RefVarExpr_ptr;
struct RefCallExpr *RefCallExpr_ptr;
struct IdentExpr *IdentExpr_ptr;
struct CallExpr *CallExpr_ptr;
struct Arg *Arg_ptr;
/* int, float to char* */
int intnum;
float floatnum;
char *id;
}
/* token definitions */
%token <intnum>INTNUM
%token <floatnum>FLOATNUM
%token <id>ID
%token INT FLOAT MAIN PRIVATE PUBLIC
%token IF ELSE FOR WHILE DO RETURN
%token DOUBLE_QUO SINGLE_QUO AND OR PLUS MINUS MULT DIV GT LT LE GE EQ NE
/* type(non-terminal) definitions */
%type <Program_ptr> PROGRAM
%type <Class_ptr> CLASS CLASSLIST
%type <Member_ptr> MEMBER
%type <VarDecl_ptr> VARDECL VARDECLLIST
%type <MethodDecl_ptr> METHODDECL METHODDECLLIST
%type <MethodDef_ptr> METHODDEF METHODDEFLIST
%type <ClassMethodDef_ptr> CLASSMETHODDEF CLASSMETHODDEFLIST
%type <MainFunc_ptr> MAINFUNC
%type <Param_ptr> PARAM PARAMLIST
%type <Ident_ptr> IDENT
%type <Type_ptr> TYPE
%type <CompoundStmt_ptr> COMPOUNDSTMT
%type <Stmt_ptr> STMT STMTLIST
%type <ExprStmt_ptr> EXPRSTMT
%type <AssignStmt_ptr> ASSIGNSTMT
%type <RetStmt_ptr> RETSTMT
%type <WhileStmt_ptr> WHILESTMT
%type <DoStmt_ptr> DOSTMT
%type <ForStmt_ptr> FORSTMT
%type <IfStmt_ptr> IFSTMT
%type <Expr_ptr> EXPR
%type <OperExpr_ptr> OPEREXPR
%type <RefExpr_ptr> REFEXPR
%type <RefVarExpr_ptr> REFVAREXPR
%type <RefCallExpr_ptr> REFCALLEXPR
%type <IdentExpr_ptr> IDENTEXPR
%type <CallExpr_ptr> CALLEXPR
%type <Arg_ptr> ARG ARGLIST
/* priority definitions from top to down, the priority becomes higher */
%right '='
%left EQ NE
%left LE GE GT LT
%left PLUS MINUS
%left MULT DIV
%right UNARY
%left '(' ')'
/*no associativity symbols */
%nonassoc IF_THEN
%nonassoc ELSE
/* the start token */
%start PROGRAM
/* C++ Program Grammar Rules */
%%
// Program := (ClassList)? (ClassMethod)? MainFunc
PROGRAM: CLASSLIST CLASSMETHODDEFLIST MAINFUNC {
struct Program *p = (struct Program*)malloc(sizeof(struct Program)); // create a new node and allocate the space
p->_class = $1;
p->classMethodDef = $2;
p->mainFunc = $3;
head = p;
$$ = p;
}
| CLASSLIST MAINFUNC {
struct Program *p = (struct Program*)malloc(sizeof(struct Program));
p->_class = $1;
p->classMethodDef = NULL;
p->mainFunc = $2;
head = p;
$$ = p;
}
| CLASSMETHODDEFLIST MAINFUNC {
struct Program *p = (struct Program*)malloc(sizeof(struct Program));
p->_class = NULL;
p->classMethodDef = $1;
p->mainFunc = $2;
head = p;
$$ = p;
}
| MAINFUNC {
struct Program *p = (struct Program*)malloc(sizeof(struct Program));
p->_class = NULL;
p->classMethodDef = NULL;
p->mainFunc = $1;
head = p;
$$ = p;
}
;
// ClassList := (Class)+
// Class := class id { (private : Member)? (public : Member)? }
CLASSLIST: CLASS {
struct Class *c;
c = $1;
c->prev = NULL;
$$ = c;
}
| CLASSLIST CLASS {
struct Class *c;
c = $2;
c->prev = $1;
$$ = c;
}
;
CLASS: TYPE ID '{' PRIVATE ':' MEMBER PUBLIC ':' MEMBER '}' ';' {
struct Class *c = (struct Class*)malloc(sizeof(struct Class));
c->id = $2;
c->priMember = $6;
c->pubMember = $9;
$$ = c;
}
| TYPE ID '{' PRIVATE ':' MEMBER '}' ';' {
struct Class *c = (struct Class*)malloc(sizeof(struct Class));
c->id = $2;
c->priMember = $6;
c->pubMember = NULL;
$$ = c;
}
| TYPE ID '{' PUBLIC ':' MEMBER '}' ';' {
struct Class *c = (struct Class*)malloc(sizeof(struct Class));
c->id = $2;
c->priMember = NULL;
c->pubMember = $6;
$$ = c;
}
| TYPE ID '{' '}' ';' {
struct Class *c = (struct Class*)malloc(sizeof(struct Class));
c->id = $2;
c->priMember = NULL;
c->pubMember = NULL;
$$ = c;
}
;
// Member := (VarDeclList)? (MethodDeclList)? (MethodDefList)?
MEMBER: VARDECLLIST METHODDECLLIST METHODDEFLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = $1;
m->methodDecl = $2;
m->methodDef = $3;
$$ = m;
}
| VARDECLLIST METHODDECLLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = $1;
m->methodDecl = $2;
m->methodDef = NULL;
$$ = m;
}
| VARDECLLIST METHODDEFLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = $1;
m->methodDecl = NULL;
m->methodDef = $2;
$$ = m;
}
| METHODDECLLIST METHODDEFLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = NULL;
m->methodDecl = $1;
m->methodDef = $2;
$$ = m;
}
| VARDECLLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = $1;
m->methodDecl = NULL;
m->methodDef = NULL;
$$ = m;
}
| METHODDECLLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = NULL;
m->methodDecl = $1;
m->methodDef = NULL;
$$ = m;
}
| METHODDEFLIST {
struct Member *m = (struct Member*)malloc(sizeof(struct Member));
m->varDecl = NULL;
m->methodDecl = NULL;
m->methodDef = $1;
$$ = m;
}
;
// VarDeclList := (VarDecl)+
// VarDecl := Type Ident (= (intnum|floatnum))?;
VARDECLLIST: VARDECL {
struct VarDecl *v;
v = $1;
v->prev = NULL;
$$ = v;
}
| VARDECLLIST VARDECL {
struct VarDecl *v;
v = $2;
v->prev = $1;
$$ = v;
}
;
VARDECL: TYPE IDENT '=' INTNUM ';' {
struct VarDecl *v = (struct VarDecl*)malloc(sizeof(struct VarDecl));
v->type = $1;
v->ident = $2;
v->assignType = eAsInt;
v->assigner.intnum = $4;
$$ = v;
}
| TYPE IDENT '=' FLOATNUM ';' {
struct VarDecl *v = (struct VarDecl*)malloc(sizeof(struct VarDecl));
v->type = $1;
v->ident = $2;
v->assignType = eAsFloat;
v->assigner.floatnum = $4;
$$ = v;
}
| TYPE IDENT ';' {
struct VarDecl *v = (struct VarDecl*)malloc(sizeof(struct VarDecl));
v->type = $1;
v->ident = $2;
v->assignType = eNon;
$$ = v;
}
;
// MethodDeclList := (MethodDecl)+
// MethodDecl := Type id ( (ParamList)? ) ;
METHODDECLLIST: METHODDECL {
struct MethodDecl *m;
m = $1;
m->prev = NULL;
$$ = m;
}
| METHODDECLLIST METHODDECL {
struct MethodDecl *m;
m = $2;
m->prev = $1;
$$ = m;
}
;
METHODDECL: TYPE ID '(' PARAMLIST ')' ';' {
struct MethodDecl *m = (struct MethodDecl*)malloc(sizeof(struct MethodDecl));
m->type = $1;
m->id = $2;
m->param = $4;
$$ = m;
}
| TYPE ID '(' ')' ';' {
struct MethodDecl *m = (struct MethodDecl*)malloc(sizeof(struct MethodDecl));
m->type = $1;
m->id = $2;
m->param = NULL;
$$ = m;
}
;
// MethodDefList := (MethodDef)+
// MethodDef := Type id ( (ParamList)? ) CompoundStmt
METHODDEFLIST: METHODDEF {
struct MethodDef *m;
m = $1;
m->prev = NULL;
$$ = m;
}
| METHODDEFLIST METHODDEF {
struct MethodDef *m;
m = $2;
m->prev = $1;
$$ = m;
}
;
METHODDEF: TYPE ID '(' PARAMLIST ')' COMPOUNDSTMT {
struct MethodDef *m = (struct MethodDef*)malloc(sizeof(struct MethodDef));
m->type = $1;
m->id = $2;
m->param = $4;
m->compoundStmt = $6;
$$ = m;
}
| TYPE ID '(' ')' COMPOUNDSTMT {
struct MethodDef *m = (struct MethodDef*)malloc(sizeof(struct MethodDef));
m->type = $1;
m->id = $2;
m->param = NULL;
m->compoundStmt = $5;
$$ = m;
}
;
// ClassMethodList := (ClassMethodDef)+
// ClassMethodDef := Type id :: id ( (ParamList)? ) CompoundStmt
CLASSMETHODDEFLIST: CLASSMETHODDEF {
struct ClassMethodDef *c;
c = $1;
c->prev = NULL;
$$ = c;
}
| CLASSMETHODDEFLIST CLASSMETHODDEF {
struct ClassMethodDef *c;
c = $2;
c->prev = $1;
$$ = c;
}
;
CLASSMETHODDEF: TYPE ID ':' ':' ID '(' PARAMLIST ')' COMPOUNDSTMT {
struct ClassMethodDef *c = (struct ClassMethodDef*)malloc(sizeof(struct ClassMethodDef));
c->type = $1;
c->className = $2;
c->methodName = $5;
c->param = $7;
c->compoundStmt = $9;
$$ = c;
}
| TYPE ID ':' ':' ID '(' ')' COMPOUNDSTMT {
struct ClassMethodDef *c = (struct ClassMethodDef*)malloc(sizeof(struct ClassMethodDef));
c->type = $1;
c->className = $2;
c->methodName = $5;
c->param = NULL;
c->compoundStmt = $8;
$$ = c;
}
;
// MainFunc := int main ( ) CompoundStmt
MAINFUNC: INT MAIN '(' ')' COMPOUNDSTMT {
struct MainFunc *m = (struct MainFunc*)malloc(sizeof(struct MainFunc));
m->compoundStmt = $5;
$$ = m;
}
;
// ParamList := Param (, Param)*
// Param := Type Ident
PARAMLIST: PARAM {
struct Param* p;
p = $1;
p->prev = NULL;
$$ = p;
}
| PARAMLIST ',' PARAM {
struct Param* p;
p = $3;
p->prev = $1;
$$ = p;
}
;
PARAM: TYPE IDENT {
struct Param *p = (struct Param*)malloc(sizeof(struct Param));
p->type = $1;
p->ident = $2;
p->prev = NULL;
$$ = p;
}
;
// Ident := id
// | id [ intnum ]
IDENT: ID {
struct Ident *i = (struct Ident*)malloc(sizeof(struct Ident));
i->id = $1;
i->len = 0;
$$ = i;
}
| ID '[' INTNUM ']' {
struct Ident *i = (struct Ident*)malloc(sizeof(struct Ident));
i->id = $1;
i->len = $3;
$$ = i;
}
;
// Type := int
// | float
// | id
TYPE: INT {
struct Type *t = (struct Type*)malloc(sizeof(struct Type));
t->id = NULL;
t->e = eInt;
$$ = t;
}
| FLOAT {
struct Type *t = (struct Type*)malloc(sizeof(struct Type));
t->id = NULL;
t->e = eFloat;
$$ = t;
}
| ID {
struct Type *t = (struct Type*)malloc(sizeof(struct Type));
t->id = $1;
t->e = eClass;
$$ = t;
}
// CompoundStmt := { (VarDeclList)? (StmtList)? }
COMPOUNDSTMT: '{' '}' {
struct CompoundStmt *comp = (struct CompoundStmt*)malloc(sizeof(struct CompoundStmt));
comp->varDecl = NULL;
comp->stmt = NULL;
$$ = comp;
}
| '{' STMTLIST '}' {
struct CompoundStmt *comp = (struct CompoundStmt*)malloc(sizeof(struct CompoundStmt));
comp->varDecl = NULL;
comp->stmt = $2;
$$ = comp;
}
| '{' VARDECLLIST STMTLIST '}' {
struct CompoundStmt *comp = (struct CompoundStmt*)malloc(sizeof(struct CompoundStmt));
comp->varDecl = $2;
comp->stmt = $3;
$$ = comp;
}
| '{' VARDECLLIST '}' {
struct CompoundStmt *comp = (struct CompoundStmt*)malloc(sizeof(struct CompoundStmt));
comp->varDecl = $2;
comp->stmt = NULL;
$$ = comp;
}
;
// StmtList := (Stmt)+
// Stmt := ExprStmt
// | AssignStmt
// | RetStmt
// | WhileStmt
// | DoStmt
// | ForStmt
// | IfStmt
// | CompoundStmt
// | ;
STMTLIST: STMT {
struct Stmt *stmt;
stmt = $1;
stmt->prev = NULL;
$$ = stmt;
}
| STMTLIST STMT {
struct Stmt *stmt;
stmt = $2;
stmt->prev = $1;
$$ = stmt;
}
;
STMT: EXPRSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eExpr;
stmt->type.exprStmt = $1;
$$ = stmt;
}
| ASSIGNSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eAssign;
stmt->type.assignStmt = $1;
$$ = stmt;
}
| RETSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eRet;
stmt->type.retStmt = $1;
$$ = stmt;
}
| WHILESTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eWhile;
stmt->type.whileStmt = $1;
$$ = stmt;
}
| DOSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eDo;
stmt->type.doStmt = $1;
$$ = stmt;
}
| FORSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eFor;
stmt->type.forStmt = $1;
$$ = stmt;
}
| IFSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eIf;
stmt->type.ifStmt = $1;
$$ = stmt;
}
| COMPOUNDSTMT {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eCompound;
stmt->type.compoundStmt = $1;
$$ = stmt;
}
| ';' {
struct Stmt *stmt = (struct Stmt*)malloc(sizeof(struct Stmt));
stmt->e = eSemi;
$$ = stmt;
}
;
// ExprStmt := Expr ;
EXPRSTMT: EXPR ';' {
struct ExprStmt *e = (struct ExprStmt*)malloc(sizeof(struct ExprStmt));
e->expr = $1;
$$ = e;
}
;
// AssignStmt := RefVarExpr = Expr ;
ASSIGNSTMT: REFVAREXPR '=' EXPR ';' {
struct AssignStmt *a = (struct AssignStmt*)malloc(sizeof(struct AssignStmt));
a->refVarExpr = $1;
a->expr = $3;
$$ = a;
}
;
// RetStmt := return (Expr)? ;
RETSTMT: RETURN ';' {
struct RetStmt *r = (struct RetStmt*)malloc(sizeof(struct RetStmt));
r->expr = NULL;
$$ = r;
}
| RETURN EXPR ';' {
struct RetStmt *r = (struct RetStmt*)malloc(sizeof(struct RetStmt));
r->expr = $2;
$$ = r;
}
;
// WhileStmt := while ( Expr ) Stmt
WHILESTMT: WHILE '(' EXPR ')' STMT {
struct WhileStmt *w = (struct WhileStmt*)malloc(sizeof(struct WhileStmt));
w->cond = $3;
w->body = $5;
$$ = w;
}
;
// DoStmt := do Stmt while ( Expr ) ;
DOSTMT: DO STMT WHILE '(' EXPR ')' ';' {
struct DoStmt *d = (struct DoStmt*)malloc(sizeof(struct DoStmt));
d->cond = $5;
d->body = $2;
$$ = d;
}
;
// ForStmt := for ( Expr ; Expr ; Expr ) Stmt
FORSTMT: FOR '(' EXPR ';' EXPR ';' EXPR ')' STMT {
struct ForStmt *f = (struct ForStmt*)malloc(sizeof(struct ForStmt));
f->init = $3;
f->cond = $5;
f->incr = $7;
f->body = $9;
$$ = f;
}
;
/* priority and conflict by using %prec */
// IfStmt := if ( Expr ) Stmt (else Stmt)?
IFSTMT: IF '(' EXPR ')' STMT %prec IF_THEN {
struct IfStmt *i = (struct IfStmt*)malloc(sizeof(struct IfStmt));
i->cond = $3;
i->ifBody = $5;
i->elseBody = NULL;
$$ = i;
}
| IF '(' EXPR ')' STMT ELSE STMT {
struct IfStmt *i = (struct IfStmt*)malloc(sizeof(struct IfStmt));
i->cond = $3;
i->ifBody = $5;
i->elseBody = $7;
$$ = i;
}
;
// Expr := OperExpr
// | RefExpr
// | intnum
// | floatnum
EXPR: OPEREXPR {
struct Expr *expr = (struct Expr*)malloc(sizeof(struct Expr));
expr->e = eOper;
expr->type.operExpr = $1;
$$ = expr;
}
| REFEXPR {
struct Expr *expr = (struct Expr*)malloc(sizeof(struct Expr));
expr->e = eRef;
expr->type.refExpr = $1;
$$ = expr;
}
| INTNUM {
struct Expr *expr = (struct Expr*)malloc(sizeof(struct Expr));
expr->e = eIntnum;
expr->type.intnum = $1;
$$ = expr;
}
| FLOATNUM {
struct Expr *expr = (struct Expr*)malloc(sizeof(struct Expr));
expr->e = eFloatnum;
expr->type.floatnum = $1;
$$ = expr;
}
;
// OperExpr := unop Expr
// | Expr addiop Expr
// | Expr multop Expr
// | Expr relaop Expr
// | Expr eqltop Expr
// | ( Expr )
OPEREXPR: MINUS EXPR %prec UNARY {
struct UnOp *unop = (struct UnOp*)malloc(sizeof(struct UnOp));
unop->e = eNegative;
unop->expr = $2;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eUn;
operexpr->type.un = unop;
$$ = operexpr;
}
| EXPR PLUS EXPR {
struct AddiOp *addiop = (struct AddiOp*)malloc(sizeof(struct AddiOp));
addiop->lhs = $1;
addiop->rhs = $3;
addiop->e = ePlus;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eAddi;
operexpr->type.addi = addiop;
$$ = operexpr;
}
| EXPR MINUS EXPR {
struct AddiOp *addiop = (struct AddiOp*)malloc(sizeof(struct AddiOp));
addiop->lhs = $1;
addiop->rhs = $3;
addiop->e = eMinus;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eAddi;
operexpr->type.addi = addiop;
$$ = operexpr;
}
| EXPR MULT EXPR {
struct MultOp *multop = (struct MultOp*)malloc(sizeof(struct MultOp));
multop->lhs = $1;
multop->rhs = $3;
multop->e = eMul;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eMult;
operexpr->type.mult = multop;
$$ = operexpr;
}
| EXPR DIV EXPR {
struct MultOp *multop = (struct MultOp*)malloc(sizeof(struct MultOp));
multop->lhs = $1;
multop->rhs = $3;
multop->e = eDiv;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eMult;
operexpr->type.mult = multop;
$$ = operexpr;
}
| EXPR LT EXPR {
struct RelaOp *relaop = (struct RelaOp*)malloc(sizeof(struct RelaOp));
relaop->lhs = $1;
relaop->rhs = $3;
relaop->e = eLT;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eRela;
operexpr->type.rela = relaop;
$$ = operexpr;
}
| EXPR GT EXPR {
struct RelaOp *relaop = (struct RelaOp*)malloc(sizeof(struct RelaOp));
relaop->lhs = $1;
relaop->rhs = $3;
relaop->e = eGT;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eRela;
operexpr->type.rela = relaop;
$$ = operexpr;
}
| EXPR LE EXPR {
struct RelaOp *relaop = (struct RelaOp*)malloc(sizeof(struct RelaOp));
relaop->lhs = $1;
relaop->rhs = $3;
relaop->e = eLE;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eRela;
operexpr->type.rela = relaop;
$$ = operexpr;
}
| EXPR GE EXPR {
struct RelaOp *relaop = (struct RelaOp*)malloc(sizeof(struct RelaOp));
relaop->lhs = $1;
relaop->rhs = $3;
relaop->e = eGE;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eRela;
operexpr->type.rela = relaop;
$$ = operexpr;
}
| EXPR EQ EXPR {
struct EqltOp *eqltop = (struct EqltOp*)malloc(sizeof(struct EqltOp));;
eqltop->lhs = $1;
eqltop->rhs = $3;
eqltop->e = eEQ;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eEqlt;
operexpr->type.eqlt = eqltop;
$$ = operexpr;
}
| EXPR NE EXPR {
struct EqltOp *eqltop = (struct EqltOp*)malloc(sizeof(struct EqltOp));;
eqltop->lhs = $1;
eqltop->rhs = $3;
eqltop->e = eNE;
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eEqlt;
operexpr->type.eqlt = eqltop;
$$ = operexpr;
}
| '(' EXPR ')' {
struct OperExpr *operexpr = (struct OperExpr*)malloc(sizeof(struct OperExpr));
operexpr->e = eBracket;
operexpr->type.bracket = $2;
$$ = operexpr;
}
;
// RefExpr := RefVarExpr
// | RefCallExpr
REFEXPR: REFVAREXPR {
struct RefExpr *r = (struct RefExpr*)malloc(sizeof(struct RefExpr));
r->e = eVar;
r->type.refVarExpr = $1;
$$ = r;
}
| REFCALLEXPR {
struct RefExpr *r = (struct RefExpr*)malloc(sizeof(struct RefExpr));
r->e = eCall;
r->type.refCallExpr = $1;
$$ = r;
}
;
// RefVarExpr := (RefExpr .)? IdentExpr
REFVAREXPR: REFEXPR '.' IDENTEXPR {
struct RefVarExpr *r = (struct RefVarExpr*)malloc(sizeof(struct RefVarExpr));
r->refExpr = $1;
r->identExpr = $3;
$$ = r;
}
| IDENTEXPR {
struct RefVarExpr *r = (struct RefVarExpr*)malloc(sizeof(struct RefVarExpr));
r->refExpr = NULL;
r->identExpr = $1;
$$ = r;
}
;
// RefCallExpr := (RefExpr .)? CallExpr
REFCALLEXPR: REFEXPR '.' CALLEXPR {
struct RefCallExpr *r = (struct RefCallExpr*)malloc(sizeof(struct RefCallExpr));
r->refExpr = $1;
r->callExpr = $3;
$$ = r;
}
| CALLEXPR {
struct RefCallExpr *r = (struct RefCallExpr*)malloc(sizeof(struct RefCallExpr));
r->refExpr = NULL;
r->callExpr = $1;
$$ = r;
}
;
// IdentExpr := id [ Expr ]
// | id
IDENTEXPR: ID '[' EXPR ']' {
struct IdentExpr* identexpr = (struct IdentExpr*)malloc(sizeof(struct IdentExpr));
identexpr->id = $1;
identexpr->expr = $3;
$$ = identexpr;
}
| ID {
struct IdentExpr* identexpr = (struct IdentExpr*)malloc(sizeof(struct IdentExpr));
identexpr->id = $1;
identexpr->expr = NULL;
$$ = identexpr;
}
;
// CallExpr := id ( (ArgList)? )
CALLEXPR: ID '(' ARGLIST ')' {
struct CallExpr *callexpr = (struct CallExpr*)malloc(sizeof(struct CallExpr));
callexpr->id = $1;
callexpr->arg = $3;
$$ = callexpr;
}
| ID '(' ')' {
struct CallExpr *callexpr = (struct CallExpr*)malloc(sizeof(struct CallExpr));
callexpr->id = $1;
callexpr->arg = NULL;
$$ = callexpr;
}
;
// ArgList := Expr (, Expr)*
ARGLIST: ARG {
struct Arg* arg;
arg = $1;
arg->prev = NULL;
$$ = arg;
}
| ARGLIST ',' ARG {
struct Arg* arg;
arg = $3;
arg->prev = $1;
$$ = arg;
}
;
ARG: EXPR {
struct Arg *arg = (struct Arg*)malloc(sizeof(struct Arg));
arg->expr = $1;
arg->prev = NULL;
$$ = arg;
}
;
%%
int main(int argc, char* argv[]) {
fp1 = fopen("ASTtree.txt","w");
fp2 = fopen("symbolTable.txt","w");
/* initialize the start and end scope */
startScope = endScope = NULL;
/* generate the AST tree in the process */
yyparse();
/* the start scope should be the GLOBAL means CLASS scope type */
startScope = createNewScope(CLASS_scope, NULL);
/* same at the beginning */
endScope = startScope;
/* traverse the AST generated by the yyparse() before */
visitProgram(head);
fprintf(fp1, "\n");
fclose(fp1);
fclose(fp2);
return 0;
}