-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
1160 lines (1051 loc) · 34.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 <cstring>
#include <list>
#include "llist.h"
#include <csignal>
using namespace std;
// stuff from flex that bison needs to know about:
// extern "C" int yylex();
// extern "C" int yyparse();
extern FILE *yyin;
extern int yylineno;
extern char* yytext;
extern char* lexID;
extern int lexNum;
extern double lexReal;
extern char* lexChar;
extern bool lexBool;
extern int yylex(void);
// char symbolTable[1000][50];
int wrong=0;
int nextquad = 0;
int num = 0; // temporary variable numbers
void yyerror(const char *s);
FILE *fout;
enum {
TYPE_UNKNOWN = -1,
TYPE_INT = 0,
TYPE_REAL = 1,
TYPE_CHAR = 2,
TYPE_BOOL = 3
};
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);
}
if (elems.empty()) elems.push_back(s);
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
/*** Symbol Table ***/
struct symbolTableEntry {
string id;
int type;
bool is_array = false;
vector <symbolTableEntry> *forward = NULL;
vector <symbolTableEntry> *backward = NULL;
int uid = 0;
};
int cursor = 0;
vector<symbolTableEntry*> symbolTable;
int symbolTableInsert(string _id, int _type, bool _isArray) {
symbolTableEntry* ste = new symbolTableEntry;
if (_id[0] == '#') _id = _id.substr(1);
printf("%s %d\n", _id.c_str(), _id.size());
ste->id = _id;
ste->type = _type;
ste->is_array = _isArray;
ste->uid = cursor;
symbolTable.push_back(ste);
return cursor++;
}
symbolTableEntry* symbolTableLookup(const string& _id) {
for (auto& ste : symbolTable) {
if (ste->id == _id) {
return ste;
}
}
return nullptr;
}
string printSymbolTable() {
std::string s;
for(auto& ste : symbolTable) {
std::string arr = (ste->is_array) ? "*" : "";
switch(ste->type) {
case TYPE_INT:
s += "\tint " + arr + ste->id + ";\n";
break;
case TYPE_BOOL:
s += "\tchar " + arr + ste->id + ";\n";
break;
case TYPE_REAL:
s += "\tdouble " + arr + ste->id + ";\n";
break;
case TYPE_CHAR:
s += "\tchar " + arr + ste->id + ";\n";
break;
}
}
return s;
}
char* newTemp(int _type, bool _isArray) {
string* name = new string{"temp"};
*name += std::to_string(num++);
symbolTableInsert(*name, _type, _isArray);
return const_cast<char*>(name->c_str());
}
llist* makelist(int _data) {
node* n = create_node(_data);
llist* l = new llist;
l->head = n;
l->tail = nullptr;
return l;
}
// END_OF_SYMBOL_TABLE
/*** Quadruples ***/
// // Quadruple
/* ______________________________________________________________________________
* | |
* | Quadruples |
* |______________________________________________________________________________|
* | Statement | Operation | Arg0 | Arg1 | Result |
* |____________________________________|___________|___________|_______|_________|
* | goto L | goto | | | L |
* | if BOOLEAN then goto L | check | BOOLEAN | | L |
* | E = E1 < E2 | < | E1 | E2 | E |
* | E = E1 <= E2 | <= | E1 | E2 | E |
* | E = E1 > E2 | > | E1 | E2 | E |
* | E = E1 >= E2 | >= | E1 | E2 | E |
* | E = E1 == E2 | == | E1 | E2 | E |
* | E = E1 + E2 | + | E1 | E2 | E |
* | E = E1 - E2 | - | E1 | E2 | E |
* | E = E1 * E2 | * | E1 | E2 | E |
* | E = E1 / E2 | / | E1 | E2 | E |
* | E = E1 % E2 | % | E1 | E2 | E |
* | E = -E1 | usub | E1 | | E |
* | E = *E1 | asterisk | E1 | | E |
* | E = ?E1 | quest | E1 | | E |
* | E = E1 | = | E1 | | E |
* | E = (TYPE) E1 | cast | E1 | TYPE | E |
* | TYPE E | init | | TYPE | E |
* | printf("E = E.val") | iprint | | | int |
* | printf("E = E.val") | rprint | | | real |
* | printf("E = E.val") | cprint | | | char |
* | printf("E = E.val") | bprint | | | boolean |
* | printf("E[PLACE] = E[INDEX].val") | aiprint | PLACE | INDEX | int |
* | printf("E[PLACE] = E[INDEX].val") | arprint | PLACE | INDEX | real |
* | printf("E[PLACE] = E[INDEX].val") | acprint | PLACE | INDEX | char |
* | printf("E[PLACE] = E[INDEX].val") | abprint | PLACE | INDEX | boolean |
* | E[INDEX] = E1 | []= | E1 | INDEX | E |
* | E = E1[INDEX] | =[] | E1 | INDEX | E |
* |____________________________________|___________|___________|_______|_________|
*/
struct Quadruple
{
Quadruple(string _op, string _arg1, string _arg2, string _res) :
operation(_op),
arg1(_arg1),
arg2(_arg2),
result(_res)
{
}
string operation;
string arg1;
string arg2;
string result;
};
vector <Quadruple*> quadruples;
void emit(string _op, string _arg1, string _arg2, string _result) {
nextquad++;
quadruples.push_back(new Quadruple(_op, _arg1, _arg2, _result));
}
void backpatch(struct llist* _head, int _label) {
struct node* current;
for (current = _head->head; current != nullptr; current = current->next) {
quadruples[current->data]->result = std::to_string(_label);
}
}
void fillQuad(int i, int j, string _data) {
switch(j) {
case 0:
quadruples[i]->operation = _data;
break;
case 1:
quadruples[i]->arg1 = _data;
break;
case 2:
quadruples[i]->arg2 = _data;
break;
case 3:
quadruples[i]->result = _data;
break;
default:
printf("Wrong index%d\n", j);
}
}
string printQuadruple()
{
std::string s;
for(int i = 0;i < quadruples.size();i++)
{
s += "L" + std::to_string(i) + ": ";
if (quadruples[i] -> operation == "+") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + " + " + quadruples[i] -> arg2 + ";\n";
} else if(quadruples[i] -> operation == "-") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + " - " + quadruples[i] -> arg2 + ";\n";
} else if(quadruples[i] -> operation == "*") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + " * " + quadruples[i] -> arg2 + ";\n";
} else if(quadruples[i] -> operation == "/") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + " / " + quadruples[i] -> arg2 + ";\n";
} else if(quadruples[i] -> operation == "%") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + " % " + quadruples[i] -> arg2 + ";\n";
} else if(quadruples[i] -> operation == "ifgoto") {
s += std::string("if") + " ( " + quadruples[i] -> arg1 +" ) " + "goto " + "L" + quadruples[i] -> result + ";\n";
} else if(quadruples[i] -> operation == "goto") {
s += std::string("goto ") + "L" + quadruples[i] -> result + ";\n";
} else if(quadruples[i] -> operation == "=") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + ";\n";
} else if(quadruples[i] -> operation == "<") {
s += quadruples[i] -> result + " = (" + quadruples[i] -> arg1 + "<" + quadruples[i] -> arg2 + ") ;\n";
} else if(quadruples[i] -> operation == "<=") {
s += quadruples[i] -> result + " = (" + quadruples[i] -> arg1 + "<=" + quadruples[i] -> arg2 + ") ;\n";
} else if(quadruples[i] -> operation == "==") {
s += quadruples[i] -> result + " = (" + quadruples[i] -> arg1 + "==" + quadruples[i] -> arg2 + ") ;\n";
} else if(quadruples[i] -> operation == ">") {
s += quadruples[i] -> result + " = (" + quadruples[i] -> arg1 + ">" + quadruples[i] -> arg2 + ") ;\n";
} else if(quadruples[i] -> operation == ">=") {
s += quadruples[i] -> result + " = (" + quadruples[i] -> arg1 + ">=" + quadruples[i] -> arg2 + ") ;\n";
} else if(quadruples[i] -> operation == "usub") {
s += quadruples[i] -> result + " = " + "-1 * " + quadruples[i] -> arg1 + ";\n";
} else if(quadruples[i] -> operation == "asterisk") {
s += quadruples[i] -> result + " = " + "sizeof(" + quadruples[i] -> arg1 + ")/sizeof(" + quadruples[i] -> arg1 + "[0]) ;\n";
} else if(quadruples[i] -> operation == "quest") {
s += quadruples[i] -> result + " = " + "ud(0, " + quadruples[i] -> arg1 + ") ;\n";
} else if(quadruples[i] -> operation == "[]=") {
s += quadruples[i] -> result + "["+ quadruples[i] -> arg2 + "]" + " = " + quadruples[i] -> arg1 + ";\n";
} else if(quadruples[i] -> operation == "=[]") {
s += quadruples[i] -> result + " = " + quadruples[i] -> arg1 + "["+ quadruples[i] -> arg2 + "]" + ";\n";
} else {
s+= quadruples[i]->operation + ";\n";
}
}
return s;
}
// END_OF_QUADRUPLES
void generateInterCode() {
FILE* interCode;
interCode = fopen("generatedCode.c", "w");
if (fout == NULL) {
printf("Error opening file!\n");
return;
}
fprintf(interCode, "#include <stdio.h>\n");
fprintf(interCode, "#include <time.h>\n");
fprintf(interCode, "#include <stdlib.h>\n\n");
fprintf(interCode, "int ud(int rL, int rH) {\n\tdouble mR = rand()/(1.0 + RAND_MAX);\n\tint r = rH - rL + 1;\n\tint mRS = (mR * r) + rL;\n\treturn mRS;\n}\n\n");
fprintf(interCode, "int main() {\n");
fprintf(interCode, "/* SYMBOL TABLE */\n");
fprintf(interCode, "%s\n", printSymbolTable().c_str());
fprintf(interCode, "/* Quadruples */\n");
fprintf(interCode, "%s\n", printQuadruple().c_str());
std::string nextLabel = std::string("L") + std::to_string(quadruples.size()) + ": ;";
fprintf(interCode, "%s\n", nextLabel.c_str());
fprintf(interCode, "return 0;\n}\n");
fclose(interCode);
}
%}
%union {
struct {
int type;
char* place;
char* code;
struct llist* truelist;
struct llist* falselist;
struct llist* nextlist;
int quad;
} E;
}
// define the "terminal symbol" token types I'm going to use (in CAPS
// by convention), and associate each with a field of the union:
%token PROGRAM_KW STRUCT_KW CONST_KW INT_KW REAL_KW CHAR_KW BOOL_KW IF_KW THEN_KW ELSE_KW SWITCH_KW DEFAULT_KW WHEN_KW RETURN_KW BREAK_KW OR_KW AND_KW XOR_KW ALSO_KW NOT_KW GT_KW LT_KW LE_KW EQ_KW GE_KW PLUS_KW MINUS_KW MULT_KW DIV_KW MOD_KW QUEST_MARK ASSIGN_PLUS ASSIGN_MINUS ASSIGN_MULT ASSIGN_DIV INC_KW DEC_KW CASE_KW END_KW
%token <E> INT_NUM
%token <E> REAL_NUM
%token <E> BOOL_CONSTANT_TRUE
%token <E> BOOL_CONSTANT_FALSE
%token <E> IDENTIFIER
%token <E> CHAR_CONSTANT
%type <E> idetifier_type
%type <E> int_type
%type <E> real_type
%type <E> bool_type
%type <E> char_type
%type <E> program declist dec structdec localdec limitedvardec limitedvartype type vardec varsdecs primiryvardec varIDdec funcdec arg args argstype argsID argID sentence compSent sentences exprSent selectSent caseelement defaultelement repeatSent returnSent argsVector constant argVector call breakSent unvar expr simpleexp variable relativeexp relativeop arthlogicexpr unaryexpr unaryop opera
%type <E> M
%right THEN_KW
%right ELSE_KW
%left XOR_KW OR_KW
%right '='
%left AND_KW ALSO_KW
%left EQ_KW LT_KW GT_KW LE_KW GE_KW
%left PLUS_KW MINUS_KW
%left MULT_KW DIV_KW MOD_KW
%right NOT_KW
%%
program : PROGRAM_KW idetifier_type declist
{
printf("Mahi\n");
generateInterCode();
fprintf(fout, "Rule 1 \t\t program -> PROGRAM_KW idetifier_type declist \n") ;
};
declist : declist dec
{
fprintf(fout, "Rule 2.1 \t\t declist -> declist dec \n") ;
};
| dec
{
fprintf(fout, "Rule 2.2 \t\t declist -> dec \n") ;
};
dec : structdec
{
fprintf(fout, "%d: Rule 3.1 \t\t dec -> structdec \n", yylineno) ;
};
| vardec
{
fprintf(fout, "%d: Rule 3.2 \t\t dec -> vardec \n", yylineno) ;
};
| funcdec
{
fprintf(fout, "%d: Rule 3.3 \t\t dec -> funcdec \n", yylineno) ;
};
structdec : STRUCT_KW idetifier_type '{' localdec '}'
{
fprintf(fout, "%d: Rule 4 \t\t structdec -> STRUCT_KW idetifier_type { localdec } \n", yylineno);
};
localdec : localdec limitedvardec
{
fprintf(fout, "%d: Rule 5.1 \t\t localdec -> localdec limitedvardec \n", yylineno);
};
| /* empty */
{
fprintf(fout, "%d: Rule 5.2 \t\t localdec -> e \n", yylineno);
};
limitedvardec : limitedvartype varsdecs ';'
{
fprintf(fout, "%d: Rule 6 \t\t limitedvardec -> limitedvartype varsdecs ; \n", yylineno);
vector<string> tokens = split($2.code, ',');
for(auto& token : tokens) {
symbolTableInsert(token, $1.type, (token[0] == '#'));
}
};
limitedvartype : CONST_KW type
{
$$.type = $2.type;
fprintf(fout, "%d: Rule 7.1 \t\t limitedvartype -> CONST_KW type \n", yylineno);
};
| type
{
$$.type = $1.type;
fprintf(fout, "%d: Rule 7.2 \t\t limitedvartype -> type \n", yylineno);
};
type : INT_KW
{
$$.type = TYPE_INT;
fprintf(fout, "%d: Rule 8.1 \t\t type -> INT_KW \n", yylineno);
};
| REAL_KW
{
$$.type = TYPE_REAL;
fprintf(fout, "%d: Rule 8.2 \t\t type : REAL_KW \n", yylineno);
};
| CHAR_KW
{
$$.type = TYPE_CHAR;
fprintf(fout, "%d: Rule 8.3 \t\t type : CHAR_KW \n", yylineno);
};
| BOOL_KW
{
$$.type = TYPE_BOOL;
fprintf(fout, "%d: Rule 8.4 \t\t type : BOOL_KW \n", yylineno);
};
vardec : type varsdecs ';'
{
vector<string> tokens = split($2.code, ',');
for(auto& token : tokens) {
symbolTableInsert(token, $1.type, (token[0] == '#'));
}
fprintf(fout, "%d: Rule 9 \t\t vardec -> type varsdecs ;\n", yylineno);
};
varsdecs : primiryvardec
{
fprintf(fout, "%d: Rule 10.1 \t\t varsdecs -> primiryvardec \n", yylineno);
$$.code = new char[100];
strcpy($$.code,$1.place);
};
| varsdecs ',' primiryvardec
{
fprintf(fout, "%d: Rule 10.2 \t\t varsdecs -> varsdecs , primiryvardec \n", yylineno);
char *tt = new char[100];
strcpy(tt, $1.code);
$$.code = strcat(strcat(tt, ","), $3.place);
};
primiryvardec : varIDdec
{
fprintf(fout, "%d: Rule 11.1 \t\t primiryvardec -> varIDdec \n", yylineno);
$$.place = $1.place;
};
| varIDdec '=' simpleexp
{
fprintf(fout, "%d: Rule 11.2 \t\t primiryvardec -> varIDdec = simpleexp \n", yylineno);
$$.place = $1.place;
emit("=", $3.place, "", $1.place);
};
varIDdec : idetifier_type
{
$$.place = $1.place;
fprintf(fout, "%d: Rule 12.1 \t\t varIDdec -> idetifier_type \n", yylineno);
};
| idetifier_type '[' int_type ']'
{
fprintf(fout, "%d: Rule 12.2 \t\t varIDdec -> idetifier_type [ int_type ] \n", yylineno);
$$.place = new char[100];
strcpy($$.place,"#");
strcat($$.place,$1.place);
printf("%s\n", $$.place);
};
funcdec : type idetifier_type '(' arg ')' sentence
{
fprintf(fout, "%d: Rule 13.1 \t\t funcdec -> type idetifier_type ( arg ) sentence \n", yylineno);
};
| idetifier_type '(' arg ')' sentence
{
fprintf(fout, "%d: Rule 13.2 \t\t funcdec -> idetifier_type ( arg ) sentence \n", yylineno);
};
arg : args
{
fprintf(fout, "%d: Rule 14.1 \t\t arg -> args \n", yylineno);
};
| /* empty */
{
fprintf(fout, "%d: Rule 14.2 \t\t arg : e \n", yylineno);
};
args : args ';' argstype
{
fprintf(fout, "%d: Rule 15.1 \t\t args -> args ; argstype \n", yylineno);
};
| argstype
{
fprintf(fout, "%d: Rule 15.2 \t\t args -> argstype \n", yylineno);
};
argstype : type argsID
{
fprintf(fout, "%d: Rule 16 \t\t argstype -> type argsID \n", yylineno);
};
argsID : argsID ',' argID
{
fprintf(fout, "Rule 17.1 \t\t argsID -> argsID , argID \n");
};
| argID
{
fprintf(fout, "Rule 17.2 \t\t argsID -> argID \n");
};
argID : idetifier_type
{
fprintf(fout, "Rule 18.1 \t\t argID -> idetifier_type \n");
};
| idetifier_type '[' ']'
{
fprintf(fout, "Rule 18.2 \t\t argID : idetifier_type [ ] \n");
};
sentence : compSent
{
fprintf(fout, "Rule 19.1 \t\t sentence -> compSent \n");
};
| exprSent
{
fprintf(fout, "Rule 19.2 \t\t sentence -> exprSent \n");
};
| selectSent
{
fprintf(fout, "Rule 19.3 \t\t sentence -> selectSent \n");
};
| repeatSent
{
fprintf(fout, "Rule 19.4 \t\t sentence -> repeatSent \n");
};
| returnSent
{
fprintf(fout, "Rule 19.5 \t\t sentence -> returnSent \n");
};
| breakSent
{
fprintf(fout, "Rule 19.6 \t\t sentence -> breakSent \n");
};
compSent : '{' localdec sentences '}'
{
fprintf(fout, "Rule 20 \t\t compSent -> { localdec sentences } \n");
};
sentences : sentences sentence
{
fprintf(fout, "Rule 21.1 \t\t sentences -> sentences sentence \n");
};
| /* empty */
{
fprintf(fout, "Rule 21.2 \t\t sentences -> e \n");
};
exprSent : expr ';'
{
fprintf(fout, "Rule 22.1 \t\t exprSent -> expr ; \n");
};
| ';'
{
fprintf(fout, "Rule 22.2 \t\t exprSent -> ; \n");
};
selectSent : IF_KW simpleexp THEN_KW sentence
{
fprintf(fout, "Rule 23.1 \t\t selectSent -> IF_KW simpleexp THEN_KW sentence \n");
};
| IF_KW simpleexp THEN_KW sentence ELSE_KW sentence
{
fprintf(fout, "Rule 23.2 \t\t selectSent -> IF_KW simpleexp THEN_KW sentence ELSE_KW sentence \n");
};
| SWITCH_KW '(' simpleexp ')' caseelement defaultelement END_KW
{
fprintf(fout, "Rule 23.3 \t\t selectSent -> SWITCH_KW '(' simpleexp ')' caseelement defaultelement END_KW \n");
};
caseelement : CASE_KW int_type ':' sentence ';'
{
fprintf(fout, "Rule 24.1 \t\t caseelement -> CASE_KW int_type : sentence ; \n");
};
| caseelement CASE_KW int_type ':' sentence ';'
{
fprintf(fout, "Rule 24.2 \t\t caseelement -> caseelement CASE_KW int_type : sentence ; \n");
};
defaultelement : DEFAULT_KW ':' sentence ';'
{
fprintf(fout, "Rule 25.1 \t\t defaultelement -> DEFAULT_KW : sentence ; \n");
};
| /* empty */
{
fprintf(fout, "Rule 25.2 \t\t defaultelement -> e \n");
};
repeatSent : WHEN_KW '(' simpleexp ')' sentence
{
fprintf(fout, "Rule 26 \t\t repeatSent -> WHEN_KW '(' simpleexp ')' sentence \n");
};
returnSent : RETURN_KW expr ';'
{
fprintf(fout, "Rule 27 \t\t returnSent -> RETURN_KW ; \n");
};
breakSent : BREAK_KW ';'
{
fprintf(fout, "Rule 28 \t\t breakSent -> BREAK_KW ; \n");
};
expr : variable '=' expr
{
fprintf(fout, "Rule 29.1 \t\t expr -> variable = expr \n");
if($3.type == TYPE_BOOL) {
backpatch($3.truelist,nextquad);
backpatch($3.falselist,nextquad + 2);
emit("=", "1", "", $1.place);
emit("goto", "", "", std::to_string(nextquad + 3));
emit("=", "0", "",$1.place);
} else {
if (symbolTableLookup($1.place) != nullptr && symbolTableLookup($1.place)->is_array) {
$$.type = $1.type;
emit("[]=", $1.place, $3.place, $$.place);
} else if (symbolTableLookup($3.place) != nullptr && symbolTableLookup($3.place)->is_array) {
$$.type = $1.type;
emit("=[]", $1.place, $3.place, $$.place);
} else {
$$.type = $1.type;
emit("=", $3.place, "", $1.place);
}
}
};
| variable ASSIGN_PLUS expr
{
$$.place = newTemp($1.type, false);
$$.type = $1.type;
emit("+", $1.place, $3.place, $$.place);
emit("+", $1.place, $3.place, $1.place);
fprintf(fout, "Rule 29.2 \t\t expr -> variable += expr \n");
};
| variable ASSIGN_MINUS expr
{
$$.place = newTemp($1.type, false);
$$.type = $1.type;
emit("-", $1.place, $3.place, $$.place);
emit("-", $1.place, $3.place, $1.place);
fprintf(fout, "Rule 29.3 \t\t expr -> variable -= expr \n");
};
| variable ASSIGN_MULT expr
{
$$.place = newTemp($1.type, false);
$$.type = $1.type;
emit("*", $1.place, $3.place, $$.place);
emit("*", $1.place, $3.place, $1.place);
fprintf(fout, "Rule 29.4 \t\t expr -> variable *= expr \n");
};
| variable ASSIGN_DIV expr
{
$$.place = newTemp($1.type, false);
$$.type = $1.type;
emit("/", $1.place, $3.place, $$.place);
emit("/", $1.place, $3.place, $1.place);
fprintf(fout, "Rule 29.5 \t\t expr -> variable /= expr \n");
};
| variable INC_KW
{
$$.place = newTemp($1.type, false);
fprintf(fout, "Rule 29.6 \t\t expr -> variable ++ \n");
$$.type = $1.type;
emit("+", $1.place, "1", $$.place);
emit("+", $1.place, "1", $1.place);
};
| variable DEC_KW
{
fprintf(fout, "Rule 29.7 \t\t expr -> variable -- \n");
$$.place = newTemp($1.type, false);
$$.type = $1.type;
emit("-", $1.place, "1", $$.place);
emit("-", $1.place, "1", $1.place);
};
| simpleexp
{
fprintf(fout, "Rule 29.8 \t\t expr -> simpleexp \n");
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
};
simpleexp : simpleexp OR_KW M simpleexp
{
fprintf(fout, "Rule 30.1 \t\t simpleexp -> simpleexp OR simpleexp \n");
$$.place = newTemp(TYPE_BOOL, false);
backpatch($1.falselist,$3.quad);
$$.truelist = merge_lists($1.truelist,$4.truelist);
$$.falselist = $4.falselist;
$$.type = TYPE_BOOL;
};
| simpleexp AND_KW M simpleexp
{
fprintf(fout, "Rule 30.2 \t\t simpleexp -> simpleexp AND simpleexp \n");
$$.place = newTemp(TYPE_BOOL, false);
backpatch($1.truelist,$3.quad);
$$.truelist = $4.truelist;
$$.falselist = merge_lists($1.falselist,$4.falselist);
$$.type = TYPE_BOOL;
};
| simpleexp XOR_KW simpleexp
{
fprintf(fout, "Rule 30.3 \t\t simpleexp -> simpleexp XOR simpleexp \n");
};
| simpleexp ALSO_KW simpleexp
{
fprintf(fout, "Rule 30.4 \t\t simpleexp -> simpleexp ALSO simpleexp \n");
};
| NOT_KW simpleexp
{
fprintf(fout, "Rule 30.5 \t\t simpleexp -> NOT simpleexp \n");
$$.place = newTemp(TYPE_BOOL, false);
$$.type = TYPE_BOOL;
$$.truelist = $2.falselist;
$$.falselist = $2.truelist;
};
| relativeexp
{
fprintf(fout, "Rule 30.6 \t\t simpleexp -> relativeexp \n");
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
};
relativeexp : arthlogicexpr
{
fprintf(fout, "Rule 31.1 \t\t relativeexp -> arthlogicexpr \n");
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
};
| arthlogicexpr relativeop arthlogicexpr
{
fprintf(fout, "Rule 31.2 \t\t relativeexp -> arthlogicexpr relativeop arthlogicexpr \n");
$$.place = newTemp(TYPE_BOOL, false);
$$.type = TYPE_BOOL;
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit($2.place, $1.place, $3.place, $$.place);
printf("%d,,, %s\n", nextquad + 2, std::to_string(nextquad + 2).c_str());
emit("ifgoto", $$.place,"", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
};
relativeop : LT_KW
{
fprintf(fout, "Rule 32.1 \t\t relativeop -> < \n");
$$.place = new char[3];
strcpy($$.place,"<");
};
| LE_KW
{
fprintf(fout, "Rule 32.2 \t\t relativeop -> <= \n");
$$.place = new char[3];
strcpy($$.place,"<=");
};
| EQ_KW
{
fprintf(fout, "Rule 32.3 \t\t relativeop -> == \n");
$$.place = new char[3];
strcpy($$.place,"==");
};
| GE_KW
{
fprintf(fout, "Rule 32.4 \t\t relativeop -> >= \n");
$$.place = new char[3];
strcpy($$.place,">=");
};
| GT_KW
{
fprintf(fout, "Rule 32.5 \t\t relativeop -> > \n");
$$.place = new char[3];
strcpy($$.place,">");
};
arthlogicexpr : unaryexpr
{
fprintf(fout, "Rule 33.1 \t\t arthlogicexpr -> unaryexpr \n");
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
};
| arthlogicexpr PLUS_KW arthlogicexpr
{
fprintf(fout, "Rule 33.2 \t\t arthlogicexpr -> arthlogicexpr PLUS_KW arthlogicexpr \n");
$$.place = newTemp($1.type, false);
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("+", $1.place, $3.place, $$.place);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
};
| arthlogicexpr MINUS_KW arthlogicexpr
{
fprintf(fout, "Rule 33.3 \t\t arthlogicexpr -> arthlogicexpr MINUS_KW arthlogicexpr \n");
$$.place = newTemp($1.type, false);
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("-", $1.place, $3.place, $$.place);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
};
| arthlogicexpr MULT_KW arthlogicexpr
{
fprintf(fout, "Rule 33.4 \t\t arthlogicexpr -> arthlogicexpr MULT_KW arthlogicexpr \n");
$$.place = newTemp($1.type, false);
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("*", $1.place, $3.place, $$.place);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
};
| arthlogicexpr DIV_KW arthlogicexpr
{
fprintf(fout, "Rule 33.5 \t\t arthlogicexpr -> arthlogicexpr DIV_KW arthlogicexpr \n");
$$.place = newTemp($1.type, false);
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("-", $1.place, $3.place, $$.place);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
};
| arthlogicexpr MOD_KW arthlogicexpr
{
fprintf(fout, "Rule 33.6 \t\t arthlogicexpr -> arthlogicexpr MOD_KW arthlogicexpr \n");
$$.place = newTemp($1.type, false);
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("%", $1.place, $3.place, $$.place);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
};
unaryexpr : unaryop unaryexpr
{
if ($1.type == TYPE_UNKNOWN) {
$$.place = newTemp($2.type, false);
$$.type = $2.type;
$$.truelist = $2.truelist;
$$.falselist = $2.falselist;
emit($1.place, $2.place, "", $$.place);
}
else {
$$.place = newTemp($1.type, false);
$$.type = $1.type;
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit($1.place, $2.place, "", $$.place);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
}
fprintf(fout, "Rule 34.1 \t\t unaryexpr -> unaryop unaryexpr \n");
};
| opera
{
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
fprintf(fout, "Rule 34.2 \t\t unaryexpr -> opera \n");
};
unaryop : MINUS_KW
{
fprintf(fout, "Rule 35.1 \t\t unaryop -> - \n");
$$.place = new char[10];
$$.type = TYPE_UNKNOWN;
strcpy($$.place,"usub");
};
| MULT_KW
{
fprintf(fout, "Rule 35.2 \t\t unaryop -> * \n");
$$.place = new char[10];
$$.type = TYPE_INT;
strcpy($$.place,"asterisk");
};
| QUEST_MARK
{
fprintf(fout, "Rule 35.3 \t\t unaryop -> ? \n");
$$.place = new char[10];
$$.type = TYPE_INT;
strcpy($$.place,"quest");
};
opera : variable
{
fprintf(fout, "Rule 36.1 \t\t opera -> variable \n");
$$.place = $1.place;
};
| unvar
{
fprintf(fout, "Rule 36.2 \t\t opera -> unvar \n");
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
};
variable : idetifier_type
{
fprintf(fout, "Rule 37.1 \t\t variable -> idetifier_type \n");
symbolTableEntry* temp = symbolTableLookup($1.place);
if (temp == nullptr) {
printf("%d : Error! %s is not declared.\n", yylineno, $1.place);
} else {
$$.place = $1.place;
$$.type = temp->type;
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
}
};
| variable '[' expr ']'
{
fprintf(fout, "Rule 37.2 \t\t variable -> variable [ expr ] \n");
symbolTableEntry* tmp = symbolTableLookup($1.place);
if (tmp == nullptr || !tmp->is_array) {
printf("%d : Error! %s is not declared.\n", yylineno, $1.place);
} else {
$$.place = newTemp($1.type, false);
$$.type = tmp->type;
$$.truelist = makelist(nextquad + 1);
$$.falselist = makelist(nextquad + 2);
emit("ifgoto", $$.place, "", std::to_string(nextquad + 2));
emit("goto", "", "", std::to_string(nextquad + 1));
}
};
| variable '.' idetifier_type
{
fprintf(fout, "Rule 37.3 \t\t variable : variable . idetifier_type \n");
};
unvar : '(' expr ')'
{
fprintf(fout, "Rule 38.1 \t\t unvar -> ( expr ) \n");
$$.type = $2.type;
$$.place = $2.place;
$$.truelist = $2.truelist;
$$.falselist = $2.falselist;
};
| call
{
fprintf(fout, "Rule 38.2 \t\t unvar -> call \n");
};
| constant
{
fprintf(fout, "Rule 38.3 \t\t unvar : constant \n");
$$.type = $1.type;
$$.place = $1.place;
$$.truelist = $1.truelist;
$$.falselist = $1.falselist;
};
call : idetifier_type '(' argVector ')'
{
fprintf(fout, "Rule 39 \t\t call -> idetifier_type '(' argVector ')' \n");
};
argVector : argsVector
{
fprintf(fout, "Rule 40.1 \t\t argVector -> argsVector \n");
};
| /* empty */
{
fprintf(fout, "Rule 40.2 \t\t argVector -> e \n");
};
argsVector : argsVector ',' expr
{