-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
1334 lines (1242 loc) · 32 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
/* BISON TOKEN NAMES */
%token T_AND T_BEGIN T_FORWARD T_DIV T_DO T_ELSE T_END T_FOR T_FUNCTION T_IF T_ARRAY T_MOD T_NOT T_OF T_OR T_PROCEDURE T_PROGRAM T_RECORD T_THEN T_TO T_TYPE T_VAR T_WHILE T_ID T_INT T_STR T_ASSIGNMENT T_RANGE T_RELOP T_MUL
%{
#include <fstream>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <map> //data structure for symbol table
#include <vector>
#include <stack>
#include <string>
#include "lex.h"
#define YYDEBUG 1
#define YYERROR_VERBOSE 1
#define FW 50
#define FUNC_REF "funcall "
#define PROC_CALL "call "
#define SYM_OUTPUT "symtable.out"
#define RULES_OUTPUT "rules.out"
#define TAC_OUTPUT "a.txt"
#define LABEL "L"
typedef struct id_attr{
std::string type;
std::map< std::string, std::string > field_list;
size_t addr;
id_attr(const std::string _type, const size_t _addr):type(_type), addr(_addr){}
id_attr(void):type(""), addr(0){}
} id_attr;
typedef struct scope{
std::map<std::string, id_attr> symt;
scope *p;
scope(void):p(NULL){ //predefined types
symt["integer"] = id_attr("integer", 0);
symt["typedef integer"] = id_attr("integer", 1);
symt["string"] = id_attr("string", 2);
symt["typedef string"] = id_attr("string", 3);
symt["boolean"] = id_attr("boolean", 4);
symt["typedef boolean"] = id_attr("boolean", 5);
symt["var true"] = id_attr("boolean", 6); //"true" and "false" will first be recognized as variables
symt["var false"] = id_attr("boolean", 7);
}
scope(scope *_p):p(_p){}
} scope;
scope prog_scope, *current_scope = &prog_scope, *next_scope;
bool temp_var, unop, lhs_unop;
int argc = 0, current_argc = 0, s_err = 0, ind = 0, tmpc = 0, lc = 0, current_sgn, term_sgn, current_const, current_l, current_u, temp_exp = 1, temp_m_exp = 1; //l/u: array lower/upper bounds //reg: indicating which register to use
std::string prog_name, current_id, current_ret, type, current_typename, exp_type, lhs_type, ret_type, vt, index_t, current_var, for_var, lhs_vt, lhs_var, current_factor, et, _et, m_et, current_exp, _current_exp, current_m_exp, current_m_exps, tmp_exp, current_relop = ""; //tac output for current variable/expression
std::stack<bool> temp_var_save;
std::stack<std::string> func_ref, current_id_save, current_ret_save, type_save, current_typename_save, exp_type_save, lhs_type_save, ret_type_save, vt_save, current_var_save, lhs_vt_save, lhs_var_save, current_factor_save, et_save, _et_save, m_et_save, current_exp_save, _current_exp_save, current_m_exp_save, current_m_exps_save, current_relop_save, prev_id_save, array_t, for_var_save; //stacks are required for nested '(' Expression ')' s and '[' Expression ']' s
std::stack<int> current_sgn_save, temp_exp_save, temp_m_exp_save, term_sgn_save, lc_save;
std::vector<std::string> current_argv, current_param;
std::stack< std::vector<std::string> > func_param;
id_attr current_id_attr;
std::fstream rules(RULES_OUTPUT, std::ios::out | std::ios::trunc), tac(TAC_OUTPUT, std::ios::out | std::ios::trunc);
template <class T>
inline std::string to_string (const T & t){
std::stringstream ss;
ss << t;
return ss.str();
}
int yyerror(const char *), UpdateVar(void), UpdateType(scope *), LookupId(scope *, const std::string, std::string &);
std::string LookupTypeDef(const std::string), Temp(void), Temp(int), Temp_Eq(void), Temp_Eq(int);
void print_label(const std::string), print_next_label(const std::string), print_tac(const std::string), print_var(const std::string), print_exp(const std::string), print_m_exp(const std::string), print_m_exps(const std::string), print_exp_text(const std::string), print_addop(const std::string), print_mulop(const std::string), save_state(bool), restore_state(bool), print_multiplicand(void), add_param(void), get_exp(void);
/*, print_m_exp_text(const std::string), print_exp_text(void), print_m_exp_text(void)*/
%}
/* THE GRAMMAR */
%%
Program
:
T_PROGRAM T_ID
{
prog_name = std::string(yytext_ptr);
current_scope -> symt[std::string("program ").append(prog_name)] = id_attr("program", current_scope -> symt.size());
print_tac(std::string("goto program_").append(prog_name).append("\n\n"));
}
';' OptTypeDefinitions OptVariableDeclarations OptSubprogramDeclarations
{
print_label(std::string("program_").append(prog_name));
}
CompoundStatement '.'
{
print_tac("return\n");
rules<<"Program\n";
}
;
TypeDefinitions
:
T_TYPE TypeDefinition ';' _OptTypeDefinitions {rules<<"TypeDefinitions\n";}
;
OptTypeDefinitions
:
/* empty */ {rules<<"OptTypeDefinitions\n";}
|
TypeDefinitions {rules<<"OptTypeDefinitions\n";}
;
_OptTypeDefinitions
:
/* empty */ {rules<<"_OptTypeDefinitions\n";}
|
TypeDefinition ';' _OptTypeDefinitions {rules<<"_OptTypeDefinitions\n";}
;
TypeDefinition
:
T_ID {
current_id = std::string("typedef ").append(current_typename = std::string(yytext_ptr));
} '=' Type
{
current_id_attr.addr = current_scope -> symt.size();
current_scope -> symt[current_id] = current_id_attr; //this stores the FieldList of the type
prog_scope.symt[current_typename] = id_attr(LookupTypeDef(current_id_attr.type), current_scope -> symt.size()); //this stores type equivalence
current_id_attr.type = "";
current_id_attr.field_list.clear();
}
{rules<<"TypeDefinition\n";}
;
VariableDeclarations
:
T_VAR VariableDeclaration ';' _OptVariableDeclarations {rules<<"VariableDeclarations\n";}
;
VariableDeclaration
:
IdentifierList ':' Type {UpdateVar();} {rules<<"VariableDeclaration\n";}
;
OptVariableDeclarations
:
/* empty */ {rules<<"OptVariableDeclarations\n";}
|
VariableDeclarations {rules<<"OptVariableDeclarations\n";}
;
_OptVariableDeclarations
:
/* empty */ {rules<<"_OptVariableDeclarations\n";}
|
VariableDeclaration ';' _OptVariableDeclarations {rules<<"_OptVariableDeclarations\n";}
;
OptSubprogramDeclarations
:
/* empty */ {rules<<"OptSubprogramDeclarations\n";}
|
SubprogramDeclarations {rules<<"OptSubprogramDeclarations\n";}
;
SubprogramDeclarations
:
/* empty */ {rules<<"SubprogramDeclarations\n";}
|
SubprogramDeclaration ';' SubprogramDeclarations {rules<<"SubprogramDeclarations\n";}
;
SubprogramDeclaration
:
ProcedureDeclaration {rules<<"SubprogramDeclaration\n";}
|
FunctionDeclaration {rules<<"SubprogramDeclaration\n";}
;
ProcedureDeclaration
:
T_PROCEDURE T_ID
{
current_id = std::string(yytext_ptr);
next_scope = new scope(current_scope);
print_label(std::string("procedure_").append(current_id)); //++ind;
}
'(' FormalParameterList {
current_scope -> symt[std::string("procedure ").append(current_id)] = id_attr("void", current_scope -> symt.size()); //procedure returns type 'void'
current_id_attr.type = "";
current_id_attr.field_list.clear();
argc = 0;
current_scope = next_scope;
next_scope = NULL;
}')' ';' DeclarationBody
{
print_tac("return\n\n");
--ind;
current_scope = current_scope -> p;
rules<<"ProcedureDeclaration\n";
}
;
FunctionDeclaration
:
T_FUNCTION T_ID
{
current_ret = current_id = std::string(yytext_ptr);
next_scope = new scope(current_scope);
print_label(std::string("function_").append(current_id)); //++ind;
} '(' FormalParameterList ')' ':' ResultType
{
current_scope -> symt[std::string("function ").append(current_id)] = id_attr(exp_type, current_scope -> symt.size());
current_id_attr.type = "";
current_id_attr.field_list.clear();
argc = 0;
current_scope = next_scope;
next_scope = NULL;
}';' DeclarationBody
{
print_tac(std::string("funreturn ").append(current_ret).append("\n\n"));
--ind;
current_scope = current_scope -> p;
rules<<"FunctionDeclaration\n";
}
;
DeclarationBody
:
Block
{
rules<<"DeclarationBody\n";
}
|
T_FORWARD
{
print_tac("forward\t\t ;; note: forward declaration is not handled in this project\n");
rules<<"DeclarationBody\n";
}
;
FormalParameterList
:
{
argc = 0;
}
/* empty */ {rules<<"FormalParameterList\n";}
|
{
argc = 0;
}
IdentifierList ':' Type {
UpdateType(next_scope);
}OptIdentifiers {rules<<"FormalParameterList\n";}
;
OptIdentifiers
:
/* empty */ {rules<<"OptIdentifiers\n";}
|
';' IdentifierList ':' Type {
UpdateType(next_scope);
}
OptIdentifiers {rules<<"OptIdentifiers\n";}
;
Block
:
{
}
CompoundStatement
{
rules<<"Block\n";
}
|
VariableDeclarations CompoundStatement
{
rules<<"Block\n";
}
;
CompoundStatement
:
T_BEGIN StatementSequence T_END {rules<<"CompoundStatement\n";}
;
StatementSequence
:
Statement Statements {rules<<"StatementSequence\n";}
;
Statement
:
SimpleStatement {rules<<"Statement\n";}
|
StructuredStatement {rules<<"Statement\n";}
;
SimpleStatement
:
/* empty */ {rules<<"SimpleStatement\n";}
|
AssignmentStatement {rules<<"SimpleStatement\n";}
|
ProcedureStatement {rules<<"SimpleStatement\n";}
;
AssignmentStatement
:
Variable
{
lhs_type = exp_type;
lhs_var = current_var;
current_var = "";
lhs_vt = vt;
lhs_unop = unop;
unop = false;
}
T_ASSIGNMENT Expression
{
std::string l_type, r_type;
if (lhs_type != "" && exp_type != "" && (l_type = LookupTypeDef(lhs_type)) != (r_type = LookupTypeDef(exp_type))){ //if both lhs and rhs are syntatically valid (hence have types)
yyerror(std::string("incompatible types in assignment of ").append(r_type).append(" to ").append(l_type).c_str());
++s_err;
}else if (lhs_type == ""){
yyerror("unable to determine the type of the left-hand side due to previous error(s)");
}else if (exp_type == ""){
yyerror("unable to determine the type of the right-hand side due to previous error(s)");
}
lhs_type = "";
exp_type = ""; //note: CORNER CASE "a[i] := b[j]" contains 4 addresses, hence require a temporary when represented in tac form
tac<<current_exp<<lhs_var; //evaluate the right-hand side, get the left-hand side, and then perform the ':=' operator
current_exp = "";
current_relop = "";
lhs_var = "";
if (lhs_unop && (unop || (temp_exp == 2) || (temp_exp == 1 && temp_m_exp == 2))){
print_tac(Temp_Eq(++tmpc).append(et).append("\n"));
print_tac(lhs_vt.append(" := ").append(Temp()).append("\n"));
}else{
print_tac(lhs_vt.append(" := ").append(et).append("\n"));
}
et = "";
current_m_exp = "";
current_var = "";
lhs_unop = false;
rules<<"AssignmentStatement\n";
}
;
ProcedureStatement
:
T_ID {
if (!LookupId(current_scope, std::string("procedure ").append(prev_id), exp_type) && !LookupId(current_scope, std::string("function ").append(prev_id), exp_type)){
yyerror(std::string("procedure or function '").append(prev_id).append("' is not defined").c_str());
++s_err;
}
}
'('
{
save_state(true);
}
ActualParameterList
')'
{
restore_state(true);
for (size_t i = 0; i < current_param.size(); ++i){
print_tac(std::string("param ").append(current_param[i]).append("\n"));
}
current_param.clear();
print_tac(std::string(PROC_CALL).append(prev_id).append("\n"));
current_factor = Temp();
rules<<"ProcedureStatement\n";
}
;
StructuredStatement
:
CompoundStatement {rules<<"StructuredStatement\n";}
|
T_IF Expression
{
if (current_relop == ""){
get_exp();
print_tac(std::string("if ").append(et).append(" = true goto ").append(LABEL).append(to_string<int>(++lc)).append("\n"));
}else{
print_tac(std::string("if ").append(et).append(" goto ").append(LABEL).append(to_string<int>(++lc)).append("\n"));
}
current_relop = "";
}
T_THEN
{
lc_save.push(lc);
print_tac(std::string("goto ").append(LABEL).append(to_string<int>(lc + 1)).append("\n")); //goto else
print_next_label(std::string(LABEL).append(to_string<int>(lc))); //label for then //lc + 2: label for everything ekse after then and else
lc += 2;
}Statement CloseIf
{
rules<<"StructuredStatement\n";
}
|
T_WHILE
{
print_next_label(std::string(LABEL).append(to_string<int>(++lc)));
}
Expression
{
if (current_relop == ""){
get_exp();
print_tac(std::string("if ").append(et).append(" = true goto ").append(LABEL).append(to_string<int>(lc + 1)).append("\n"));
}else{
print_tac(std::string("if ").append(et).append(" goto ").append(LABEL).append(to_string<int>(lc + 1)).append("\n"));
}
current_relop = "";
}
T_DO
{
lc_save.push(lc); //label for while
print_tac(std::string("goto ").append(LABEL).append(to_string<int>(lc + 2)).append("\n")); //exit the while loop
print_next_label(std::string(LABEL).append(to_string<int>(lc + 1)));
lc += 2;
}
Statement
{
int t = lc_save.top();
lc_save.pop();
print_tac(std::string("goto ").append(LABEL).append(to_string<int>(t)).append("\n"));
print_next_label(std::string(LABEL).append(to_string<int>(t + 2)));
rules<<"StructuredStatement\n";
}
|
T_FOR T_ID
{
for_var = std::string(yytext_ptr);
for_var_save.push(for_var);
if (!LookupId(current_scope, std::string("var ").append(for_var), exp_type)){
yyerror(std::string("variable ").append(for_var).append(" is not declared").c_str());
++s_err;
}
}
T_ASSIGNMENT Expression {
get_exp();
print_tac(std::string(for_var).append(" := ").append(et).append("\n")); //initialization
current_relop = "";
} T_TO Expression
{
print_next_label(std::string(LABEL).append(to_string<int>(++lc))); //for loop condition
get_exp();
lc_save.push(lc);
print_tac(std::string("if ").append(for_var).append(" > ").append(et).append(" goto ").append(std::string(LABEL)).append(to_string<int>(++lc)).append("\n"));
current_relop = "";
} T_DO
{
++ind;
}
Statement
{
int t = lc_save.top();
std::string v = for_var_save.top();
lc_save.pop();
for_var_save.pop();
print_tac(std::string(v).append(" := ").append(v).append(" + 1\n"));
--ind;
print_tac(std::string("goto ").append(std::string(LABEL)).append(to_string<int>(t)).append("\n"));
print_next_label(std::string(LABEL).append(to_string<int>(t+1)));
rules<<"StructuredStatement\n";
}
;
CloseIf
:
/* empty */
{
rules<<"CloseIf\n";
print_next_label(std::string(LABEL).append(to_string<int>(lc_save.top() + 1)));
lc_save.pop();
}
|
T_ELSE
{
int t = lc_save.top();
print_tac(std::string("goto ").append(LABEL).append(to_string<int>(t + 2)).append("\n")); //close the if block
print_next_label(std::string(LABEL).append(to_string<int>(t + 1)));
}Statement{
print_next_label(std::string(LABEL).append(to_string<int>(lc_save.top() + 2)));
lc_save.pop();
rules<<"CloseIf\n";
}
;
Statements
:
/* empty */ {rules<<"Statements\n";}
|
';' Statement Statements {rules<<"Statements\n";}
;
Type /* todo: replace literal typename with special (reserved) symbols */
:
T_ID {
type = std::string(yytext_ptr);
if (current_id_attr.type == ""){
current_id_attr.type = type;
}
} {rules<<"Type\n";}
|
T_ARRAY '[' Constant{current_l = current_const;} T_RANGE Constant{current_u = current_const;} ']' T_OF
{
current_id_attr.type.append("array[").append(to_string<int>(current_l)).append("..").append(to_string<int>(current_u)).append("]_of_");
} Type {rules<<"Type\n";}
{
current_id_attr.type.append(LookupTypeDef(type));
current_id_attr.field_list["."] = type;
}
|
T_RECORD {
current_id_attr.type.append("record{");
} FieldList T_END {
current_id_attr.type.append("}");
} {rules<<"Type\n";}
;
ResultType
:
T_ID {
std::string type_name(yytext_ptr);
exp_type = LookupTypeDef(type_name);
if (!LookupId(current_scope, type_name, exp_type)){
yyerror(std::string("invalid return type: type '").append(type_name).append("' is not declared").c_str());
++s_err;
}
rules<<"ResultType\n";
}
;
FieldList /* assuming FieldList is only used in record declarations */
:
/* empty */ {rules<<"FieldList\n";}
|
IdentifierList ':' Type {
UpdateType(NULL);
}
OptIdentifiers {rules<<"FieldList\n";}
;
Constant
:
T_INT
{
current_const = atoi(yytext_ptr);
rules<<"Constant\n";
}
|
Sign T_INT
{
current_const = current_sgn * atoi(yytext_ptr);
rules<<"Constant\n";
}
;
Expression
:
SimpleExpression
{
rules<<"Expression\n";
}
|
SimpleExpression
{
get_exp();
_et = et;
_current_exp = current_exp; //et = "";
}
RelationalOp
SimpleExpression
{
get_exp();
current_exp = _current_exp.append(current_exp);
et = _et.append(" ").append(current_relop).append(" ").append(et);
temp_exp = 2;
exp_type = "boolean";
rules<<"Expression\n";
}
;
RelationalOp
:
T_RELOP
{
current_relop = std::string(yytext_ptr);
rules<<"RelationalOp\n";
}
|
'='
{
current_relop = "=";
rules<<"RelationalOp\n";
}
;
SimpleExpression
:
{
temp_exp = 1; //at least 1 term exists on the right-hand side
current_exp = "";
current_m_exps = "";
et = "";
}
Term
{
term_sgn = 1;
print_multiplicand();
}
Summand
{
current_exp = current_m_exps.append(current_exp);
rules<<"SimpleExpression\n";
}
|
Sign
{
temp_exp = 1; //at least 1 term exists on the right-hand side
term_sgn = current_sgn;
current_exp = "";
current_m_exps = "";
et = "";
}
Term
{
print_multiplicand();
}
Summand
{
current_exp = current_m_exps.append(current_exp);
rules<<"SimpleExpression\n";
}
;
MulOp
:
T_MUL
{
print_mulop(" * ");
rules<<"MulOp\n";
}
|
T_DIV
{
print_mulop(" / ");
rules<<"MulOp\n";
}
|
T_MOD
{
print_mulop(" mod ");
rules<<"MulOp\n";
}
|
T_AND
{
print_mulop(" and ");
rules<<"MulOp\n";
}
;
Multiplicand
:
/* empty */
|
MulOp Factor
{
if (temp_m_exp <= 2){
m_et.append(current_factor);
}else{
current_m_exp.append(current_factor).append("\n");
}
current_factor = "";
}
Multiplicand
{
rules<<"Multiplicand\n";
}
;
Sign
:
'+'
{
current_sgn = 1; /* note: different semantic actions needed, depending on whether sign appears in array index or in some expression */
rules<<"Sign\n";
}
|
'-'
{
current_sgn = -1;
rules<<"Sign\n";
}
;
AddOp
:
Sign
{
print_addop(current_sgn == -1 ? " - " : " + ");
rules<<"AddOp\n";
}
|
T_OR
{
print_addop(" or ");
rules<<"AddOp\n";
}
;
Term
:
Factor
{
temp_m_exp = 1;
m_et = current_factor;
current_m_exp = "";
}
Multiplicand
{
rules<<"Term\n";
}
;
Summand
:
/* empty */
{
rules<<"Summand\n";
}
|
AddOp Term
{
term_sgn = 1;
print_multiplicand();
if (temp_exp > 2){
current_exp.append("\n");
}
}
Summand {
rules<<"Summand\n";
}
;
Factor
:
{
unop = false;
}
T_INT
{
current_factor = std::string(yytext_ptr);
exp_type = "integer";
rules<<"Factor\n";
}
|
T_STR
{
current_factor = std::string(yytext_ptr);
exp_type = "string";
rules<<"Factor\n";
}
|
Variable
{
current_m_exps.append(current_var);
current_var = "";
current_factor = vt;
rules<<"Factor\n";
}
|
FunctionReference
{ //unop = true; //note: with the current grammar using unop for function calls and params would be too complicated and error-prone
for (size_t i = 0; i < func_param.top().size(); ++i){
print_m_exps(std::string("param ").append(func_param.top()[i]).append("\n"));
}
func_param.pop();
print_m_exps(Temp_Eq(++tmpc).append(FUNC_REF).append(func_ref.top()).append("\n"));
current_factor = Temp();
func_ref.pop();
rules<<"Factor\n";
}
|
T_NOT Factor
{
print_m_exps(Temp_Eq(++tmpc).append(current_factor).append("\n"));
print_m_exps(Temp_Eq().append("not ").append(Temp()).append("\n"));
current_factor = Temp();
unop = false;
rules<<"Factor\n";
}
|
'('
{
save_state(false);
}
Expression
{
exp_type = "";
if (temp_exp == 1 && temp_m_exp == 1){ //constant or single variable
current_factor = et;
}else if (temp_exp == 2 || (temp_exp == 1 && temp_m_exp == 2)){ //only 1 operator in current expression
print_exp(Temp_Eq(++tmpc).append(et).append("\n"));
current_factor = Temp();
}else{ //temporary variable required
current_factor = et;
}
tmp_exp = current_exp;
}
')'
{
restore_state(false);
current_m_exps.append(tmp_exp);
rules<<"Factor\n";
}
;
FunctionReference
:
T_ID
{
if (!LookupId(current_scope, std::string("function ").append(prev_id), exp_type)){ // <-- must be a function
yyerror(std::string("invalid function reference: function '").append(prev_id).append("' is not defined").c_str());
++s_err;
}
func_ref.push(prev_id); //reason for using stack: function reference can also occur in ActualParameterList
ret_type = exp_type;
}
'('
{
save_state(true);
}
ActualParameterList //similar reasoning as above
{
func_param.push(current_param);
current_param.clear();
}
')'
{
restore_state(true);
exp_type = ret_type;
rules<<"FunctionReference\n";
}
;
Variable
:
T_ID
{ /* note: the 'variable' in this context could also be a function's return value */
temp_var = false;
unop = false;
if (!LookupId(current_scope, std::string("var ").append(prev_id), exp_type) && !LookupId(current_scope, std::string("function ").append(prev_id), exp_type)){ // <-- must be a variable
yyerror(std::string("variable '").append(prev_id).append("' is not declared").c_str());
++s_err;
}
current_var = "";
vt = prev_id;
}
ComponentSelection {
if (temp_var){
vt = Temp();
}
rules<<"Variable\n";
}
;
ComponentSelection
:
/* empty */
{
rules<<"ComponentSelection\n";
}
|
'.'
{
if (unop){
print_var(Temp_Eq(++tmpc).append(vt).append("\n"));
print_var(Temp_Eq(tmpc + 1).append(Temp()).append("."));
unop = false;
}else if (!temp_var){
print_var(Temp_Eq(tmpc + 1).append(vt).append("."));
temp_var = true;
}else{
print_var(Temp_Eq(tmpc + 1).append(Temp()).append("."));
}
vt = Temp(++tmpc);
}
T_ID
{
std::map<std::string, id_attr>::iterator t;
std::map<std::string, std::string>::iterator ft;
if (exp_type != ""){ //if no error occurred in previous type lookup
if ( (t = current_scope -> symt.find(std::string("typedef ").append(exp_type))) == current_scope -> symt.end() ){
yyerror(std::string("type '").append(exp_type).append("' is not defined").c_str());
exp_type = "";
++s_err;
}else if ( (ft = t -> second.field_list.find(prev_id)) == t -> second.field_list.end() ){
yyerror(std::string("field '").append(prev_id).append("' of type '").append(exp_type).append("' is not defined").c_str());
exp_type = "";
++s_err;
}else{
exp_type = ft -> second;
}
}
current_var.append(prev_id).append("\n");
}
ComponentSelection
{
rules<<"ComponentSelection\n";
}
|
'['
{
array_t.push(vt);
save_state(true);
}
Expression
{
vt = array_t.top();
array_t.pop();
if (unop){
print_exp(Temp_Eq(++tmpc).append(et).append("\n"));
et = Temp();
unop = false;
}else if ((temp_exp == 2) || (temp_exp == 1 && temp_m_exp == 2)){ //note: in tac form [] operator can only have constant or 1 single variable as argument
print_exp(Temp_Eq(++tmpc).append(et).append("\n"));
et = Temp();
}
if (!temp_var){
index_t = vt.append("[").append(et).append("]");
}else{
index_t = Temp().append("[").append(et).append("]");
}
tac<<current_exp<<current_var; //evaluate the right-hand side, get the left-hand side, and then perform the ':=' operator
}
']'
{
restore_state(true);
current_var = "";
vt = index_t;
unop = true;
std::map<std::string, id_attr>::iterator t;
std::map<std::string, std::string>::iterator ft;
if (exp_type != ""){ //if no error occurred in previous type lookup
if ( (t = current_scope -> symt.find(std::string("typedef ").append(exp_type))) == current_scope -> symt.end() ){
yyerror(std::string("type '").append(exp_type).append("' is not defined").c_str());
exp_type = "";
++s_err;
}else if ( (ft = t -> second.field_list.find(".")) == t -> second.field_list.end() ){
yyerror(std::string("type '").append(exp_type).append("' is not defined as an array").c_str());
exp_type = "";
++s_err;
}else{
exp_type = ft -> second;
}
}
}
ComponentSelection{
rules<<"ComponentSelection\n";
}
;
ActualParameterList
:
/* empty */ {rules<<"ActualParameterList\n";}
|
Expression
{
add_param();
current_relop = "";
}
OptExpressions {rules<<"ActualParameterList\n";}
;
OptExpressions
:
/* empty */ {rules<<"OptExpressions\n";}
|
',' Expression
{
add_param();
current_relop = "";
}
OptExpressions {rules<<"OptExpressions\n";}
;
IdentifierList
:
T_ID {
current_argv.push_back(std::string(yytext_ptr));
++current_argc;
} Identifiers {rules<<"IdentifierList\n";}
;
Identifiers