-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
1335 lines (1218 loc) · 46.1 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
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 <cstdio>
#include <cstdlib>
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include "list.h"
using namespace std;
extern FILE *yyin;
extern int yylineno;
extern char* yytext;
void yyerror(const char *s);
extern int yylex(void);
FILE *fout;
char symbol = 'L';
int num_of_tables = 0;
struct symbol_table_entry {
string id;
string type;
bool is_array = false;
vector <symbol_table_entry> *forward = NULL;
vector <symbol_table_entry> *backward = NULL;
int uid = 0;
};
struct function_name {
string id;
int line;
};
vector <string> var_decleration[2];
vector <symbol_table_entry> *start_symbol_table = new vector<symbol_table_entry>;
vector <symbol_table_entry> *current_symbol_table = start_symbol_table;
vector <string> quadruple[4];
vector <string> quadruple_temp[4];
vector <vector<symbol_table_entry> *> quad_symbol;
vector <function_name> registers;
ofstream myfile;
int indent = 0;
void temp_symbol_table_insert(string token, char *type) {
struct symbol_table_entry entry;
var_decleration[0].push_back(token);
var_decleration[1].push_back(type);
}
void print_symbol_table(vector<symbol_table_entry> *start_symbol_table) {
for(int i = 0; i < indent; i++, cout << "\t");
cout << "========" << endl;
for(int i = 0; i < start_symbol_table->size(); i++) {
for(int i = 0; i < indent; i++, cout << "\t");
cout << start_symbol_table->at(i).id << endl;
for(int i = 0; i < indent; i++, cout << "\t");
cout << start_symbol_table->at(i).type << endl;
if(start_symbol_table->at(i).forward) {
indent++;
print_symbol_table(start_symbol_table->at(i).forward);
}
}
for(int i = 0; i < indent; i++, cout << "\t");
cout << "=========" << endl;
indent--;
}
void split(const string &s, char delim, vector<string> &elems) {
stringstream ss;
ss.str(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
int num = 0;
symbol_table_entry symbol_table_lookup(string token, vector <symbol_table_entry> *current_symbol_table) {
for(int i = 0; i < current_symbol_table->size(); i++) {
if(token[0] == '#') {
token = token.substr(1);
}
if(token.back() == '*') {
token.pop_back();
}
if(token.find("_") != string::npos) {
if(current_symbol_table->at(i).id.compare(token) == 0) {
return current_symbol_table->at(i);
}
}
else if(current_symbol_table->at(i).id.compare(token + "_d" + to_string(current_symbol_table->at(0).uid)) == 0) {
return current_symbol_table->at(i);
}
}
if(current_symbol_table->at(0).backward == NULL) {
cout << token << " " << "Not defined" << endl;
exit(-1);
}
current_symbol_table = current_symbol_table->at(0).backward;
return symbol_table_lookup(token, current_symbol_table);
}
symbol_table_entry symbol_table_lookup(string token) {
return symbol_table_lookup(token, current_symbol_table);
}
vector<symbol_table_entry>* create_symbol_table() {
vector<symbol_table_entry>* symbol_table = new vector<symbol_table_entry>();
return symbol_table;
}
void symbol_table_insert(string token, string type) {
struct symbol_table_entry entry;
if(token[0] == '#') {
token = token.substr(1) + "_d" + to_string(current_symbol_table->at(0).uid);
} else {
token = token + "_d" + to_string(current_symbol_table->at(0).uid);
}
entry.id = token;
entry.type = type;
current_symbol_table->push_back(entry);
}
void symbol_table_insert(vector<string> tokens, char *type) {
for(int i = 0; i < tokens.size(); i++) {
symbol_table_insert(tokens[i], type);
}
}
char* new_temp(char *c) {
string name("t");
name += to_string(num);
num++;
char *what = (char *) malloc(sizeof(char) * 100);
strcpy(what, name.c_str());
symbol_table_insert(what, c);
strcpy(what, symbol_table_lookup(what).id.c_str());
return what;
}
void quadruple_print_symbol_table(vector <symbol_table_entry> *current_symbol_table) {
for(int i = 0 ;i < current_symbol_table->size(); i++) {
if(current_symbol_table->at(i).type[0] == 's' && current_symbol_table->at(i).forward) {
myfile << current_symbol_table->at(i).type << "{" << endl;
quadruple_print_symbol_table(current_symbol_table->at(i).forward);
myfile << "};" << endl;
continue;
} else if (current_symbol_table->at(i).type[0] == 's') {
myfile << current_symbol_table->at(i).type << " " << current_symbol_table->at(i).id
<< "=(" << current_symbol_table->at(i).type << ") malloc(sizeof(" << current_symbol_table->at(i).type<< "));" << endl;
}
else if(current_symbol_table->at(i).id[0] != '#') {
if(current_symbol_table->at(i).type == "int")
myfile << "int " << current_symbol_table->at(i).id << ";" << endl;
else if(current_symbol_table->at(i).type == "real")
myfile << "double " << current_symbol_table->at(i).id << ";" << endl;
else if(current_symbol_table->at(i).type == "char")
myfile << "char " << current_symbol_table->at(i).id << ";" << endl;
}
if(current_symbol_table->at(i).forward) {
quadruple_print_symbol_table(current_symbol_table->at(i).forward);
}
}
}
void quadruple_print() {
myfile.open("intermediatecode.c");
myfile << "#include <stdio.h>\n\n";
myfile << "#include <stdlib.h>\n\n";
myfile << "#include \"stack.h\"\n\n";
myfile << endl<<"int main(){\n\n";
myfile << "struct stack *activation_stack = stack_create();\n";
/* for print declaration of variables*/
quadruple_print_symbol_table(start_symbol_table);
for(int i = 0; i < registers.size(); i++) {
cout << registers[i].id << endl;
cout << registers[i].line << endl;
}
int line = 0;
int index = -1;
bool found = false;
for(int i = 0; i < registers.size(); i++) {
if(registers[i].id.compare("#aa00") == 0) {
line = registers[i].line;
index = i;
found = true;
}
}
if(!found) {
cout << "Error main function not defined" << endl;
exit(-1);
}
bool printed = false;
myfile << "const void *labels[] = {";
for(int i = 0; i < quadruple[0].size() - 1; i++) {
myfile << "&&L" << i << ", ";
}
myfile << "&&L" << quadruple[0].size() - 1 << "};" << endl;
if(index == 0)
myfile << "goto L" << 0 << ";" << endl;
else
myfile << "goto L" << registers[index].line << ";" << endl;
for(int i = 0; i < quadruple[0].size(); i++) {
myfile << symbol << i << " : ";
if(quadruple[2][i] == ":=")
myfile << quadruple[3][i] << " = " << quadruple[0][i] << ";"
<< endl;
else if(quadruple[2][i] == "+")
myfile << quadruple[3][i] << " = " << quadruple[0][i] << " + "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == "-")
myfile << quadruple[3][i] << " = " << quadruple[0][i] << " - "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == "*")
myfile << quadruple[3][i] << " = " << quadruple[0][i] << " * "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == "/")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " / "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == "%")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " % "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == "minus")
myfile << quadruple[3][i] << " = "<<" -1 * " <<quadruple[0][i] << ";" << endl;
else if(quadruple[2][i] == ".le")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " <= "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == ".lt")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " < "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == ".gt")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " > "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == ".ge")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " >= "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == ".eq")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " == "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == ".ne")
myfile << quadruple[3][i] << " = " <<quadruple[0][i] << " != "
<< quadruple[1][i] << ";" << endl;
else if(quadruple[2][i] == "if")
myfile << "if" << " ( " <<quadruple[0][i] << " ) "
<< quadruple[1][i] << endl;
else if(quadruple[2][i] == "push")
myfile << "stack_push(activation_stack, &(" << quadruple[0][i] << "),sizeof(" << quadruple[1][i] << "));" <<endl;
else if(quadruple[2][i] == "pop")
myfile << "stack_pop(activation_stack, &(" << quadruple[0][i] << "),sizeof(" << quadruple[1][i] << "));" <<endl;
else if(quadruple[2][i] == "goto") {
myfile << "goto *labels[" << quadruple[0][i] << "];"<<endl;
} else if(quadruple[2][i] == "comment") {
myfile << "//" << quadruple[0][i] << endl;
}
}
myfile << symbol << quadruple[0].size() << ":" << " return 0;" << endl;
myfile << endl << "}" << endl;
}
void quadruple_push(string arg1, string arg2, string op, string result) {
if(arg1[0] == '#') {
arg1 = symbol_table_lookup(arg1).id;
}
if(arg2[0] == '#') {
arg2 = symbol_table_lookup(arg2).id;
}
if(result[0] == '#') {
result = symbol_table_lookup(result).id;
}
quadruple[0].push_back(arg1);
quadruple[1].push_back(arg2);
quadruple[2].push_back(op);
quadruple[3].push_back(result);
quad_symbol.push_back(current_symbol_table);
}
void quadruple_push_temp(string arg1, string arg2, string op, string result) {
if(arg1[0] == '#') {
arg1 = symbol_table_lookup(arg1).id;
}
if(arg2[0] == '#') {
arg2 = symbol_table_lookup(arg2).id;
}
if(result[0] == '#') {
result = symbol_table_lookup(result).id;
}
quadruple_temp[0].push_back(arg1);
quadruple_temp[1].push_back(arg2);
quadruple_temp[2].push_back(op);
quadruple_temp[3].push_back(result);
}
void quadruple_push(int row, string data) {
quadruple[0][row] = data;
}
void backpatch(struct node *first, int data) {
struct node *current;
for(current = first; current != NULL; current = current->link) {
quadruple_push(current->data, to_string(data));
}
}
void backpatch(int address, int data) {
quadruple_push(address, to_string(data));
}
bool once = false;
void save_environment(vector<symbol_table_entry> *current_symbol_table) {
for(int i = 0; i < current_symbol_table->size(); i++) {
if(current_symbol_table->at(i).type[0] == 's' && current_symbol_table->at(i).forward) {
//myfile << current_symbol_table->at(i).type << "{" << endl;
//quadruple_print_symbol_table(current_symbol_table->at(i).forward);
//myfile << "};" << endl;
//continue;
} else if (current_symbol_table->at(i).type[0] == 's') {
quadruple_push(current_symbol_table->at(i).id, current_symbol_table->at(i).type, "push", "");
}
else if(current_symbol_table->at(i).id[0] != '#') {
if(current_symbol_table->at(i).type == "int")
quadruple_push(current_symbol_table->at(i).id, "int", "push", "");
else if(current_symbol_table->at(i).type == "real")
quadruple_push(current_symbol_table->at(i).id, "double", "push", "");
else if(current_symbol_table->at(i).type == "char")
quadruple_push(current_symbol_table->at(i).id, "char", "push", "");
}
if(current_symbol_table->at(i).forward) {
save_environment(current_symbol_table->at(i).forward);
}
}
}
void restore_environment(vector<symbol_table_entry> *current_symbol_table) {
for(int i = current_symbol_table->size() - 1; i >= 0; i--) {
if(current_symbol_table->at(i).forward) {
quadruple_print_symbol_table(current_symbol_table->at(i).forward);
}
if(current_symbol_table->at(i).type[0] == 's' && current_symbol_table->at(i).forward) {
//myfile << current_symbol_table->at(i).type << "{" << endl;
//quadruple_print_symbol_table(current_symbol_table->at(i).forward);
//myfile << "};" << endl;
//continue;
} else if (current_symbol_table->at(i).type[0] == 's') {
quadruple_push(current_symbol_table->at(i).id, current_symbol_table->at(i).type, "pop", "");
}
else if(current_symbol_table->at(i).id[0] != '#') {
if(current_symbol_table->at(i).type == "int")
quadruple_push(current_symbol_table->at(i).id, "int", "pop", "");
else if(current_symbol_table->at(i).type == "real")
quadruple_push(current_symbol_table->at(i).id, "double", "pop", "");
else if(current_symbol_table->at(i).type == "char")
quadruple_push(current_symbol_table->at(i).id, "char", "pop", "");
}
}
}
%}
%union {
struct {
struct node *true_list;
struct node *false_list;
struct node *next_list;
struct node *case_list;
struct node *case_address_list;
int quad;
int is_boolean;
char *place;
char *code;
char *type;
} eval;
}
%token THEN PUNC_COMMA PUNC_DOT FAKE_ID FAKE_NUMCONST FAKE_REAL
CHARCONST_SINGLEQOUTE COMMENT KW_RECORD KW_STATIC KW_INT
KW_REAL KW_BOOL KW_CHAR KW_IF KW_ELSE KW_SWITCH KW_END KW_CASE KW_DEFAULT
KW_WHILE KW_RETURN KW_SEMICOLON KW_BREAK KW_PLUS KW_MINUS KW_EQUAL KW_DIVIDE
KW_MULTIPLY KW_MODULU KW_COND_OR KW_COND_AND KW_COND_THEN KW_COND_NOT
KW_RELOP KW_COLON KW_QUESTION_MARK PAR_OP PAR_CL BR_OP BR_CL CR_OP CR_CL
Unknown KW_PLUS_PLUS KW_MINUS_MINUS KW_MINUS_EQUAL KW_PLUS_EQUAL
KW_DIVIDE_EQUAL KW_MULTIPLY_EQUAL WHITESPACE
%token <eval> NUMCONST
%token <eval> REAL
%token <eval> BOOLCONST
%token <eval> ID
%token <eval> CHARCONST
%token IF_WITHOUT_ELSE
%type <eval> program declarationList declaration recDeclaration varDeclaration
scopedVarDeclaration varDecList varDeclInitialize varDeclId scopedTypeSpecifier
typeSpecifier returnTypeSpecifier funDeclaration params paramList paramTypeList
paramIdList paramId statement compoundStmt localDeclarations statementList
expressionStmt selectionStmt caseElement defaultElement iterationStmt
returnStmt breakStmt expression simpleExpression relExpression relop
funInitiation mathlogicExpression unaryExpression unaryop factor mutable immutable call
par_cl_var par_op_var null_before_simple_expr else_var quadder switch_var
args argList constant
%left KW_COND_OR
%left KW_COND_AND
%left KW_PLUS KW_MINUS
%left KW_MULTIPLY KW_DIVIDE KW_MODULU
%left KW_COND_NOT
%nonassoc IF_WITHOUT_ELSE
%nonassoc ID_PREC
%left KW_COND_THEN
%left KW_ELSE
%%
program : declarationList
{
fprintf(fout, "Rule 1 \t\t program -> declarationList\n");
quadruple_print();
print_symbol_table(start_symbol_table);
};
declarationList : declarationList declaration
{
fprintf(fout, "Rule 2 \t\t declarationList -> declarationList declaration\n");
};
| declaration
{
fprintf(fout, "Rule 3 \t\t declarationList -> declaration\n");
};
declaration : varDeclaration
{
fprintf(fout, "Rule 4 \t\t declaration -> varDeclaration \n");
};
| funDeclaration
{
fprintf(fout, "Rule 5 \t\t declaration -> funDeclaration \n");
};
| recDeclaration
{
fprintf(fout, "Rule 6 \t\t declaration -> recDeclaration \n");
};
recDeclaration : KW_RECORD ID CR_OP localDeclarations CR_CL
{
fprintf(fout, "Rule 7 \t\t recDeclaration -> KW_RECORD ID CR_OP localDeclarations CR_CL\n");
current_symbol_table = current_symbol_table->at(0).backward;
string token = $2.place;
token = token.substr(1) + "_d" + to_string(current_symbol_table->at(0).uid);
current_symbol_table->back().id = token;
current_symbol_table->back().type = string("struct ") + token;
$$.next_list = $4.next_list;
};
varDeclaration : typeSpecifier varDecList KW_SEMICOLON
{
fprintf(fout, "Rule 8 \t\t varDeclaration -> typeSpecifier varDecList KW_SEMICOLON\n");
$$.type = $1.type;
string declaration_list($2.code);
vector<string> tokens = split(declaration_list, ',');
symbol_table_insert(tokens, $1.type);
};
| ID varDecList KW_SEMICOLON
{
fprintf(fout, "Rule 8.1 \t\t varDeclaration -> ID varDecList KW_SEMICOLON\n");
};
scopedVarDeclaration : scopedTypeSpecifier varDecList KW_SEMICOLON
{
fprintf(fout, "Rule 9 \t\t scopedVarDeclaration -> scopedTypeSpecifier varDecList KW_SEMICOLON\n");
$$.type = $1.type;
string declaration_list($2.code);
vector<string> tokens = split(declaration_list, ',');
for(int i = 0; i < tokens.size(); i++) {
vector<string> inits = split(tokens[i], ' ');
if(inits.size() == 1) {
symbol_table_insert(tokens[i], $1.type);
} else if(inits.size() == 2) {
symbol_table_insert(inits[0], $1.type);
quadruple_push(inits[1], "", ":=", inits[0]);
}
}
};
varDecList : varDecList PUNC_COMMA varDeclInitialize
{
fprintf(fout, "Rule 10 \t\t varDecList -> varDecList PUNC_COMMA varDeclInitialize\n");
char *temp = new char[100];
strcpy(temp, $1.code);
$$.code = strcat(strcat(temp, ","), $3.code);
};
| varDeclInitialize
{
fprintf(fout, "Rule 11 \t\t varDecList -> varDeclInitialize\n");
$$.code = $1.code;
};
varDeclInitialize : varDeclId
{
fprintf(fout, "Rule 12 \t\t varDeclInitialize -> varDeclId\n");
$$.code = $1.code;
$$.type = "unknown";
};
| varDeclId KW_COLON simpleExpression
{
fprintf(fout, "Rule 13 \t\t varDeclInitialize -> varDeclId KW_COLON simpleExpression\n");
char *temp = new char[100];
strcpy(temp, $1.code);
$$.code = strcat(strcat(temp, " "), $3.place);
$$.type = $3.type;
};
varDeclId : ID
{
fprintf(fout, "Rule 14 \t\t varDeclId -> ID\n");
$$.code = $1.place;
};
| ID BR_OP NUMCONST BR_CL
{
fprintf(fout, "Rule 15 \t\t varDeclId -> ID BR_OP NUMCONST BR_CL\n");
$$.code = $1.place;
};
scopedTypeSpecifier : KW_STATIC typeSpecifier
{
fprintf(fout, "Rule 16 \t\t scopedTypeSpecifier -> KW_STATIC typeSpecifier\n");
$$.type = $2.type;
};
| typeSpecifier
{
fprintf(fout, "Rule 17 \t\t scopedTypeSpecifier -> typeSpecifier\n");
$$.type = $1.type;
};
| KW_STATIC ID
{
fprintf(fout, "Rule 17.1 \t\t scopedTypeSpecifier -> KW_STATIC ID\n");
};
typeSpecifier : returnTypeSpecifier
{
fprintf(fout, "Rule 18 \t\t typeSpecifier -> returnTypeSpecifier\n");
$$.type = $1.type;
}
| KW_RECORD ID
{
fprintf(fout, "Rule 19 \t\t typeSpecifier -> KW_RECORD returnTypeSpecifier\n");
char *what = (char *) malloc(sizeof(char) * 100);
strcpy(what, ("struct " + symbol_table_lookup($2.place).id + "*").c_str());
$$.type = what;
};
returnTypeSpecifier : KW_INT
{
fprintf(fout, "Rule 20 \t\t returnTypeSpecifier -> KW_INT\n");
$$.type = "int";
};
| KW_REAL
{
fprintf(fout, "Rule 21 \t\t returnTypeSpecifier -> KW_REAL\n");
$$.type = "real";
};
| KW_BOOL
{
fprintf(fout, "Rule 22 \t\t returnTypeSpecifier -> KW_BOOL\n");
$$.type = "int";
};
| KW_CHAR
{
fprintf(fout, "Rule 23 \t\t returnTypeSpecifier -> KW_CHAR\n");
$$.type = "char";
};
funDeclaration : funInitiation statement
{
current_symbol_table->back();
};
funInitiation : typeSpecifier ID par_op_var params par_cl_var
{
fprintf(fout, "Rule 24 \t\t funDeclaration -> typeSpecifier ID par_op_var params par_cl_var statement\n");
struct symbol_table_entry entry;
entry.type = $1.type;
entry.id = $2.place;
current_symbol_table->push_back(entry);
$$.type = $1.type;
$$.place = $2.place;
struct function_name function;
function.id = $2.place;
function.line = quadruple[0].size();
registers.push_back(function);
};
| ID par_op_var params par_cl_var statement
{
fprintf(fout, "Rule 25 \t\t funDeclaration -> ID par_op_var params par_cl_var statement\n");
};
| ID ID par_op_var params par_cl_var statement
{
fprintf(fout, "Rule 25.1 \t\t funDeclaration -> ID par_op_var params par_cl_var statement\n");
};
params : paramList
{
fprintf(fout, "Rule 26 \t\t params -> paramList\n");
};
|
{
fprintf(fout, "Rule 27 \t\t params -> empty \n");
};
paramList : paramList KW_SEMICOLON paramTypeList
{
fprintf(fout, "Rule 28 \t\t paramList -> paramList KW_SEMICOLON paramTypeList\n");
};
| paramTypeList { fprintf(fout, "Rule 29 \t\t paramList -> paramTypeList\n");
};
paramTypeList : typeSpecifier paramIdList
{
fprintf(fout, "Rule 30 \t\t paramTypeList -> typeSpecifier paramIdList\n");
$$.type = $1.type;
string declaration_list($2.code);
vector<string> tokens = split(declaration_list, ',');
for(int i = 0; i < tokens.size(); i++) {
vector<string> inits = split(tokens[i], ' ');
if(inits.size() == 1) {
temp_symbol_table_insert(tokens[i], $1.type);
} else if(inits.size() == 2) {
temp_symbol_table_insert(inits[0], $1.type);
quadruple_push(inits[1], "", ":=", inits[0]);
}
}
};
paramIdList : paramIdList PUNC_COMMA paramId
{
fprintf(fout, "Rule 31 \t\t paramIdList -> paramIdList PUNC_COMMA paramId\n");
char *temp = new char[100];
strcpy(temp, $1.code);
$$.code = strcat(strcat(temp, ","), $3.code);
};
| paramId
{
fprintf(fout, "Rule 32 \t\t paramIdList -> paramId\n");
$$.code = $1.code;
};
paramId : ID
{
fprintf(fout, "Rule 33 \t\t paramId -> ID\n");
$$.code = $1.place;
};
| ID BR_OP BR_CL
{
fprintf(fout, "Rule 34 \t\t paramId -> ID BR_OP BR_CL\n");
};
statement : expressionStmt
{
$$.next_list = $1.next_list;
fprintf(fout, "Rule 35 \t\t statement -> expressionStmt\n");
};
| compoundStmt
{
$$.next_list = $1.next_list;
fprintf(fout, "Rule 36 \t\t statement -> compoundStmt\n");
};
| selectionStmt
{
$$.next_list = $1.next_list;
fprintf(fout, "Rule 37 \t\t statement -> selectionStmt\n");
};
| iterationStmt
{
$$.next_list = $1.next_list;
fprintf(fout, "Rule 38 \t\t statement -> iterationStmt\n");
};
| returnStmt
{
$$.next_list = $1.next_list;
fprintf(fout, "Rule 39 \t\t statement -> returnStmt\n");
};
| breakStmt
{
$$.next_list = $1.next_list;
fprintf(fout, "Rule 40 \t\t statement -> breakStmt\n");
};
compoundStmt : CR_OP localDeclarations statementList CR_CL
{
fprintf(fout, "Rule 41 \t\t compoundStmt -> CR_OP localDeclarations statementList CR_CL\n");
current_symbol_table = current_symbol_table->at(0).backward;
$$.next_list = $3.next_list;
};
localDeclarations : localDeclarations scopedVarDeclaration
{
fprintf(fout, "Rule 42 \t\t localDeclarations -> localDeclarations scopedVarDeclaration\n");
};
|
{
fprintf(fout, "Rule 43 \t\t localDeclarations -> empty\n");
num_of_tables++;
struct symbol_table_entry entry;
entry.id = "statement_scope";
entry.type = "link";
entry.forward = create_symbol_table();
current_symbol_table->push_back(entry);
entry.uid = num_of_tables;
entry.forward = NULL;
entry.id = to_string(current_symbol_table->size());
entry.type = "link";
entry.backward = current_symbol_table;
current_symbol_table = current_symbol_table->back().forward;
current_symbol_table->push_back(entry);
for(int i = 0; i < var_decleration[0].size(); i++)
{
symbol_table_insert(var_decleration[0][i], var_decleration[1][i]);
quadruple_push(var_decleration[0][i], var_decleration[1][i], "pop", "");
}
var_decleration[0].clear();
var_decleration[1].clear();
};
statementList : statementList statement
{
fprintf(fout, "Rule 44 \t\t statementList -> statementList statement\n");
$$.next_list = $2.next_list;
};
|
{
fprintf(fout, "Rule 45 \t\t statementList -> empty\n");
};
expressionStmt : expression KW_SEMICOLON
{
fprintf(fout, "Rule 46 \t\t expressionStmt -> expression KW_SEMICOLON\n");
};
| KW_SEMICOLON
{
fprintf(fout, "Rule 47 \t\t expressionStmt -> empty\n");
};
selectionStmt : KW_IF par_op_var simpleExpression par_cl_var statement %prec IF_WITHOUT_ELSE
{
fprintf(fout, "Rule 48 \t\t selectionStmt -> KW_IF par_op_var simpleExpression par_cl_var statement\n");
backpatch($3.false_list,quadruple[0].size());
backpatch($3.true_list,$4.quad);
$$.next_list = $5.next_list;
};
| KW_IF par_op_var simpleExpression par_cl_var statement else_var statement
{
fprintf(fout, "Rule 49 \t\t selectionStmt -> KW_IF par_op_var simpleExpression par_cl_var statement KW_ELSE statement\n");
backpatch($3.true_list,$4.quad);
backpatch($3.false_list, $6.quad+1);
backpatch($6.quad,quadruple[0].size());
$$.next_list = $7.next_list;
};
| switch_var par_op_var simpleExpression par_cl_var caseElement defaultElement KW_END
{
fprintf(fout, "Rule 50 \t\t selectionStmt -> switch_var par_op_var simpleExpression par_cl_var caseElement defaultElement KW_END declaration\n");
struct node *current;
struct node *currentAddress;
backpatch($1.next_list, quadruple[0].size());
for(current = $5.case_list, currentAddress=$5.case_address_list;
current != NULL; current = current->link, currentAddress=currentAddress->link) {
char *what = (char *) malloc (sizeof(char)*100);
strcpy(what,to_string(current->data).c_str());
char* temp = (char *) malloc (sizeof(char)*100);
strcpy(temp , $3.place);
strcat(temp , "==");
strcat(temp , what);
quadruple_push(temp,"","if","");
char *cast = (char *)malloc(sizeof(char)*100);
strcpy(cast,to_string(currentAddress->data).c_str());
quadruple_push(cast,"","goto","");
}
char *cast2 = (char *)malloc(sizeof(char)*100);
strcpy(cast2,to_string($6.quad).c_str());
quadruple_push(cast2,"","goto","");
backpatch($6.next_list,quadruple[0].size());
backpatch($5.next_list,quadruple[0].size());
};
caseElement : KW_CASE NUMCONST KW_COLON quadder statement KW_SEMICOLON
{
fprintf(fout, "Rule 51 \t\t caseElement -> KW_CASE NUMCONST KW_COLON statement KW_SEMICOLON\n");
$$.case_list = create_node(atoi($2.place));
$$.case_address_list = create_node($4.quad);
$$.next_list = $5.next_list;
};
| caseElement KW_CASE NUMCONST KW_COLON quadder statement KW_SEMICOLON
{
fprintf(fout, "Rule 52 \t\t caseElement -> KW_CASE NUMCONST KW_COLON statement KW_SEMICOLON\n");
$$.case_list = merge_lists($1.case_list, (create_node(atoi($3.place))));
$$.case_address_list = merge_lists($1.case_address_list, (create_node($5.quad)));
$$.next_list = merge_lists($1.next_list,$6.next_list);
};
defaultElement : KW_DEFAULT KW_COLON quadder statement KW_SEMICOLON
{
fprintf(fout, "Rule 53 \t\t defaultElement -> KW_DEFAULT KW_COLON statement KW_SEMICOLON\n");
$$.quad = $3.quad;
quadruple_push("","","goto","");
$$.next_list = create_node(quadruple[0].size()-1);
};
|
{
$$.quad = quadruple[0].size();
fprintf(fout, "Rule 54 \t\t defaultElement -> empty\n");
quadruple_push("","","goto","");
$$.next_list = create_node(quadruple[0].size()-1);
};
iterationStmt : KW_WHILE par_op_var simpleExpression par_cl_var statement
{
quadruple_push("","","goto","");
backpatch($3.false_list,quadruple[0].size());
backpatch($3.true_list,$4.quad);
backpatch(create_node(quadruple[0].size()-1),$2.quad);
backpatch($5.next_list,quadruple[0].size());
fprintf(fout, "Rule 55 \t\t iterationStmt -> KW_WHILE par_op_var simpleExpression par_cl_var statement\n");
};
returnStmt : KW_RETURN KW_SEMICOLON
{
fprintf(fout, "Rule 56 \t\t returnStmt -> KW_RETURN KW_SEMICOLON\n");
};
| KW_RETURN expression KW_SEMICOLON
{
fprintf(fout, "Rule 57 \t\t returnStmt -> KW_RETURN expression KW_SEMICOLON\n");
quadruple_push("Beginning of return statement", "", "comment", "");
string temp = new_temp("int");
quadruple_push(temp, "int", "pop", "");
quadruple_push($2.place, $2.type, "push", "");
quadruple_push(temp, "", "goto", "");
};
breakStmt : KW_BREAK KW_SEMICOLON
{
fprintf(fout, "Rule 58 \t\t breakStmt -> KW_BREAK KW_SEMICOLON\n");
$$.next_list = create_node(quadruple[0].size());
quadruple_push("","","goto","");
};
expression : mutable KW_EQUAL expression
{
fprintf(fout, "Rule 59 \t\t expression -> mutable KW_EQUAL expression\n");
$$.type = $1.type;
quadruple_push($3.place, "", ":=", $1.place);
};
| mutable KW_PLUS_EQUAL expression
{
fprintf(fout, "Rule 60 \t\t expression -> mutable KW_PLUS_EQUAL expression\n");
$$.type = $1.type;
$$.place = new_temp($1.type);
quadruple_push($1.place, $3.place, "+", $1.place);
quadruple_push($1.place, "", ":=", $$.place);
};
| mutable KW_MINUS_EQUAL expression
{
fprintf(fout, "Rule 61 \t\t expression -> mutable KW_MINUS_EQUAL expression\n");
$$.type = $1.type;
$$.place = new_temp($1.type);
quadruple_push($1.place, $3.place, "-", $1.place);
quadruple_push($1.place, "", ":=", $$.place);
};
| mutable KW_MULTIPLY_EQUAL expression
{
fprintf(fout, "Rule 62 \t\t expression -> mutable KW_MULTIPLY_EQUAL expression\n");
$$.type = $1.type;
$$.place = new_temp($1.type);
quadruple_push($1.place, $3.place, "*", $1.place);
quadruple_push($1.place, "", ":=", $$.place);
};
| mutable KW_DIVIDE_EQUAL expression
{
fprintf(fout, "Rule 63 \t\t expression -> mutable KW_DIVIDE_EQUAL expression\n");
$$.type = $1.type;
$$.place = new_temp($1.type);
quadruple_push($1.place, $3.place, "/", $1.place);
quadruple_push($1.place, "", ":=", $$.place);
};
| mutable KW_PLUS_PLUS
{
fprintf(fout, "Rule 64 \t\t expression -> mutable KW_PLUS_PLUS\n");
$$.place = new_temp($1.type);
$$.type = $1.type;
quadruple_push($1.place, "1", "+", $1.place);
quadruple_push($1.place, "", ":=", $$.place);
};
| mutable KW_MINUS_MINUS
{
fprintf(fout, "Rule 65 \t\t expression -> mutable KW_MINUS_MINUS\n");
$$.place = new_temp($1.type);
$$.type = $1.type;
quadruple_push($1.place, "1", "-", $1.place);
quadruple_push($1.place, "", ":=", $$.place);
};
| simpleExpression
{
fprintf(fout, "Rule 66 \t\t expression -> simpleExpression\n");
//backpatch($1.true_list,quadruple[0].size());
};
simpleExpression : simpleExpression KW_COND_OR quadder simpleExpression
{
fprintf(fout, "Rule 67 \t\t simpleExpression -> simpleExpression KW_COND_OR simpleExpression\n");
// Generate the gotos under the main gotos
$$.place = new_temp("int");
backpatch($1.true_list,quadruple[0].size());
quadruple_push("1","",":=",$$.place);
quadruple_push(to_string($3.quad),"","goto","");
backpatch($1.false_list,quadruple[0].size());
quadruple_push("0","",":=",$$.place);
quadruple_push(to_string($3.quad),"","goto","");
$$.true_list = $4.true_list;
backpatch($4.false_list, quadruple[0].size());
quadruple_push(strcat((strcat($$.place,"==")),"0"),"","if","");
$$.false_list = create_node(quadruple[0].size());
quadruple_push("","","goto","");
$$.true_list = merge_lists($4.true_list,create_node(quadruple[0].size()));
quadruple_push("","","goto","");
};
| simpleExpression KW_COND_AND quadder simpleExpression
{
fprintf(fout, "Rule 68 \t\t simpleExpression -> simpleExpression KW_COND_AND simpleExpression\n");
$$.place = new_temp("int");
backpatch($1.true_list,quadruple[0].size());
quadruple_push("1","",":=",$$.place);
quadruple_push(to_string($3.quad),"","goto","");
backpatch($1.false_list,quadruple[0].size());
quadruple_push("0","",":=",$$.place);
quadruple_push(to_string($3.quad),"","goto","");
$$.false_list = $4.false_list;
backpatch($4.true_list, quadruple[0].size());
quadruple_push(strcat((strcat($$.place,"==")),"0"),"","if","");
$$.false_list = merge_lists($4.false_list,create_node(quadruple[0].size()));
quadruple_push("","","goto","");
$$.true_list = create_node(quadruple[0].size());
quadruple_push("","","goto","");
};
| simpleExpression KW_COND_OR KW_ELSE null_before_simple_expr simpleExpression
{
fprintf(fout, "Rule 69 \t\t simpleExpression -> simpleExpression KW_COND_OR KW_ELSE simpleExpression\n");
backpatch($1.false_list,$4.quad);
$$.true_list = merge_lists($1.true_list,$5.true_list);
$$.false_list = $5.false_list;
};
| simpleExpression KW_COND_AND KW_COND_THEN null_before_simple_expr simpleExpression
{
fprintf(fout, "Rule 70 \t\t simpleExpression -> simpleExpression KW_COND_AND KW_COND_THEN simpleExpression\n");
backpatch($1.true_list,$4.quad);
$$.true_list = $5.true_list;
$$.false_list = merge_lists($1.false_list,$5.false_list);
};
| KW_COND_NOT simpleExpression
{
fprintf(fout, "Rule 71 \t\t simpleExpression -> KW_COND_NOT simpleExpression\n");
$$.true_list = $2.false_list;
$$.false_list = $2.true_list;
};
| relExpression
{
fprintf(fout, "Rule 72 \t\t simpleExpression -> relExpression\n");
};
null_before_simple_expr : {
fprintf(fout, "Rule 67.1 \t\t simpleExpression -> empty\n");