forked from ZhenbangYou/SysY-Front-End
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_without_inherited_attributes.y
975 lines (926 loc) · 30.5 KB
/
parser_without_inherited_attributes.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
%{
#include <stdio.h>
#include <iostream>
#include <string>
#include <unordered_map>
#include <cassert>
#include <vector>
#include <deque>
using namespace std;
#define YYSTYPE void *
extern FILE* yyin;
extern FILE* yyout;
extern int yylex();
extern int yylineno;
class WhileLoop;
class Function;
void yyerror(const char *msg)
{
cerr<<"line: "<<yylineno<<"\t" << msg << endl;
exit(1);
}
string indent;
bool TimerOn=false;
const int INT_SIZE=4;
vector<string>pre_eeyore;
void emit(string s)
{
//cout << indent << s << endl;
pre_eeyore.push_back(s);
}
class Var
{
public:
static int count;
int SeqNo;
bool is_param;
bool is_const;
int value;//valid only when is_const==true
deque<int>shape;//judge whether being an array from the size of "shape"
vector<int>element_value;//valid only when this is an array
bool is_access;//whether being array access
Var* array_name;//valid only when is_access==true
Var* offset;//valid only when is_access==true
Var() : SeqNo(count++),is_param(false),is_const(false),is_access(false)
{
decl();
}
Var(int seq):SeqNo(seq),is_param(true),is_const(false),is_access(false){}
Var(bool IsConst,int v):
is_const(IsConst),value(v),is_param(false),SeqNo(-1),is_access(false){}
Var(bool IsConst,deque<int>*dq,
bool IsParam,int seq=-1,
bool IsAccess=false,Var *ArrayName=NULL,Var *Off=NULL):
is_const(IsConst),
is_param(IsParam),
is_access(IsAccess),array_name(ArrayName),offset(Off)
{
if(dq)
shape=*dq;
if(is_access==false)
{
if(is_param)
SeqNo=seq;
else if(is_const==false || dq->size()>0)
{
SeqNo=count++;
if(dq->size()>0)
decl_array();
else
decl();
}
element_value=vector<int>(size(),0);
}
}
bool is_array()
{
return shape.size()!=0;
}
int size()
{
int ans=1;
for(int i:shape)
ans*=i;
return ans;
}
deque<int>* size_of_each_dimension()
{
deque<int>*ans=new deque<int>;
ans->push_front(INT_SIZE);
for(int i=shape.size()-1;i>=1;i--)
ans->push_front(ans->front()*shape[i]);
return ans;
}
string getname()
{
if(is_const&&is_array()==false)
return to_string(value);
else if(is_access)
{
return array_name->getname()+"["+offset->getname()+"]";
}
else if(!is_param)
return "T"+to_string(SeqNo);
else
return "p"+to_string(SeqNo);
}
void decl()
{
emit("var " + getname());
}
void decl_array()
{
emit("var " + to_string(size()*INT_SIZE) + " " + getname());
}
};
int Var::count = 0;
class Env
{
public:
unordered_map<string, Var *> var_table;
Env *prev;
bool is_param;
Env(Env *n,bool IsParam) : prev(n),is_param(IsParam) {}
void put(string name, Var *p)
{
assert(p != NULL);
if(var_table.find(name)!=var_table.end())
yyerror("variable redefined");
if(prev!=NULL && prev->is_param==true)
if(prev->var_table.find(name)!=prev->var_table.end())
yyerror("function parameter shadowed");
var_table.insert(make_pair(name, p));
}
Var *get(string name)
{
Env *n=this;
while(n!=NULL)
{
auto found=n->var_table.find(name);
if(found!=n->var_table.end())
return found->second;
else
n=n->prev;
}
return NULL;
}
};
class Parser
{
public:
Env *top;
vector<WhileLoop*>while_stack;
unordered_map<string,Function*>func_table;
Parser() : top(new Env(NULL,false)) {}
void NewEnv(bool IsParam)
{
top = new Env(top,IsParam);
}
void DeleteEnv()
{
top = top->prev;
}
void PutFunc(string name,Function*f)
{
if(func_table.find(name)!=func_table.end())
yyerror("function redefined\n");
func_table.insert(make_pair(name,f));
}
Function* GetFunc(string name)
{
auto found=func_table.find(name);
if(found==func_table.end())
return NULL;
return found->second;
}
};
Parser parser;
int NewLabel()
{
static int labels=0;
return labels++;
}
void emitLabel(int i)
{
emit("l"+to_string(i)+":");
}
class IfStmt
{
public:
int True;
int False;
int After;
IfStmt(int t,int f,int a):True(t),False(f),After(a){}
};
class WhileLoop
{
public:
int Begin;
int Body;
int After;
WhileLoop(int be,int bo,int a):Begin(be),Body(bo),After(a){}
};
class Function
{
public:
int param_count;
int retval;
Function(int p,int rv):param_count(p),retval(rv){}
};
class Initializer
{
public:
Var*var_to_init;
deque<int>batch_size;
int pos;
int batch_size_index;
void compute_batch_size()
{
batch_size=var_to_init->shape;
if(var_to_init->is_array())
for(int i=batch_size.size()-2;i>=0;i--)
batch_size[i]*=batch_size[i+1];
}
void set(Var*var)
{
var_to_init=var;
compute_batch_size();
pos=0;
batch_size_index=-1;
}
bool is_array()
{
return batch_size.size()>0;
}
int get_batch_size()
{
return batch_size[batch_size_index];
}
};
Initializer initializer;
%}
%token INT CONST VOID
%token IF ELSE WHILE BREAK CONTINUE RETURN
%token AND OR EQ NE LE GE
%token IDENT INT_CONST
%%
CompUnits : CompUnits CompUnit
|
;
CompUnit : Decl
| FuncDef
;
Decl : ConstDecl
| VarDecl
;
ConstDecl : CONST INT ConstDefs ';'
;
ConstDefs : ConstDefs ',' ConstDef
| ConstDef
;
ConstDef : IDENT ConstExpList
{
string name=*(string*)$1;
if(((deque<int>*)$2)->size()==0)//scalar variable
$$=new Var(true,0);
else//array variable
$$=new Var(true,(deque<int>*)$2,false);
parser.top->put(name,(Var*)$$);
initializer.set((Var*)$$);
}
'=' ConstInitVal
;
ConstExpList : ConstExpList '[' ConstExp ']'
{
$$=$1;
((deque<int>*)$$)->push_back(((Var*)$3)->value);
}
| {$$=new deque<int>;}
;
ConstInitVal : ConstExp
{
if(initializer.is_array())
{
emit(initializer.var_to_init->getname()+
"["+to_string(initializer.pos*INT_SIZE)+"]="+((Var*)$1)->getname());
initializer.var_to_init->element_value[initializer.pos]=((Var*)$1)->value;
initializer.pos++;
}
else
initializer.var_to_init->value=((Var*)$1)->value;
}
| '{'
{
initializer.batch_size_index++;
}
ConstInitVals '}'
{
for(;initializer.pos%initializer.get_batch_size()!=0;initializer.pos++)
emit(initializer.var_to_init->getname()+
"["+to_string(initializer.pos*INT_SIZE)+"]=0");
initializer.batch_size_index--;
}
| '{' '}'
{
initializer.batch_size_index++;
for(int i=0;i<initializer.get_batch_size();i++)
{
emit(initializer.var_to_init->getname()+
"["+to_string(initializer.pos*INT_SIZE)+"]=0");
initializer.pos++;
}
initializer.batch_size_index--;
}
;
ConstInitVals : ConstInitVals ',' ConstInitVal
| ConstInitVal
;
VarDecl : INT VarDefs ';'
;
VarDefs : VarDefs ',' VarDef
| VarDef
;
VarDef : IDENT ConstExpList {
string name=*(string*)$1;
if(((deque<int>*)$2)->size()==0)//scalar variable
{
$$=new Var();
if(parser.top->prev==NULL)//global variable
emit(((Var*)$$)->getname()+"=0");
}
else//array variable
{
$$=new Var(false,(deque<int>*)$2,false);
if(parser.top->prev==NULL)//global variable
{
int size=((Var*)$$)->size();
for(int i=0;i<size;i++)
emit(((Var*)$$)->getname()+"["+to_string(i*4)+"]=0");
}
}
parser.top->put(name,(Var*)$$);
}
| IDENT ConstExpList {
string name=*(string*)$1;
if(((deque<int>*)$2)->size()==0)//scalar variable
$$=new Var();
else//array variable
$$=new Var(false,(deque<int>*)$2,false);
parser.top->put(name,(Var*)$$);
initializer.set((Var*)$$);
}
'=' InitVal
;
InitVal : Exp
{
if(initializer.is_array())
{
emit(initializer.var_to_init->getname()+
"["+to_string(initializer.pos*INT_SIZE)+"]="+((Var*)$1)->getname());
initializer.pos++;
}
else
emit(initializer.var_to_init->getname()+"="+((Var*)$1)->getname());
}
| '{'
{
initializer.batch_size_index++;
}
InitVals '}'
{
for(;initializer.pos%initializer.get_batch_size()!=0;initializer.pos++)
emit(initializer.var_to_init->getname()+
"["+to_string(initializer.pos*INT_SIZE)+"]=0");
initializer.batch_size_index--;
}
| '{' '}'
{
initializer.batch_size_index++;
for(int i=0;i<initializer.get_batch_size();i++)
{
emit(initializer.var_to_init->getname()+
"["+to_string(initializer.pos*INT_SIZE)+"]=0");
initializer.pos++;
}
initializer.batch_size_index--;
}
;
InitVals : InitVals ',' InitVal
| InitVal
;
FuncDef : INT IDENT '('
{
parser.NewEnv(true);
}
FuncFParams ')'
{
string name=*(string*)$2;
int param_count=*(int*)$5;
emit("f_"+name+" ["+to_string(param_count)+"]");
Function*func=new Function(param_count,INT);
parser.PutFunc(name,func);
}
Block
{
parser.DeleteEnv();
emit("return 0");
string name=*(string*)$2;
emit("end f_"+name);
}
| VOID IDENT '('
{
parser.NewEnv(true);
}
FuncFParams ')'
{
string name=*(string*)$2;
int param_count=*(int*)$5;
emit("f_"+name+" ["+to_string(param_count)+"]");
Function*func=new Function(param_count,VOID);
parser.PutFunc(name,func);
}
Block
{
parser.DeleteEnv();
emit("return");
string name=*(string*)$2;
emit("end f_"+name);
}
| INT IDENT '(' ')'
{
string name=*(string*)$2;
int param_count=0;
emit("f_"+name+" ["+to_string(param_count)+"]");
Function*func=new Function(param_count,INT);
parser.PutFunc(name,func);
}
Block
{
emit("return 0");
string name=*(string*)$2;
emit("end f_"+name);
}
| VOID IDENT '(' ')'
{
string name=*(string*)$2;
int param_count=0;
emit("f_"+name+" ["+to_string(param_count)+"]");
Function*func=new Function(param_count,VOID);
parser.PutFunc(name,func);
}
Block
{
emit("return");
string name=*(string*)$2;
emit("end f_"+name);
}
;
FuncFParams : FuncFParams ',' INT IDENT '[' ']' ConstExpList
{
$$=$1;
(*(int*)$$)++;
deque<int>*shape=(deque<int>*)$7;
shape->push_front(0);
parser.top->put(*(string*)$4,new Var(false,shape,true,(*(int*)$$)-1));
}
| FuncFParams ',' INT IDENT
{
$$=$1;
(*(int*)$$)++;
parser.top->put(*(string*)$4,new Var((*(int*)$$)-1));
}
| INT IDENT '[' ']' ConstExpList
{
$$=new int;
(*(int*)$$)=1;
deque<int>*shape=(deque<int>*)$5;
shape->push_front(0);
parser.top->put(*(string*)$2,new Var(false,shape,true,(*(int*)$$)-1));
}
| INT IDENT
{
$$=new int;
(*(int*)$$)=1;
parser.top->put(*(string*)$2,new Var((*(int*)$$)-1));
}
;
Block : '{'
{
parser.NewEnv(false);indent.push_back('\t');
}
BlockItems
{
parser.DeleteEnv();indent.pop_back();
}
'}'
;
BlockItems : BlockItems BlockItem
|
;
BlockItem : Decl
| Stmt
;
Stmt : LVal '=' Exp ';'
{
emit(((Var*)$1)->getname()+"="+((Var*)$3)->getname());
}
| Exp ';'
| ';'
| Block
| IF '(' Cond ')'
{
$1=new IfStmt(*(int*)$3,NewLabel(),NewLabel());
emit("goto l"+to_string(((IfStmt*)$1)->False));
emitLabel(((IfStmt*)$1)->True);
}
Stmt
{
emit("goto l"+to_string(((IfStmt*)$1)->After));
emitLabel(((IfStmt*)$1)->False);
}
DanglingElse
{
emitLabel(((IfStmt*)$1)->After);
}
| WHILE
{
$1=new WhileLoop(NewLabel(),0,NewLabel());
parser.while_stack.push_back((WhileLoop*)$1);
emitLabel(((WhileLoop*)$1)->Begin);
}
'(' Cond ')'
{
emit("goto l"+to_string(((WhileLoop*)$1)->After));
((WhileLoop*)$1)->Body=*(int*)$4;
emitLabel(((WhileLoop*)$1)->Body);
}
Stmt
{
emit("goto l"+to_string(((WhileLoop*)$1)->Begin));
emitLabel(((WhileLoop*)$1)->After);
parser.while_stack.pop_back();
}
| BREAK ';'
{
if(parser.while_stack.size()==0)
yyerror("No While");
emit("goto l"+to_string(parser.while_stack.back()->After));
}
| CONTINUE ';'
{
if(parser.while_stack.size()==0)
yyerror("No While");
emit("goto l"+to_string(parser.while_stack.back()->Begin));
}
| RETURN Exp ';'
{
emit("return "+((Var*)$2)->getname());
}
| RETURN ';' {emit("return");}
;
DanglingElse : ELSE Stmt
|
;
Exp : AddExp {$$=$1;}
;
Cond : LOrExp {$$=$1;}
;
LVal : IDENT ExpList
{
string name=*(string*)$1;
$$=parser.top->get(name);
deque<Var*>&subscripts=*(deque<Var*>*)$2;
if(subscripts.size()>0)
{
deque<int>&size_of_each_dimension=
*(((Var*)$$)->size_of_each_dimension());
bool all_const=true;
for(Var*i:subscripts)
if(i->is_const==false)
all_const=false;
if(all_const)
{
int offset=0;
for(int i=0;i<subscripts.size();i++)
offset+=(subscripts[i]->value)*size_of_each_dimension[i];
if(((Var*)$$)->is_const
&&subscripts.size()==size_of_each_dimension.size())
$$=new Var(true,((Var*)$$)->element_value[offset/INT_SIZE]);
else
{
Var*var_offset=new Var(true,offset);
if(subscripts.size()==size_of_each_dimension.size())
$$=new Var(false,NULL,false,-1,true,(Var*)$$,var_offset);
else
{
Var*tmp=new Var();
emit(tmp->getname()+"="+tmp->getname()+" + "+var_offset->getname());
$$=tmp;
}
}
}
else
{
Var*var_offset=new Var();
emit(var_offset->getname()+"=0");
for(int i=0;i<subscripts.size();i++)
{
Var*mul=new Var();
emit(mul->getname()+"="
+subscripts[i]->getname()+
" * "+to_string(size_of_each_dimension[i]));
emit(var_offset->getname()+"="
+var_offset->getname()+" + "+mul->getname());
}
if(subscripts.size()==size_of_each_dimension.size())
$$=new Var(false,NULL,false,-1,true,(Var*)$$,var_offset);
else
{
Var*tmp=new Var();
emit(tmp->getname()+"="+((Var*)$$)->getname()+" + "+var_offset->getname());
$$=tmp;
}
}
}
}
;
ExpList : ExpList '[' Exp ']'
{
$$=$1;
((deque<Var*>*)$$)->push_back((Var*)$3);
}
| {$$=new deque<Var*>;}
;
PrimaryExp : '(' Exp ')' {$$=$2;}
| LVal
{
if(((Var*)$1)->is_access)
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname());
}
else
$$=$1;
}
| INT_CONST { $$=new Var(true,*(int*)$1); }
;
UnaryExp : PrimaryExp {$$=$1;}
| IDENT '(' FuncRParams ')'
{
string name=*(string*)$1;
auto found=parser.GetFunc(name);
if(found==NULL)
yyerror("function undefined");
vector<Var*>&v=*(vector<Var*>*)$3;
for(int i=0;i<v.size();i++)
emit("param "+v[i]->getname());
int retval=found->retval;
if(retval==INT)
{
$$=new Var();
emit(((Var*)$$)->getname()+"=call f_"+name);
}
else if(retval==VOID)
emit("call f_"+name);
}
| IDENT '(' ')'
{
string name=*(string*)$1;
if(name=="starttime")
{
if(TimerOn)
yyerror("timer has been on");
TimerOn=true;
emit("param "+to_string(yylineno));
emit("call f__sysy_starttime");
}
else if(name=="stoptime")
{
if(!TimerOn)
yyerror("no timer yet");
TimerOn=false;
emit("param "+to_string(yylineno));
emit("call f__sysy_stoptime");
}
else
{
auto found=parser.GetFunc(name);
if(found==NULL)
yyerror("function undefined");
int retval=found->retval;
if(retval==INT)
{
$$=new Var();
emit(((Var*)$$)->getname()+"=call f_"+name);
}
else if(retval==VOID)
emit("call f_"+name);
}
}
| '+' UnaryExp {$$=$2;}
| '-' UnaryExp
{
if(((Var*)$2)->is_const)
$$=new Var(true,-(((Var*)$2)->value));
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"= - "+((Var*)$2)->getname());
}
}
| '!' UnaryExp
{
if(((Var*)$2)->is_const)
$$=new Var(true,!(((Var*)$2)->value));
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"= ! "+((Var*)$2)->getname());
}
}
;
FuncRParams : FuncRParams ',' Exp
{
$$=$1;
((vector<Var*>*)$$)->push_back((Var*)$3);
}
| Exp
{
$$=new vector<Var*>;
((vector<Var*>*)$$)->push_back((Var*)$1);
}
;
MulExp : UnaryExp {$$=$1;}
| MulExp '*' UnaryExp
{
if(((Var*)$1)->is_const&&((Var*)$3)->is_const)
$$=new Var(true,((Var*)$1)->value*((Var*)$3)->value);
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" * "+((Var*)$3)->getname());
}
}
| MulExp '/' UnaryExp
{
if(((Var*)$1)->is_const&&((Var*)$3)->is_const)
$$=new Var(true,((Var*)$1)->value/((Var*)$3)->value);
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" / "+((Var*)$3)->getname());
}
}
| MulExp '%' UnaryExp
{
if(((Var*)$1)->is_const&&((Var*)$3)->is_const)
$$=new Var(true,((Var*)$1)->value%((Var*)$3)->value);
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" % "+((Var*)$3)->getname());
}
}
;
AddExp : MulExp {$$=$1;}
| AddExp '+' MulExp
{
if(((Var*)$1)->is_const&&((Var*)$3)->is_const)
$$=new Var(true,((Var*)$1)->value+((Var*)$3)->value);
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" + "+((Var*)$3)->getname());
}
}
| AddExp '-' MulExp
{
if(((Var*)$1)->is_const&&((Var*)$3)->is_const)
$$=new Var(true,((Var*)$1)->value-((Var*)$3)->value);
else
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" - "+((Var*)$3)->getname());
}
}
;
RelExp : AddExp {$$=$1;}
| RelExp '<' AddExp
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" < "+((Var*)$3)->getname());
}
| RelExp '>' AddExp
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" > "+((Var*)$3)->getname());
}
| RelExp LE AddExp
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" <= "+((Var*)$3)->getname());
}
| RelExp GE AddExp
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" >= "+((Var*)$3)->getname());
}
;
EqExp : RelExp {$$=$1;}
| EqExp EQ RelExp
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" == "+((Var*)$3)->getname());
}
| EqExp NE RelExp
{
$$=new Var();
emit(((Var*)$$)->getname()+"="+((Var*)$1)->getname()+" != "+((Var*)$3)->getname());
}
;
LAndExp : EqExp
{
$$=new int;//false label
*(int*)$$=NewLabel();
emit("if "+((Var*)$1)->getname()+"== 0 goto l"+to_string(*(int*)$$));
}
| LAndExp AND EqExp
{
$$=$1;//false label
emit("if "+((Var*)$3)->getname()+"== 0 goto l"+to_string(*(int*)$$));
}
;
LOrExp : LAndExp
{
$$=new int;//true label
*(int*)$$=NewLabel();
emit("goto l"+to_string(*(int*)$$));
emitLabel(*(int*)$1);//false label
}
| LOrExp OR LAndExp
{
$$=$1;//true label
emit("goto l"+to_string(*(int*)$$));
emitLabel(*(int*)$3);//false label
}
;
ConstExp : AddExp
{
assert(((Var*)$1)->is_const);
$$=$1;
}
;
%%
void output(const string&s)
{
cout<<s<<endl;
}
bool is_fun_header(const string & s)
{
return s.substr(0,2)=="f_";
}
bool is_fun_end(const string & s)
{
return s.substr(0,3)=="end";
}
bool is_var_def(const string & s)
{
return s.substr(0,3)=="var";
}
void to_eeyore(const vector<string>&instructions)
{
vector<string>global_init;
//definitions of global variables
bool is_global=true;
for(auto&i:instructions)
{
if(is_fun_header(i))
is_global=false;
else if(is_fun_end(i))
is_global=true;
else if(is_global&&is_var_def(i))
output(i);
else if(is_global&&!is_var_def(i))
global_init.push_back(i);
}
//definitions of functions
is_global=true;
for(auto i=instructions.begin(),j=instructions.begin();i!=instructions.end();i++)
if(is_fun_header(*i))
{
for(j=i+1;!is_fun_end(*j);j++);
output(*i);
//local definitions
for(auto k=i+1;k!=j;k++)
if(is_var_def(*k))
output(*k);
//special check for "main"
if(i->substr(2,5)=="main ")//don't forget the following space
for(auto k:global_init)
output(k);
//other local things
for(auto k=i+1;k!=j;k++)
if(!is_var_def(*k))
output(*k);
output(*j);
i=j;
}
}
int main(int argc,char**argv)
{
if(argc>=4)
if((yyin=fopen(argv[3],"r"))==NULL)
exit(1);
if(argc>=6)
if(freopen(argv[5],"w",stdout)==NULL)
exit(1);
parser.PutFunc("getint",new Function(0,INT));
parser.PutFunc("getch",new Function(0,INT));
parser.PutFunc("getarray",new Function(1,INT));
parser.PutFunc("putint",new Function(1,VOID));
parser.PutFunc("putch",new Function(1,VOID));
parser.PutFunc("putarray",new Function(2,VOID));
yyparse();
to_eeyore(pre_eeyore);
fclose(yyin);
}