-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.y
2912 lines (2675 loc) · 81.4 KB
/
compiler.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
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
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%{
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int yylex();
int yyerror();
extern int yylineno;
extern char* yytext;
typedef struct node
{
char *token;
struct node *left;
struct node *right;
char *type;
int visit;
char *code;
char *var;
char* begin;
char* after;
char* next_block;
int location;
}node;
node* makenode(char *token, node *left, node *right, char* type);
char* GetNodeType(node* n);
void printtree(node *tree, int tab);
typedef struct LinkedList
{
char *id;
char *group;
char *type;
int parms_num;
struct LinkedList *next;
struct LinkedList *prev;
struct LinkedList *scope;
}LinkedList;
LinkedList* BuildVarNode(char* i, char* g, char* t, LinkedList *sc);
LinkedList* BuildFuncNode(char* i, char* g, char* t, int p, LinkedList *sc);
void AddNode(LinkedList* tale, LinkedList* node);
void DeleteList(LinkedList* lst);
int IsInList(LinkedList* lst, char* i);
typedef struct Stack
{
int maxsize;
int top;
struct LinkedList **scopes;
}Stack;
Stack* NewStack();
int IsEmpty(Stack* st);
int IsFull(Stack* st);
void Push(Stack* st, LinkedList* lst);
LinkedList* Peek(Stack* st);
LinkedList* GetByIndex(Stack* st, int index);
LinkedList* Pop(Stack* st);
int IsInStack(Stack* st, char* word);
void BuildSymbolTable(node* root);
void RunOnTree(node* node, LinkedList* global);
void PrintList(LinkedList* lst);
void PrintNode(LinkedList* node);
void GetParameters(node* node, char* v_type, LinkedList* scope);
void PrintStack(Stack* st);
void GetDeclarations(LinkedList* scope, node* node, char* v_type);
void CheckAbs(node* node);
void CheckStringDec(node* node);
char* UpdateOperatorType(node* node);
char* UpdateAssOperator(node* node);
void CheckAssignments(node* node);
char* CheckBool(node* node);
void CheckFunc(LinkedList* scope, node* node, char* word);
void CheckFuncCall(node* node, LinkedList* scope);
void CheckFuncCallAss(node* node, LinkedList* scope);
int CheckFuncCallParmsNum(node* node, LinkedList* scope, int count, LinkedList* func);
void CheckMain(LinkedList* scope);
void CheckGlobal(LinkedList* scope, char* word);
void PrintTree(node* node, int tab);
void PrintFunc(node* node, int tab);
void PrintArgs(node* node, int tab);
void PrintBody(node* node, int tab);
void PrintDeclarations(node* node, int tab);
void PrintAssignment(node* node, int tab);
void PrintExp(node* node, int tab);
void PrintIf(node* node, int tab);
void PrintWhile(node* node, int tab);
void PrintFor(node* node, int tab);
void PrintDoWhile(node* node, int tab);
void PrintFuncCall(node* node, int tab);
void UpdateVisit(node* node);
char* FreshVar();
char* FreshLable();
void SetLable(node* root, node* node, int flag);
void SetNext(node* node, char* loc, int flag);
void SetParmsVar(node* node);
void SetVar(node* node);
void SetCode(node* node);
int CalaParams(char* func_name);
void Params(node* node);
char* ShortCircuit(node* node, char* truel, char* falsel);
void itoa(int n, char s[]);
void reverse(char s[]);
Stack* st;
LinkedList* scope;
LinkedList* global;
int freshvar = 0;
int freshlable = 1;
char* after;
%}
%union{
char *string;
struct node *node;
struct LinkedList *List;
struct Stack *Stack;
}
%token<string> CHAR BOOL INT REAL STRING INT_P CHAR_P REAL_P IF ELSE WHILE FOR VAR RETURN
%token<string> VOID DO AND DIV ASS EQ BIGGER BIGGER_EQ SMALLER SMALLER_EQ SUB NOT NOT_EQ OR
%token<string> ADD MUL ADDRESS TRUE FALSE REALS INTEGERS CHAR_SIGN STRING_PHRASE ENDLINE
%token<string> ID HEX EXPONENT NONE SBLOCK EBLOCK
%left OR
%left AND NOT
%left BIGGER BIGGER_EQ SMALLER SMALLER_EQ ADDRESS EQ NOT_EQ
%left ADD, SUB
%left MUL, DIV
%type<node> s exp expr value func_call func_call2 func_call_tag func_call_parms
%type<node> var_type ass ass_type var_dec var_dectag str_dec var_dectag2 str_dectag var_dec_rec
%type<node> for_loop for_looptag while_loop while_tag cond cond2 cond3
%type<node> do_loop stm stm_block func func_body func_parms func_parmstag func_parms_rec
%type<node> return stm_block_tag2 code_block block block_tag main return2 string_ass
%%
s: main { $$ = makenode("CODE", $1, NULL, ""); BuildSymbolTable($$);}
main: func main { $$ = makenode("NEW_SCOPE", $1, $2, "");}|
{$$ = makenode("", NULL, NULL, "");}
stm: while_loop {$$ = $1;}|
do_loop {$$ = $1;}|
cond {$$ = $1;}|
ass {$$ = $1;}|
var_dec {$$ = makenode("DECLARATIONS", $1, NULL, "");}|
for_loop {$$ = $1;}|
func_call {$$ = $1;}|
return {$$ = $1;}
stm_block: while_loop {$$ = makenode("SBT_WHILE",NULL,$1,"");}|
do_loop {$$ = makenode("SBT_DO",NULL,$1,"");}|
cond {$$ = makenode("SBT_COND",NULL,$1,"");}|
for_loop {$$ = makenode("SBT_FOR",NULL,$1,"");}|
func_call {$$ = makenode("SBT_FUNC",NULL,$1,"");}
stm_block_tag2: stm_block {$$ = makenode("",$1, NULL, "");}|
ass {$$ = makenode("", $1, NULL, "");}
var_type: CHAR {$$ = makenode("char", NULL, NULL, "");}|
BOOL {$$ = makenode("bool", NULL, NULL, "");}|
INT {$$ = makenode("int", NULL, NULL, "");}|
REAL {$$ = makenode("real", NULL, NULL, "");}|
INT_P {$$ = makenode("int*", NULL, NULL, "");}|
CHAR_P {$$ = makenode("char*", NULL, NULL, "");}|
REAL_P {$$ = makenode("real*", NULL, NULL, "");}
value: ID {$$ = makenode($1, NULL, NULL, "id");}|
ID '[' exp ']' {$$ = makenode($1, $3, NULL, "id");}|
INTEGERS {$$ = makenode($1, NULL, NULL, "int");}|
REALS {$$ = makenode($1, NULL, NULL, "real");}|
HEX {$$ = makenode($1, NULL, NULL, "int");}|
EXPONENT {$$ = makenode($1, NULL, NULL, "real");}|
CHAR_SIGN {$$ = makenode($1, NULL, NULL, "char");}|
TRUE {$$ = makenode($1, NULL, NULL, "bool");}|
FALSE {$$ = makenode($1, NULL, NULL, "bool");}|
STRING_PHRASE {$$ = makenode($1, NULL, NULL, "string");}|
NONE {$$ = makenode($1, NULL, NULL, "none");}
code_block: SBLOCK block return EBLOCK {$$ = makenode("{{", makenode("", $2, $3, ""), makenode("}}", NULL, NULL, ""), "BLOCK");}
block: block_tag {$$ = makenode("B", $1, NULL, "");} |
var_dec_rec block_tag {$$ = makenode("", makenode("DECLARATIONS", $1, NULL, ""), $2, "");} |
code_block block_tag {$$ = makenode( "", makenode( "{{", $1, makenode("}}", NULL, NULL, ""), "BLOCK"), $2, "");} |
var_dec_rec {$$ = makenode("", makenode("DECLARATIONS", $1, NULL, ""), NULL, "");} |
{$$ = makenode("", NULL, NULL, "");}
block_tag: stm_block_tag2 block_tag {$$ = makenode("BT", $1, $2, "");} |
stm_block_tag2 {$$ = makenode("LBT", $1, NULL, "");} |
code_block {$$ = makenode("", $1, NULL, "");} |
SBLOCK EBLOCK {$$ = makenode("{{", makenode("}}", NULL, NULL, ""), NULL, "BLOCK");}
ass: ID ASS ass_type ENDLINE {$$ = makenode($2, makenode($1,NULL,NULL,"var"), $3, "ASSIGNMENT");}|
ID '[' exp ']' ASS exp ENDLINE {$$ = makenode($5, makenode($1,$3,NULL,"var"), $6, "CHAR_ASSIGNMENT");} |
MUL ID ASS ass_type ENDLINE {$$ = makenode($3, makenode("*",makenode($2,NULL,NULL,"pointer"),NULL,""), $4, "ASSIGNMENT");}
ass_type: exp {$$ = $1;}
var_dec: VAR var_dectag {$$=makenode( "", $2, NULL, ""); }|
STRING str_dec {$$=makenode( "", $2, NULL, ""); }|
func {$$ = $1;}
var_dectag: var_type ID ASS exp var_dectag2{$$=makenode(GetNodeType($1), makenode($2, NULL, makenode($3, $4, NULL, ""), "var"), $5,"DEC_ASS");} |
var_type ID var_dectag2 {$$=makenode( GetNodeType($1), makenode( $2, NULL, NULL, "var"), $3, ""); }
str_dec: ID '[' exp ']' ASS string_ass str_dectag {$$=makenode("string",makenode($1,$3,makenode($5,makenode("",$6,NULL,""),NULL,""),"var"),$7,"DEC_STR");} |
ID '[' exp ']' str_dectag{$$=makenode("string",makenode($1,$3,NULL,"var"),$5, "");}
var_dectag2: ',' ID ASS exp var_dectag2{$$=makenode( "", makenode( $2, NULL, makenode($3, $4, NULL, ""), "var"), $5, ""); } |
',' ID var_dectag2{$$=makenode( "", makenode($2 , NULL, NULL, "var"), $3, ""); } |
ENDLINE {$$=makenode( "", NULL, NULL, ""); }
str_dectag: ','ID '[' exp ']' ASS string_ass str_dectag{$$=makenode("",makenode($2,$4,makenode($6,makenode("", $7, NULL, ""),NULL,""),"var"),$8, "");}|
',' ID '[' exp ']' str_dectag {$$=makenode( "", makenode( $2, $4, NULL, "var"), $6, ""); }|
ENDLINE {$$=makenode( "", NULL, NULL, ""); }
var_dec_rec: var_dec var_dec_rec {$$=makenode( "", $1, $2, "");} |
var_dec {$$=makenode( "", $1, NULL, "");}
string_ass: STRING_PHRASE {$$ = makenode($1,NULL,NULL,"string");} |
ID {$$ = makenode($1,NULL,NULL,"id");}
exp: expr {$$ = makenode("", $1, NULL, "EXP");}
expr: '(' expr ')' {$$ = makenode( "(", $2, makenode(")", NULL, NULL, ""), "2EXP");}|
expr ADD expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr SUB expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr MUL expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr DIV expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr BIGGER expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr BIGGER_EQ expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr SMALLER expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr SMALLER_EQ expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr EQ expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr NOT_EQ expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr OR expr {$$ = makenode($2, $1, $3, "2EXP");}|
expr AND expr {$$ = makenode($2, $1, $3, "2EXP");}|
NOT expr {$$ = makenode($1, NULL, $2, "1EXP");}|
ADDRESS expr {$$ = makenode($1, NULL, $2, "ADDRESS");}|
MUL expr {$$ = makenode($1, NULL, $2, "1EXP");}|
ADD expr {$$ = makenode($1, NULL, $2, "1EXP");}|
SUB expr {$$ = makenode($1, NULL, $2, "1EXP");}|
'|' ID '|' {$$ = makenode("|", makenode($2,makenode("|",NULL,NULL,""),NULL, ""), NULL, "ABS");}|
value {$$ = $1;}|
func_call2 {$$ = $1;}
cond: IF '(' exp ')' code_block cond2 {$$ = makenode( "IF", makenode( "", $3, $5, ""), $6,"");} |
IF '(' exp ')' stm cond3 {$$ = makenode( "IF", makenode( "", $3, $5, ""), $6,"");}
cond2: ELSE code_block {$$ = makenode("ELSE", NULL, $2, "");}|
{$$ = makenode("NO_ELSE", NULL, NULL, "");}
cond3: ELSE stm {$$ = makenode("ELSE", NULL, $2, "");}|
{$$ = makenode("NO_ELSE", NULL, NULL, "");}
func: var_type ID '(' func_parms_rec ')' SBLOCK func_body return2 EBLOCK
{$$=makenode("FUNC", makenode($2, makenode("PARAMETERS", $4, NULL, ""),
makenode("RETURN_TYPE", makenode(GetNodeType($1),NULL,NULL,""), NULL,""),""),
makenode($6, makenode("", $7, $8, ""), makenode($9, NULL, NULL, ""),""),"");} |
VOID ID '(' func_parms_rec ')' SBLOCK func_body EBLOCK
{$$=makenode("FUNC",makenode($2, makenode("PARAMETERS", $4, NULL, ""),
makenode("RETURN_TYPE",makenode($1,NULL,NULL,""), NULL,""),""),
makenode($6, makenode("", $7, NULL, ""), makenode($8,NULL,NULL,"END_FUNC"),""),"");}
func_body: func block {$$ = makenode("", $1, $2, "");}|
block {$$ = $1;}
func_parms: var_type ID func_parmstag {$$ = makenode(GetNodeType($1), makenode( $2, NULL, NULL, "parm"), $3, ""); }
func_parmstag: ',' ID func_parmstag {$$ = makenode( "", makenode($2 , NULL, NULL, "parm"), $3, ""); } |
{ $$ = makenode( "", NULL, NULL, ""); }
func_parms_rec: func_parms_rec ENDLINE func_parms { $$ = makenode( "", $1, $3, ""); } |
func_parms {$$ = makenode( "", $1, NULL, ""); } |
{$$ = makenode( "", NULL, NULL, ""); }
return: RETURN exp ENDLINE {$$ = makenode("RETURN_STM", $2, NULL, "");}|
{$$ = makenode( "", NULL, NULL, "");}
return2: RETURN exp ENDLINE {$$ = makenode("RETURN_STM", $2, NULL, "END_FUNC");}
func_call: ID ASS func_call2 ENDLINE{$$ = makenode($2, makenode($1,NULL,NULL,""), $3, "FUNC_CALL_ASS");}|
func_call2 ENDLINE{$$ = $1;}
func_call2: ID '(' func_call_parms ')' {$$ = makenode( $1, NULL, $3, "FUNC_CALL");}
func_call_parms: exp func_call_tag {$$ = makenode("", $1, $2, "FUNC_CALL_PARMS");}|
{$$ = makenode("", NULL, NULL, "");}
func_call_tag: ',' exp func_call_tag {$$ = makenode(", ", $2, $3, "FUNC_CALL_PARMS");}|
{$$ = makenode("", NULL, NULL, "");}
for_loop: FOR '(' ID ASS INTEGERS ENDLINE exp ENDLINE ID ASS exp ')' for_looptag
{$$=makenode("FOR",
makenode("for_init",
makenode($4, makenode($3, NULL, NULL,""), makenode($5,NULL,NULL,""), ""),
makenode("",makenode("for_exp",NULL,$7, ""), makenode("for_update", NULL, makenode($10, makenode($9,NULL,NULL,""),$11,""),""),""),""),$13,"");}
for_looptag: code_block {$$ = $1;}|
stm {$$ = $1;}
while_loop: WHILE '(' exp ')' while_tag {$$=makenode("WHILE", $3, $5, "");}
while_tag: code_block {$$ = $1;}|
stm {$$ = $1;}
do_loop: DO code_block WHILE '(' exp ')' ENDLINE
{$$=makenode("DO_WHILE", $2, $5, "");}
%%
int main()
{
return yyparse();
}
node* makenode(char* token, node* left, node* right, char* type)
{
node* newnode = (node*)malloc(sizeof(node));
char* newtoken = (char*)malloc(sizeof(char) * 30);
char* newtype = (char*)malloc(sizeof(char) * 30);
char* newvar = (char*)malloc(sizeof(char) * 30);
char* newcode = (char*)malloc(sizeof(char) * 1000);
char* newbegin = (char*)malloc(sizeof(char) * 30);
char* newafter = (char*)malloc(sizeof(char) * 30);
char* newnext_block = (char*)malloc(sizeof(char) * 30);
strcpy(newtoken, token);
strcpy(newtype, type);
strcpy(newbegin, " ");
strcpy(newafter, "");
newnode->visit = 0;
newnode->token = newtoken;
newnode->left = left;
newnode->right = right;
newnode->type = newtype;
newnode->code = newcode;
newnode->var = newvar;
newnode->begin = newbegin;
newnode->after = newafter;
newnode->next_block = newnext_block;
newnode->location = 0;
return newnode;
}
char* GetNodeType(node* n)
{
return n->token;
}
LinkedList* BuildVarNode(char* i, char* g, char* t, LinkedList *sc)
{
LinkedList* node = (LinkedList*)malloc(sizeof(LinkedList));
node->id = (char*)malloc(sizeof(char) * 30);
strcpy(node->id, i);
node->group = (char*)malloc(sizeof(char) * 30);
strcpy(node->group, g);
node->type = (char*)malloc(sizeof(char) * 30);
strcpy(node->type, t);
node->next = NULL;
node->prev = NULL;
node->scope = sc;
return node;
}
LinkedList* BuildFuncNode(char* i, char* g, char* t, int p, LinkedList *sc)
{
LinkedList* node = (LinkedList*)malloc(sizeof(LinkedList));
node->id = (char*)malloc(sizeof(char) * 30);
strcpy(node->id, i);
node->group = (char*)malloc(sizeof(char) * 30);
strcpy(node->group, g);
node->type = (char*)malloc(sizeof(char) * 30);
strcpy(node->type, t);
node->parms_num = p;
node->next = NULL;
node->prev = NULL;
node->scope = sc;
return node;
}
void AddNode(LinkedList* tale, LinkedList* node)
{
tale->next = node;
node->prev = tale;
}
void DeleteList(LinkedList* lst)
{
LinkedList* temp = lst;
if (temp->next != NULL)
DeleteList(temp->next);
free(temp->id);
free(temp->group);
free(temp->type);
}
int IsInList(LinkedList* lst, char* i)
{
LinkedList* temp = lst;
while(temp->next != NULL)
{
if (strcmp(temp->id, i) == 0)
{
return 1;
}
temp = temp->next;
}
return 0;
}
Stack* NewStack()
{
Stack* st = (Stack*)malloc(sizeof(Stack));
st->maxsize = 30;
st->top = -1;
st->scopes = (LinkedList**)malloc(sizeof(LinkedList*) * st->maxsize);
return st;
}
int IsEmpty(Stack* st)
{
return st->top == - 1;
}
int IsFull(Stack* st)
{
return st->top == st->maxsize - 1;
}
void Push(Stack* st, LinkedList* lst)
{
if(IsFull(st))
{
printf("Too much scopes - Stack is full\n");
exit(EXIT_FAILURE);
}
st->top = st->top + 1;
st->scopes[st->top] = lst;
}
LinkedList* Peek(Stack* st)
{
if(IsEmpty(st))
{
printf("No scopes - Stack is Empty\n");
exit(EXIT_FAILURE);
}
return st->scopes[st->top];
}
LinkedList* GetByIndex(Stack* st, int index)
{
if(IsEmpty(st))
{
printf("No scopes - Stack is Empty\n");
exit(EXIT_FAILURE);
}
if(index >= 0)
return st->scopes[index];
}
LinkedList* Pop(Stack* st)
{
if(IsEmpty(st))
{
printf("No scopes - Stack is Empty\n");
exit(EXIT_FAILURE);
}
st->top = st->top - 1;
return st->scopes[st->top + 1];
}
int IsInStack(Stack* st, char* word)
{
int index = st->top;
LinkedList* temp;
while(index != -1)
{
LinkedList* temp = GetByIndex(st, index);
if(IsInList(temp, word))
return 1;
index -= 1;
}
return 0;
}
void BuildSymbolTable(node* root)
{
st = NewStack();
global = BuildFuncNode( "global", "func", "global", 0, NULL);
Push(st, global);
RunOnTree(root, global);
CheckMain(scope);
printf("CODE COMPILED SUCCESSFULLY.\n\n");
/* printf("(CODE\n");
UpdateVisit(root);
PrintTree(root, 0);
printf(")\n"); */
UpdateVisit(root);
SetLable(root, root, 0);
SetVar(root);
UpdateVisit(root);
SetCode(root);
printf("%s\n", root->code);
}
void RunOnTree(node* node, LinkedList* scope)
{
if(strcmp(node->token, "NEW_SCOPE") == 0)
{
scope = BuildFuncNode( "global", "func", "global", 0, NULL);
}
else if (strcmp(node->token, "FUNC") == 0 && node->visit == 0)
{
LinkedList* func_node;
if(strcmp(scope->id, "global") == 0)
{
scope = BuildFuncNode(node->left->token, "func", node->left->right->left->token, 0, global);
if(strcmp(node->left->token, "main") != 0)
{
CheckFunc(scope, node, "");
CheckGlobal(scope, node->left->token);
}
}
else
{
func_node = BuildFuncNode(node->left->token, "func", node->left->right->left->token, 0, scope);
char* word = (char*)malloc(sizeof(char) * 30);
strcpy(word, node->left->token);
CheckFunc(func_node, node, word);
while(scope->next != NULL)
scope = scope->next;
AddNode(scope, func_node);
scope = func_node;
}
}
else if (strcmp(node->token, "PARAMETERS") == 0 && node->visit == 0)
{
while(scope->next != NULL)
scope = scope->next;
while(scope->prev != NULL && strcmp(scope->group, "func)") != 0)
scope = scope->prev;
GetParameters(node, "", scope);
}
else if((strcmp(node->token, "{") == 0 || strcmp(node->token, "{{") == 0) && node->visit == 0)
{
if(strcmp(node->token, "{{") == 0)
{
LinkedList* func_node;
if(strcmp(scope->id, "global") == 0)
scope = BuildFuncNode("block", "block", "block", 0, global);
else
{
func_node = BuildFuncNode("block", "block", "block", 0, scope);
while(scope->next != NULL)
scope = scope->next;
AddNode(scope, func_node);
scope = func_node;
}
}
while(scope -> next != NULL) /* save the last scope */
scope = scope->next;
while(scope->prev != NULL && strcmp(scope->group, "func") != 0 && strcmp(scope->group, "block") != 0)
scope = scope->prev;
Push(st, scope); /* push the last scope */
}
else if((strcmp(node->token, "}") == 0 || strcmp(node->token, "}}") == 0) && node->visit == 0)
{
int flag = 0;
LinkedList* temp = scope->scope;
if(strcmp(scope->scope->id, "global") != 0)
{
flag = 1;
Pop(st);
}
while(scope->next != NULL)
scope = scope->next;
while(scope->prev != NULL && strcmp(scope->group, "func") != 0 && strcmp(scope->group, "block") != 0)
scope = scope->prev;
strcpy(scope->group, "closed");
if(flag == 1)
scope = temp;
else
scope = global;
}
else if (strcmp(node->token, "DECLARATIONS") == 0 && node->visit == 0)
{
GetDeclarations(scope, node, "");
}
else if (strcmp(node->token, "IF") == 0 && node->visit == 0)
{
CheckBool(node->left->left->left);
}
else if(strcmp(node->token, "WHILE") == 0 && node->visit == 0)
{
CheckBool(node->left->left);
}
else if (strcmp(node->type, "ABS") == 0 && node->visit == 0)
{
CheckAbs(node);
}
else if (strcmp(node->type, "FUNC_CALL") == 0 && node->visit == 0)
{
CheckFuncCall(node, scope);
}
else if (strcmp(node->type, "FUNC_CALL_ASS") == 0 && node->visit == 0)
{
CheckFuncCallAss(node, scope);
}
else if (strcmp(node->token, "RETURN_STM") == 0 && node->visit == 0)
{
char* return_value = (char*)malloc(sizeof(char) * 30);
if(strcmp(node->left->left->type, "2EXP") == 0)
strcpy(return_value, UpdateAssOperator(node->left->left));
else
strcpy(return_value, node->left->left->type);
LinkedList* func = scope;
while(func->next != NULL)
{
func = func->next;
}
while(func != NULL && strcmp(func->group, "func") != 0 && strcmp(func->group, "block") != 0)
func = func->prev;
if (strcmp(return_value, "id") == 0)
{
int flag = 0;
LinkedList* search_id = func;
LinkedList* track = func;
while(track != NULL)
{
while(search_id != NULL && flag == 0)
{
if(strcmp(search_id->id, node->left->left->token) == 0 && (strcmp(search_id->group, "var") == 0 || strcmp(search_id->group, "parm") == 0))
{
strcpy(return_value, search_id->type);
flag = 1;
}
search_id = search_id->next;
}
if(track == NULL || flag == 1)
break;
search_id = track;
track = track->scope;
}
if(flag == 0)
{
printf("line: %d - the returned variable (%s) doesn't exist in this scope.\n", yylineno, node->left->left->token);
exit(0);
}
}
if(strcmp(return_value, "string") == 0)
{
printf("line: %d - the returned value cann't be string\n", yylineno);
exit(0);
}
if(strcmp(func->type, return_value) != 0)
{
printf("line: %d - the returned value (%s) doesn't match the function return type (%s)\n", yylineno, return_value, func->type);
exit(0);
}
free(return_value);
}
else if (strcmp(node->type, "ASSIGNMENT") == 0 || strcmp(node->type, "CHAR_ASSIGNMENT") == 0 && node->visit == 0)
{
CheckAssignments(node);
}
node->visit = 1;
if(node->left != NULL && node->left->visit != 1)
RunOnTree(node->left, scope);
if(node->right != NULL && node->right->visit != 1)
RunOnTree(node->right, scope);
}
void PrintList(LinkedList* lst)
{
while(lst != NULL)
{
PrintNode(lst);
if(lst->next != NULL)
{
lst = lst->next;
printf("-> ");
}
else
lst = NULL;
}
}
void PrintNode(LinkedList* node)
{
if(strcmp(node->group, "func") == 0)
printf("%s %s %s (%d) ", node->group, node->type, node->id, node->parms_num);
else
printf("%s %s %s ", node->group, node->type, node->id);
}
void PrintStack(Stack* st)
{
int index = st->top;
while(index != -1)
{
PrintList(GetByIndex(st, index));
index = index - 1;
printf("\n");
}
}
void GetParameters(node* node, char* v_type, LinkedList* scope)
{
char* var_type = (char*)malloc(sizeof(char) * 30);
strcpy(var_type, v_type);
if (strcmp(node->token, "int") == 0 || strcmp(node->token, "int*") == 0 ||
strcmp(node->token, "real") == 0 || strcmp(node->token, "real*") == 0 ||
strcmp(node->token, "char") == 0 || strcmp(node->token, "char*") == 0)
{
strcpy(var_type, node->token);
}
else if (strcmp(node->type, "parm") == 0)
{
while(scope->next != NULL)
scope = scope->next;
while(scope->prev != NULL && strcmp(scope->group, "func") != 0)
scope = scope->prev;
LinkedList* temp = scope;
LinkedList* var_lst = BuildVarNode(node->token, "parm", var_type, scope);
scope->parms_num += 1;
while(temp->next != NULL)
{
if(strcmp(temp->id, node->token) == 0 && temp->scope == scope)
{
printf("line: %d - there is one or more variables with the same name at the same scope. (%s)\n", yylineno, node->token);
exit(0);
}
temp = temp->next;
}
AddNode(temp, var_lst);
}
if(node->left != NULL && node->left->visit != 1)
GetParameters(node->left, var_type, scope);
if(node->right != NULL && node->right->visit != 1)
GetParameters(node->right, var_type, scope);
node->visit = 1;
free(var_type);
}
void GetDeclarations(LinkedList* scope, node* node, char* v_type)
{
if(strcmp(node->token, "FUNC") == 0)
RunOnTree(node, scope);
LinkedList* temp = scope;
char* var_type = (char*)malloc(sizeof(char) * 30);
strcpy(var_type, v_type);
if (strcmp(node->token, "int") == 0 || strcmp(node->token, "int*") == 0 ||
strcmp(node->token, "real") == 0 || strcmp(node->token, "real*") == 0 ||
strcmp(node->token, "char") == 0 || strcmp(node->token, "char*") == 0 ||
strcmp(node->token, "string") == 0 || strcmp(node->token, "bool") == 0)
{
strcpy(var_type, node->token);
}
if(strcmp(var_type, "string") == 0)
CheckStringDec(node);
if (strcmp(node->type, "var") == 0)
{
while(temp->next != NULL)
{
if(strcmp(temp->id, node->token) == 0)
{
printf("line: %d - the variable (%s) already exist in the same scope.\n", yylineno, node->token);
exit(0);
}
temp = temp->next;
}
if(node->right && strcmp(node->right->token, "=") == 0 && strcmp(UpdateAssOperator(node->right->left->left), var_type) != 0)
{
printf("line: %d - %s type can't assigned to %s type.\n", yylineno, UpdateAssOperator(node->right->left->left), var_type);
exit(0);
}
LinkedList* var_lst = BuildVarNode(node->token, "var", var_type, scope);
AddNode(temp, var_lst);
}
node->visit = 1;
if(node->left != NULL && node->left->visit != 1)
GetDeclarations(scope, node->left, var_type);
if(node->right != NULL && node->right->visit != 1)
GetDeclarations(scope, node->right, var_type);
free(var_type);
}
void CheckAbs(node* node)
{
char* save_id = (char*)malloc(sizeof(char) * 30);
strcpy(save_id, node->left->token);
LinkedList* temp = Peek(st);
while(temp->next != NULL)
temp = temp->next;
while(strcmp(temp->group, "func") != 0 && strcmp(temp->group, "block") != 0)
temp = temp->prev;
LinkedList* track = temp;
int flag_track = 0;
while(track != NULL && flag_track == 0)
{
while(temp != NULL)
{
if(strcmp(temp->id, save_id) == 0 && (strcmp(temp->group, "var") == 0 || strcmp(temp->group, "parm") == 0) && strcmp(temp->type, "string") == 0)
{
free(save_id);
return;
}
else if (strcmp(temp->id, save_id) == 0 && (strcmp(temp->group, "func") == 0 || strcmp(temp->group, "block") == 0))
{
free(save_id);
printf("line: %d - %s is function and not variable.\n", yylineno, temp->id);
exit(0);
}
else if (strcmp(temp->id, save_id) == 0 && strcmp(temp->type, "string") != 0 )
{
free(save_id);
printf("line: %d - %s must be string, got %s.\n", yylineno, temp->id, temp->type);
exit(0);
}
temp = temp->next;
}
if(track == NULL)
break;
else if(flag_track == 0)
{
temp = track;
track = track->scope;
}
}
printf("line: %d - %s undeclared.\n", yylineno, save_id);
free(save_id);
exit(0);
}
void CheckStringDec(node* node)
{
if (node->left && strcmp(node->left->type, "var") == 0)
{
char* index = (char*)malloc(sizeof(char) * 30);
strcpy(index, node->left->left->left->type);
LinkedList* track = Peek(st);
if(strcmp(index, "id") == 0)
{
int flag = 0;
LinkedList* search_id = Peek(st);
while(track != NULL)
{
while(search_id != NULL && flag == 0)
{
if(node->left->left->left && strcmp(search_id->id, node->left->left->left->token) == 0 && strcmp(search_id->group, "var") == 0)
{
strcpy(index, search_id->type);
flag = 1;
}
search_id = search_id->next;
}
if(track == NULL)
break;
search_id = track;
track = track->scope;
}
if(flag == 0)
{
free(index);
printf("line: %d - the variable (%s) inside the [ ] doesn't exist in this scope.\n", yylineno, node->left->left->left->token);
exit(0);
}
}
if (strcmp(index, "int") != 0 )
{ if(strcmp(node->left->left->left->type, "ABS") == 0)
CheckAbs(node->left->left->left);
else
{
printf("line: %d - the value of the index inside the [ ] must be int, got %s.\n", yylineno, index);
free(index);
exit(0);
}
}
}
}
char* UpdateOperatorType(node* node)
{
char* left, *right;
if (strcmp(node->type, "id") == 0)
{
int flag = 0;
LinkedList* temp = Peek(st);
LinkedList* scope = temp;
char* var_type = (char*)malloc(sizeof(char) * 30);
while(scope != NULL)
{
while(temp != NULL && flag == 0)
{
if(strcmp(temp->id, node->token) == 0)
{
strcpy(var_type, temp->type);
flag = 1;
}
temp = temp->next;
}
if(flag == 1 || scope == NULL)
break;
temp = scope->scope;
scope = scope->scope;
}
if(flag == 0)
{
printf("line: %d - the variable (%s) doesn't exist in this scope.\n", yylineno, node->token);
free(var_type);
exit(0);
}
return var_type;
}
else if(strcmp(node->type, "ABS") == 0)
return "int";
else if (strcmp(node->type, "int") == 0 || strcmp(node->type, "real") == 0 || strcmp(node->type, "int*") == 0 || strcmp(node->type, "real*") == 0
|| strcmp(node->type, "char") == 0 || strcmp(node->type, "char*") == 0 || strcmp(node->type, "bool") == 0 || strcmp(node->type, "string") == 0)
{
return node->type;
}
else if(strcmp(node->type, "2EXP") == 0 && strcmp(node->token, "(") == 0)
{
return UpdateOperatorType(node->left->left);
}
else if(strcmp(node->type, "2EXP") == 0)
{
left = (char*)malloc(sizeof(char) * 30);
if(node->left != NULL)
strcpy(left, UpdateOperatorType(node->left));
right = (char*)malloc(sizeof(char) * 30);
if(node->right != NULL)
strcpy(right, UpdateOperatorType(node->right));
}
else if(strcmp(node->token, "+") == 0 || strcmp(node->token, "-") == 0 || strcmp(node->token, "*") == 0 || strcmp(node->token, "/") == 0)
{
left = (char*)malloc(sizeof(char) * 30);
if(node->left != NULL)
strcpy(left, UpdateOperatorType(node->left));
right = (char*)malloc(sizeof(char) * 30);
if(node->right != NULL)
strcpy(right, UpdateOperatorType(node->right));
}
if(left && right)
{
if(strcmp(left, "int") == 0 && strcmp(right, "int") == 0)
return "int";
else if(strcmp(left, "int") == 0 && strcmp(right, "real") == 0)
return "real";
else if(strcmp(left, "real") == 0 && strcmp(right, "int") == 0)
return "real";
else if(strcmp(left, "real") == 0 && strcmp(right, "real") == 0)
return "real";
else
{
printf("line: %d - %s can't %s by %s.\n", yylineno, left, node-> token, right);
exit(0);
}
}
}
char* UpdateAssOperator(node* node)
{
char* ass_type = (char*)malloc(sizeof(char) * 30);
if (strcmp(node->token, "+") == 0 || strcmp(node->token, "-") == 0 || strcmp(node->token, "*") == 0 || strcmp(node->token, "/") == 0)
return UpdateOperatorType(node);
if (strcmp(node->token, "<") == 0 || strcmp(node->token, "<=") == 0 || strcmp(node->token, ">") == 0 || strcmp(node->token, ">=") == 0)
return "bool";
else if (strcmp(node->type, "id") == 0)
{
int flag = 0;
LinkedList* temp = Peek(st);
char* var_type = (char*)malloc(sizeof(char) * 30);
while(temp != NULL && flag == 0)
{
if(strcmp(temp->id, node->token) == 0)
{
strcpy(ass_type, temp->type);
flag = 1;
}
temp = temp->next;
}
if(flag == 0)
{