1.语法文法G[E]如下所示:
E→E+T | E-T | T
T→T* F | T/F | F
F→P^ F | P
P→(E) | i
解析:
E -> E+T {E.place:=newtemp; emit(E.place,‘:=‘,E.place‘+‘,T.place)}
E -> E-T {E.place:=newtemp; emit(E.place,‘:=‘,E.place‘-‘,T.place)}
E -> T {E.p;ace:=newtemp; emit(E.place,‘:=‘,‘uminus‘,T.place)}
T -> T*F {T.place:=newtemp; emit(T.place,‘:=‘,T.place‘*‘,F.place)}
T -> T/F {T.place:=newtemp; emit(T.place,‘:=‘,T.place‘/‘,F.place)}T -> F {T.place:=newtemp; emit(T.place,‘;=‘,‘uminus‘,F.place)}
F -> P^F{F.place:=newtemp; emit(F.place,‘:=‘,P.place‘^‘,F.place)}
F -> P {F.place:=newtemp; emit(F.place,‘:=’,‘uminus‘,P.palce)}
P -> (E) {P.place:=E.palce;}
P -> i {if i<>nil then emit(P.place, ‘:=‘, i.place) 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)
原文:https://www.cnblogs.com/zhif97/p/12094740.html