其他分享
首页 > 其他分享> > 语法制导的语义翻译

语法制导的语义翻译

作者:互联网

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-T  {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(E.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  {F.place := newtemp; emit(F.place, ":=", P1.place '^', F1.plcae)}  (^, 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)

标签:F1,val,E1,语义,T1,语法,newtemp,place,制导
来源: https://www.cnblogs.com/q1uj1e/p/12095740.html