BuZhiDaoen 2019-12-18
1.语法文法G[E]如下所示:
E→E+T | E-T | T
T→T* F | T/F | F
F→P^ F | P
P→(E) | i
E→E1+T1 {E.place:=newtemp; emit(E.place , ":=" ,E1.place,‘+‘ , T1.place)} (+, E1.val ,T1.val,E.val)
E→E1-T1 {E.place:=newtemp; emit(E.place , ":=" ,E1.place,‘-‘ , T1.place)} (-, E1.val ,T1.val,E.val)
E→T1 {E.place:=T1.place;}
T→T1* F1 {T.place:=newtemp; emit(T.place , ":=" ,T1.place,‘*‘ , F1.place)} (*, T1.val ,F1.val,T.val)
T→T1/F1 {T.place:=newtemp; emit(T.place , ":=" , T1.place,‘/‘ , F1.place)} (/, T1.val ,F1.val,T.val)
T→F1 {T.place:=F1.place;}
F→P1^ F1 {T.place:=newtemp; emit(F.place , ":=" , P1.place,‘^‘ , F1.place)}(^, P1.val ,F1.val,F.val)
F→P1 {F.place:=newtemp; emit(F.place , ":=" , P1.place)}
P→(E1) {P.place:=E1.place}
P→i {P:=lookup(id.name); if P<>nil then P.place:=i else error}
2.(选做)实验三:语法制导的语义翻译程序
要求:
如
输入:a+b*c,则输出
(*,b,c,T1)
(+,a,T1,T2)
输入:b*(c+b)*d,则输出
(*,b,c,T1)
(*,b,d,T2)
(+,T1,T2,T3)