语法制导的语义翻译
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-T | T
E->E1+E2{E.place:=newtemp;
emit(E.place,’:=’,E1.place’+’,E2.place) }
(+, E1.val ,T1.val,E.val)
E->E1+E2{E.place:=newtemp;
emit(E.place,’:=’,E1.place’-’,E2.place) }
(-, E1.val ,T1.val,E.val)
E→T1 {E.place:=T1.place;}
T→T1* F{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}