-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.y
More file actions
1776 lines (1628 loc) · 46.3 KB
/
parser.y
File metadata and controls
1776 lines (1628 loc) · 46.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
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 "helper.hpp"
#include<bits/stdc++.h>
using namespace std;
void yyerror(const char * s);
int yylex();
extern int yylineno;
extern FILE* yyin;
#define YYERROR_VERBOSE 1
int nodes = 0;
map<int, string> node_map;
map<int, string> node_shape;
map<string, ST*> cls_ST;
vector<pair<int,string> > baap, betaa;
extern stack<int> indent_cnt;
string s;
extern char* yytext;
Type None_type = {"None",0};
Type Int_type = {"int",0};
Type Bool_type = {"bool",0};
Type Float_type = {"float",0};
Type Str_type = {"str",0};
stack <string> s_label;
stack <string> e_label;
ST* CurrST;
int offset = 0;
string res;
%}
%define parse.error verbose
%define api.value.type { struct semantic_value_type }
/* Terminals */
%token INTEGER
%token FLOAT
%token STRING
%token SHORT_STRING
%token LONG_STRING
%token PLUS
%token MINUS
%token MULTIPLY
%token EXP
%token SLASH
%token DOUBLESLASH
%token DIVIDE
%token DIVIDEDIVIDE
%token MOD
%token AT
%token RIGHTSHIFT
%token LEFTSHIFT
%token AMPERSAND
%token EQUAL
%token NOTEQUAL
%token GREATER
%token LESS
%token GREATEREQ
%token LESSEQ
%token BITOR
%token BITXOR
%token BITNOT
%token ASSIGN
%token PLUSEQUAL
%token MINUSEQUAL
%token MULTIPLYEQUAL
%token DIVIDEEQUAL
%token MODEQUAL
%token EXPONENTEQUAL
%token ANDEQUAL
%token OREQUAL
%token XOREQUAL
%token LEFTSHIFTEQUAL
%token RIGHTSHIFTEQUAL
%token COLONEQUAL
%token LEFTBRACKET
%token RIGHTBRACKET
%token LEFTSQUAREBRACKET
%token RIGHTSQUAREBRACKET
%token LEFTCURLYBRACKET
%token RIGHTCURLYBRACKET
%token COMMA
%token COLON
%token SEMICOLON
%token DOT
%token ARROW
%token DIVIDEDIVIDEEQUAL
%token ATEQUAL
%token NAME_DUNDER
%token MAIN_DUNDER
%token INIT_DUNDER
%token SPECIAL_CHAR
%token UNDERSCORE
%token DOUBLE_QUOTE
%token SINGLE_QUOTE
%token BACKSLASH
%token SPACE
%token FALSE
%token AWAIT
%token ELSE
%token IMPORT
%token PASS
%token NONE
%token BREAK
%token EXCEPT
%token IN
%token RAISE
%token TRUE
%token CLASS
%token FINALLY
%token RANGE
%token IS
%token RETURN
%token AND
%token CONTINUE
%token FOR
%token LAMBDA
%token TRY
%token AS
%token DEF
%token FROM
%token NONLOCAL
%token WHILE
%token ASSERT
%token DEL
%token GLOBAL
%token NOT
%token WITH
%token ASYNC
%token ELIF
%token IF
%token OR
%token YIELD
%token NAME
%token COMMENT
%token NEWLINE
%token INDENT
%token DEDENT
%token ELLIPSIS
%token PRINT
%type module
%type file_input
%type funcdef
%type parameters
%type typedargslist
%type argument1
%type tfpdef
%type stmt
%type simple_stmt
%type small_stmt_help
%type small_stmt
%type expr_stmt
%type augassign
%type flow_stmt
%type break_stmt
%type continue_stmt
%type return_stmt
%type global_stmt
%type assert_stmt
%type compound_stmt
%type if_stmt
%type if_stmt_help
%type while_stmt
%type for_stmt
%type suite
%type stmt_help
%type test
%type and_test
%type not_test
%type comparison
%type comp_op
%type expr
%type xor_expr
%type and_expr
%type shift_expr
%type arith_expr
%type term
%type factor
%type power
%type atom_expr
%type atom
// %type exprlist
// %type exprlist_help
%type testlist
%type testlist_list
%type classdef
%type arglist
%type arglist_help
%type argument
// %type comp_for
// %type comp_if
// %type comp_iter
%type type_name
%type func_action
%type lvalue
%type dummy_if
%type dummy_elif
%type loop_action
%type ranges
%type print_stmt
%type dot_trailer
%type call_trailer
%type index_trailer
/*rules */
%start module
%%
module: file_input {
res += ($1)._3AC;
}
file_input : file_input NEWLINE {
$$ = $1;
}
| file_input stmt {
($$)._3AC = ($1)._3AC + ($2)._3AC;
}
| NEWLINE {
}
| stmt {
$$ = $1;
}
;
func_action : {
ST *newST = new ST;
newST->parentST = CurrST;
newST->type = FUNCTION_ST;
CurrST = newST;
}
funcdef: DEF NAME func_action parameters ARROW type_name {
CurrST->return_type = ($6).type;
ST* parentST = CurrST->parentST;
auto fun = new Function;
fun->return_type = ($6).type;
fun->args = ($4).args;
fun->funcST = CurrST;
auto entry = new STEntry;
if(parentST->type == CLASS_ST){
entry->lex = CurrST->current_Class->name + "." + ($2).lex;
}
else{
entry->lex = ($2).lex;
}
entry->func = fun;
entry->lineno = ($1).lineno;
entry->func = fun;
entry->lineno = ($1).lineno;
parentST->insert(($2).lex, entry);
} COLON suite
{
ST* oldST = CurrST;
CurrST = CurrST->parentST;
string lex;
if(CurrST->type == CLASS_ST){
// cout << "Class function" << endl;
lex = CurrST->current_Class->name + "." + ($2).lex;
}
else{
lex = ($2).lex;
}
($$)._3AC = "@beginfunc " + lex + ":\n" + "ra = popparam\n" + ($4)._3AC + ($9)._3AC + "goto ra\n" "@endfunc\n";
}
| DEF NAME func_action parameters COLON {
CurrST->return_type = None_type;
ST* parentST = CurrST->parentST;
auto fun = new Function;
fun->return_type = None_type;
fun->args = ($4).args;
fun->funcST = CurrST;
auto entry = new STEntry;
if(parentST->type == CLASS_ST){
entry->lex = CurrST->current_Class->name + "." + ($2).lex;
}
else{
entry->lex = ($2).lex;
}
entry->func = fun;
entry->lineno = ($1).lineno;
entry->func = fun;
entry->lineno = ($1).lineno;
parentST->insert(($2).lex, entry);
} suite{
ST* oldST = CurrST;
CurrST = CurrST->parentST;
string lex;
if(CurrST->type == CLASS_ST){
// cout << "Class function" << endl;
lex = CurrST->current_Class->name + "." + ($2).lex;
}
else{
// cout << "Global function" << endl;
lex = ($2).lex;
}
($$)._3AC = "@beginfunc " + lex + ":\n" + "ra = popparam\n"+ ($4)._3AC + ($7)._3AC + "goto ra\n" + "@endfunc\n";
}
;
parameters: LEFTBRACKET typedargslist RIGHTBRACKET {
$$ = $2;
}
| LEFTBRACKET RIGHTBRACKET {
}
typedargslist : argument1 {
$$ = $1;
($$).args.push_back(($1).type);
}
| typedargslist COMMA argument1 {
$$ = $1;
($$).args.push_back(($3).type);
$$._3AC = ($1)._3AC + ($3)._3AC;
}
argument1 : tfpdef {
$$ = $1;
$$._3AC = ($1).lex + " = popparam\n";
}
| tfpdef ASSIGN test {
($$).type = ($1).type;
if(($1).type!=($3).type){
cerr <<"Error! Types on both the sides of assignment do not match in Line number: "<<yylineno<<endl;;
exit(1);
}
($$)._3AC = ($3)._3AC + ($1).lex + " = " + ($3).lex + "\n";
}
tfpdef: NAME {
if ($$.lex != "self"){
cerr << "Error! Type of argument not defined at line no. " << yylineno << endl;
exit(1);
}
if (CurrST->parentST->type != CLASS_ST){
cerr << "Error! self expression used outside class in Line number: "<< yylineno << endl;
exit(1);
}
auto curr = new STEntry;
curr->lex = ($1).lex;
curr->type = {CurrST->parentST->current_Class->name, 0};
($$).type = curr->type;
curr->lineno = ($1).lineno;
CurrST->insert(($1).lex, curr);
}
| NAME COLON type_name {
auto curr = new STEntry;
($$).type = ($3).type;
curr->lex = ($1).lex;
curr->lineno = ($1).lineno;
curr->type = ($3).type;
CurrST->insert(($1).lex, curr);
}
stmt: simple_stmt {
$$ = $1;
}
| compound_stmt {
$$ = $1;
}
;
simple_stmt : small_stmt_help SEMICOLON NEWLINE {
$$ = $1;
}
| small_stmt_help NEWLINE {
$$ = $1;
}
small_stmt_help : small_stmt {
$$ = $1;
}
| small_stmt_help SEMICOLON small_stmt {
($$)._3AC = ($1)._3AC + ($3)._3AC;
}
small_stmt: expr_stmt {
$$ = $1;
}
| flow_stmt {
$$ = $1;
}
| global_stmt {
$$ = $1;
}
| assert_stmt {
$$ = $1;
}
| print_stmt {
$$ = $1;
}
print_stmt : PRINT LEFTBRACKET test RIGHTBRACKET {
($$)._3AC = ($3)._3AC;
($$)._3AC += "param " + ($3).lex + "\n";
($$)._3AC += "call print 1\n";
}
/*
a:int = 5
a:int
1,3 : int
*/
expr_stmt: lvalue COLON type_name {
if (CurrST->type == FUNCTION_ST && CurrST->parentST->type == CLASS_ST && ($1).is_attr){
auto entry = new STEntry;
entry->lex = ($1).attr_name;
entry->type = ($3).type;
entry->lineno = ($2).lineno;
CurrST->parentST->insert(($1).attr_name, entry);
}
else{
auto entry = new STEntry;
entry->lex = ($1).lex;
entry->type = ($3).type;
entry->lineno = ($2).lineno;
($$)._3AC += "stackpointer +xxx\n";
CurrST->insert(($1).lex, entry);
}
}
| lvalue COLON type_name ASSIGN test
{
if (($3).type != ($5).type){
if (($3).type == Float_type && ($5).type == Int_type){
($5).type = Float_type;
}
else if (($3).type == Int_type && ($5).type == Float_type){
($5).type = Int_type;
}
else{
cerr << "Error! Type mismatch in assignment at line number: " << yylineno << endl;
cerr << "Expected: " << ($3).type.s << " Got: " << ($5).type.s << endl;
exit(1);
}
}
if (CurrST->type == FUNCTION_ST && CurrST->parentST->type == CLASS_ST && ($1).is_attr){
auto entry = new STEntry;
entry->lex = ($1).attr_name;
entry->type = ($3).type;
entry->lineno = ($2).lineno;
CurrST->parentST->insert(($1).attr_name, entry);
offset = CurrST->parentST->lookup(($1).attr_name)->offset;
($$)._3AC = ($5)._3AC + ($1).lex + "[" + to_string(offset) + "] = " + ($5).lex + "\n";
}
else{
auto entry = new STEntry;
entry->lex = ($1).lex;
entry->type = ($3).type;
entry->lineno = ($2).lineno;
// cout << "Here inserting " << ($1).lex << " at line number: " << ($2).lineno << endl;
($$)._3AC += "stackpointer +xxx\n";
CurrST->insert(($1).lex, entry);
($$)._3AC = ($5)._3AC + ($1).lex + " = " + ($5).lex + "\n";
}
}
| atom_expr ASSIGN test {
if (($1).is_assign == 0){
cerr << "Error! Lvalue required in assignment at line number: " << yylineno << endl;
exit(1);
}
if (!($1).is_attr && !($1).is_idx){
auto entry = CurrST->lookup(($1).lex);
if (entry->type != ($3).type){
cerr << "Error! Type mismatch in Line number: " << yylineno << endl;
exit(1);
}
}
($$)._3AC = ($1)._3AC + ($3)._3AC + ($1).lex + " = " + ($3).lex + "\n";
}
| atom_expr augassign test {
if (($1).is_assign == 0){
cerr << "Error! Lvalue required in assignment at line number: " << yylineno << endl;
exit(1);
}
Type t;
if (!($1).is_attr){
auto entry = CurrST->lookup(($1).lex);
if (entry->type != ($3).type){
// cerr << "Error! Type mismatch in Line number: " << yylineno << endl;
// exit(1);
if (entry->type == Float_type && ($3).type == Int_type){
($3).type = Float_type;
}
if (entry->type != ($3).type){
cerr << "Error! Type mismatch in Line number: " << yylineno << endl;
exit(1);
}
}
t = entry->type;
}
else {
t = ($1).type;
}
if(($2).lex=="PLUSEQUAL" || ($2).lex=="MINUSEQUAL" || ($2).lex=="MULTIPLYEQUAL" || ($2).lex=="DIVIDEEQUAL" || ($2).lex=="MODEQUAL" || ($2).lex=="EXPONENTEQUAL" ){
if(t != Int_type && t !=Float_type){
cerr<<"Error! Unsupported type at line no. "<<yylineno<<endl;
exit(1);
}
if (t == Float_type && ($3).type == Int_type){
($3).type = Float_type;
}
if (t != ($3).type){
cerr << "Error! Type Mismatch at line no. " <<yylineno<<endl;
exit(1);
}
($$).type=t;
}
else if(($2).lex=="RIGHTSHIFTEQUAL" || ($2).lex=="LEFTSHIFTEQUAL" || ($2).lex=="ANDEQUAL" || ($2).lex=="OREQUAL" || ($2).lex=="XOREQUAL"){
if(t != Int_type){
cerr<<"Error! Unsupported type at line no. "<<yylineno<<endl;
exit(1);
}
if (t != ($3).type){
cerr << "Error! Type Mismatch at line no. " <<yylineno<<endl;
exit(1);
}
($$).type=t;
}
else if(($2).lex=="DIVIDEDIVIDEEQUAL"){
if(t != Int_type && t !=Float_type){
cerr<<"Error! Unsupported type at line no. "<<yylineno<<endl;
exit(1);
}
if (t != ($3).type){
cerr << "Error! Type Mismatch at line no. " <<yylineno<<endl;
exit(1);
}
($$).type=Int_type;
}
string str = ($2).lex;
str.pop_back();
string tmpp = newTemp();
($$)._3AC = ($1)._3AC;
($$)._3AC = ($3)._3AC + tmpp + " = " + ($1).lex + " " + str + " " + ($3).lex + "\n";
($$)._3AC = ($$)._3AC + ($1).lex + " = " + tmpp + "\n";
}
| test {
$$ = $1;
}
type_name : NAME {
if (sz.find(($1).lex) == sz.end())
{
cerr << "ERROR! Type not defined in line no. " << yylineno << endl;
exit(1);
}
($$).type.s = ($1).lex;
($$).type.is_list = false;
}
| NAME LEFTSQUAREBRACKET NAME RIGHTSQUAREBRACKET {
if(string(($1).lex) != "list") {
cerr << "Error! syntax error in list declaration in Line number: " << yylineno << endl;
exit(1);
}
if (sz.find(($3).lex)==sz.end()){
cerr << "Error! Type not defined in line no. : " << yylineno << endl;
exit(1);
}
($$).type.s = ($3).lex;
($$).type.is_list = true;
}
| NONE {
($$).type = None_type;
}
augassign: PLUSEQUAL {
($$).lex = "+=";
}
| MINUSEQUAL {
($$).lex = "-=";
}
| MULTIPLYEQUAL {
($$).lex = "*=";
}
| DIVIDEEQUAL {
($$).lex = "/=";
}
| MODEQUAL {
($$).lex = "%=";
}
| ANDEQUAL {
($$).lex = "&=";
}
| OREQUAL {
($$).lex = "|=";
}
| XOREQUAL {
($$).lex = "^=";
}
| LEFTSHIFTEQUAL {
($$).lex = "<<=";
}
| RIGHTSHIFTEQUAL {
($$).lex = ">>=";
}
| EXPONENTEQUAL {
($$).lex = "**=";
}
| DIVIDEDIVIDEEQUAL {
($$).lex = "//=";
}
flow_stmt: break_stmt {
$$=$1;
}
| continue_stmt {
$$=$1;
}
| return_stmt {
$$=$1;
}
break_stmt: BREAK {
($$)._3AC = "goto " + e_label.top() + "\n";
}
continue_stmt: CONTINUE {
($$)._3AC = "goto " + s_label.top() + "\n";
}
return_stmt: RETURN test {
// should occur only in a function
if(CurrST->type != FUNCTION_ST) {
cerr << "Error! Return Statement can only occur within function at line no. "<<yylineno<<endl;
exit(1);
}
// the type of test should match the function type
if (($2).type != CurrST->return_type){
// cout << "$2:" << ($2).type.s << ": " << CurrST->return_type.s << endl;
cerr << "Error! Type mismatch in Return Statement at line no. "<<yylineno<<endl;
exit(1);
}
$$._3AC = ($2)._3AC + "push " + ($2).lex + "\n";
}
| RETURN {
if(CurrST->type != FUNCTION_ST) {
cerr << "Error! Return Statement can only occur within function at line no. "<<yylineno<<endl;
exit(1);
}
if (CurrST->return_type != None_type){
cout << "Error! No value recieved in Return Statement at line no. "<<yylineno<<endl;
exit(1);
}
}
global_stmt: GLOBAL NAME {
is_global[($2).lex] = true;
}
| global_stmt COMMA NAME {
is_global[($3).lex] = true;
}
assert_stmt: ASSERT test COMMA test {
}
| ASSERT test {
}
compound_stmt: if_stmt {
$$=$1;
}
| while_stmt {
$$=$1;
}
| for_stmt {
$$=$1;
}
| funcdef {
// $$=$1;
($$)._3AC = "";
res += ($1)._3AC;
}
| classdef {
$$=$1;
}
dummy_if : IF test COLON {
($$).next = newLabel();
($$)._3AC = ($2)._3AC + "ifz " + ($2).lex + " goto " + ($$).next + "\n";
}
if_stmt: dummy_if suite if_stmt_help_1 {
auto s = newLabel();
($$)._3AC = ($1)._3AC + ($2)._3AC + "goto " + s + "\n" + "@label " + ($1).next + ":\n" + ($3)._3AC + "@label " + s + "\n";
}
| dummy_if suite if_stmt_help {
auto s = newLabel();
($$)._3AC = ($1)._3AC + ($2)._3AC + "goto " + s + "\n" + "@label " + ($1).next + ":\n" + ($3)._3AC + "@label " + s + "\n";
}
| dummy_if suite ELSE COLON suite {
auto s = newLabel();
($$)._3AC = ($1)._3AC + ($2)._3AC + "goto " + s + "\n" + "@label " + ($1).next + ":\n" + ($5)._3AC + "@label " + s + "\n";
}
| dummy_if suite {
($$)._3AC = ($1)._3AC + ($2)._3AC + "@label " + ($1).next + ":\n";
}
dummy_elif : ELIF test COLON {
($$).next = newLabel();
($$)._3AC = ($2)._3AC + "ifz " + ($2).lex + " goto " + ($$).next + "\n";
}
if_stmt_help : dummy_elif suite {
($$)._3AC = ($1)._3AC + ($2)._3AC + "@label " + ($1).next + ":\n";
}
| dummy_elif suite if_stmt_help {
auto s = newLabel();
($$)._3AC = ($1)._3AC + ($2)._3AC + "goto " + s + "\n" + "@label " + ($1).next + ":\n" + ($3)._3AC + "@label " + s + ":\n";
}
if_stmt_help_1 : dummy_elif suite ELSE COLON suite {
auto s = newLabel();
($$)._3AC = ($1)._3AC + ($2)._3AC + "goto " + s + "\n" + "@label " + ($1).next + ":\n" + ($5)._3AC + "@label " + s + ":\n";
}
| dummy_elif suite if_stmt_help_1 {
auto s = newLabel();
($$)._3AC = ($1)._3AC + ($2)._3AC + "goto " + s + "\n" + "@label " + ($1).next + ":\n" + ($3)._3AC + "@label " + s + ":\n";
}
loop_action : {
auto s = newLabel();
auto e = newLabel();
s_label.push(s);
e_label.push(e);
}
while_stmt: WHILE test COLON loop_action suite {
auto s = s_label.top();
auto e = e_label.top();
($$)._3AC = "@label " + s + ":\n" + ($2)._3AC + "ifz " + ($2).lex + " goto " + e + ":\n" + ($5)._3AC + "goto " + s + "\n" + "@label " + e + ":\n";
s_label.pop();
e_label.pop();
}
| WHILE test COLON loop_action suite ELSE COLON suite{
auto s = s_label.top();
auto e = newLabel();
auto f = e_label.top();
($$)._3AC = "@label " + s + ":\n" + ($2)._3AC + "ifz " + ($2).lex + " goto " + e + "\n" + ($5)._3AC + "goto " + s + "\n" + "@label " + e + ":\n" + ($8)._3AC + "@label " + f + ":\n";
s_label.pop();
e_label.pop();
}
for_stmt: FOR NAME IN ranges COLON loop_action suite ELSE COLON suite {
($$)._3AC += ($4)._3AC;
auto st = CurrST->lookup(($2).lex);
($$)._3AC += ($2).lex + " = " + ($4).rng_start + "\n";
auto s = s_label.top();
auto e = newLabel();
auto f = e_label.top();
auto temp = newTemp();
($$)._3AC += "@label " + s + ":\n";
($$)._3AC += temp + "=" + ($2).lex + " < " + ($4).rng_end + ":\n";
auto temp2 = newTemp();
string incr = temp2 + " = " + ($2).lex + "\n" + ($2).lex + " = " + temp2 + " + 1\n";
($$)._3AC += "@label " + s + ":\n" + "ifz " + temp + " goto " + e + "\n" + ($7)._3AC + incr + "goto " + s + "\n" + "@label " + e + ":\n" + ($10)._3AC + "@label " + f + ":\n";
s_label.pop();
e_label.pop();
}
| FOR NAME IN ranges COLON loop_action suite {
($$)._3AC += ($4)._3AC;
auto st = CurrST->lookup(($2).lex);
($$)._3AC += ($2).lex + " = " + ($4).rng_start + "\n";
auto s = s_label.top();
auto e = e_label.top();
auto temp = newTemp();
($$)._3AC += "@label " + s + ":\n";
($$)._3AC += temp + " = " + ($2).lex + " < " + ($4).rng_end + "\n";
auto temp2 = newTemp();
string incr = temp2 + " = " + ($2).lex + "\n" + ($2).lex + " = " + temp2 + " + 1\n";
($$)._3AC += "ifz " + temp + " goto " + e + "\n" + ($7)._3AC + incr + "goto " + s + "\n" + "@label " + e + ":\n";
s_label.pop();
e_label.pop();
}
ranges : RANGE LEFTBRACKET test COMMA test RIGHTBRACKET {
($$).rng_start = ($3).lex;
($$).rng_end = ($5).lex;
($$)._3AC = ($3)._3AC + ($5)._3AC;
}
| RANGE LEFTBRACKET test RIGHTBRACKET {
($$).rng_start = "0";
($$).rng_end = ($3).lex;
($$)._3AC = ($3)._3AC;
}
suite: simple_stmt {
$$ = $1;
}
| NEWLINE INDENT stmt_help DEDENT {
$$ = $3;
}
stmt_help: stmt {
$$ = $1;
}
| stmt stmt_help {
// ($1).next = newLabel();
// ($2).next = ()
($$)._3AC = ($1)._3AC + ($2)._3AC;
}
test : and_test {
$$ = $1;
}
| test OR and_test {
if (($1).type != Bool_type && ($1).type != Int_type){
cerr << "Error! Type can only be integer or Boolean at line no. " <<yylineno<<endl;
exit(1); \
}
if (($3).type != Bool_type && ($3).type != Int_type){
cerr << "Error! Type can only be integer or Boolean at line no. " <<yylineno<<endl;
exit(1);
}
($$).type = Bool_type;
($$).lex = newTemp();
($$)._3AC = ($1)._3AC + ($3)._3AC + ($$.lex + " = " + ($1).lex + " || " + ($3).lex + "\n");
}
and_test : not_test {
$$ = $1;
}
| and_test AND not_test {
if (($1).type != Bool_type && ($1).type != Int_type){
cerr << "Error! Type can only be integer or Boolean at line no. " <<yylineno<<endl;
exit(1);
}
if (($3).type != Bool_type && ($3).type != Int_type){
cerr << "Error! Type can only be integer or Boolean at line no. " <<yylineno<<endl;
exit(1);
}
($$).type = Bool_type;
($$).lex = newTemp();
($$)._3AC = ($1)._3AC + ($3)._3AC + ($$.lex + " = " + ($1).lex + " && " + ($3).lex + "\n");
}
/* B →!B1
{B1.true = B.false;B1.false = B.true;B.code = B1.code;}
*/
not_test : NOT not_test {
if (($2).type != Bool_type && ($2).type != Int_type){
cerr << "Error! Type can only be integer or Boolean at line no. " <<yylineno<<endl;
exit(1);
}
($$).type = Bool_type;
($$).lex = newTemp();
($$)._3AC = ($2)._3AC + ($$.lex + " = " + "!" + ($2).lex + "\n");
}
| comparison {
$$ = $1;
}
comparison : expr {
$$ = $1;
}
| comparison comp_op expr {
bool string_comp = false;
string_comp = ($1).type == Str_type || ($3).type == Str_type;
if (!string_comp && ($1).type != Bool_type && ($1).type != Int_type && ($1).type != Float_type ){
cerr << "Error! Type can only be integer, Boolean or Float at line no. " <<yylineno<<endl;
exit(1);
}
if (!string_comp && ($3).type != Bool_type && ($3).type != Int_type && ($3).type != Float_type){
cerr << "Error! Type can only be integer, Boolean or Float at line no. " <<yylineno<<endl;
exit(1);
}
($$).type = Bool_type;
($$).lex = newTemp();
($$)._3AC = ($1)._3AC + ($3)._3AC + ($$.lex + " = " + ($1).lex + " " + ($2).lex + " " + ($3).lex + "\n");
}
comp_op : EQUAL {
($$).lex = "==";
}
| LESS {
($$).lex = "<";
}
| GREATER {
($$).lex = ">";
}
| GREATEREQ {
($$).lex = ">=";
}
| LESSEQ {
($$).lex = "<=";
}
| NOTEQUAL {
($$).lex = "!=";
}
| IS {
($$).lex = "is";
}
| IS NOT {
($$).lex = "is not"; // not defined anywhere
}
| IN {
($$).lex = "in";
}
| NOT IN {
($$).lex = "not in"; // not defined anywhere
}
expr : xor_expr {
$$ = $1;
}
| expr BITOR xor_expr {
//error expression
if(($1).type != Int_type){
cerr << "Error! Type can only be integer at line no." <<yylineno<<endl;
exit(1);
}
if (($3).type != Int_type){
cerr << "Error! Type can only be integer at line no. " <<yylineno<<endl;
exit(1);
}
($$).type=($1).type;
($$).lex=newTemp();
($$)._3AC = ($1)._3AC + ($3)._3AC + (($$).lex + " = " + ($1).lex + " | " + ($3).lex + "\n");
}
xor_expr: and_expr {
$$ = $1;
}
| xor_expr BITXOR and_expr {
//error expression
if(($1).type != Int_type){
cerr << "Error! Type can only be integer at line no." <<yylineno<<endl;
exit(1);
}
if (($3).type != Int_type){
cerr << "Error! Type can only be integer at line no. " <<yylineno<<endl;
exit(1);
}
($$).type=($1).type;
($$).lex=newTemp();
($$)._3AC = ($1)._3AC + ($3)._3AC + (($$).lex + " = " + ($1).lex + " ^ " + ($3).lex + "\n");
}
and_expr : shift_expr {
$$ = $1;
}
| and_expr AMPERSAND shift_expr {
//error expression
if(($1).type != Int_type){
cerr << "Error! Type can only be integer at line no." <<yylineno<<endl;
exit(1);
}
if (($3).type != Int_type){
cerr << "Error! Type can only be integer at line no. " <<yylineno<<endl;
exit(1);
}
($$).type=($1).type;
($$).lex=newTemp();
($$)._3AC = ($1)._3AC + ($3)._3AC + (($$).lex + " = " + ($1).lex + " & " + ($3).lex + "\n");
}
shift_expr : arith_expr {
$$ = $1;
}
| shift_expr LEFTSHIFT arith_expr {
if(($1).type != Int_type){
cerr << "Error! Type can only be integer at line no." <<yylineno<<endl;
exit(1);
}
if (($3).type != Int_type){
cerr << "Error! Type can only be integer at line no. " <<yylineno<<endl;
exit(1);