Compiler theory learning (16) of semantic syntax-directed translation

1. The syntax grammar G [E] is as follows: 

–E→E+T | E-T | T 

–T→T* F | T/F | F 

–F→P^ F | P 

–P→(E) | i 

  • Requirements constructed attribute grammar analysis requirements described semantically

 

solution:

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.place := newtemp; emit( E.place , ' := ' , 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 , ' := ' , F.place )}

F -> P^F      { F.place := newtemp; emit( F.place , ' := ' , P.place , ' ^ ' , F.place )}

F -> P         { P.place := newtemp; emit( F.place , ' := ' , P.place )}

P -> (E)       { P.place :=  E.place;}

P -> i           { if  i <> nil   then  emit( P.place , ':=' , i.place )   else error}

 

 

2. (OPTIONAL) third experiment: Syntax-Semantics translator

Claim:

  • To achieve the above expression grammar analysis by operator priority syntax-directed translation process.
  • Upon completion of the second experiment (operator priority syntax analysis) on the semantic analysis program design.
  • The final output is equivalent to the test Quaternion intermediate code sequence.

Such as

Input: a + b * c, the output

(*,b,c,T1)

(+,a,T1,T2)

Input: b * (c + b) * d, the output

(*,b,c,T1)

(*,b,d,T2)

(+, T1, T2, T3)

 

Guess you like

Origin www.cnblogs.com/xyqzzz/p/12109585.html