next up previous
Next: Exercise 7. Up: Final-2003 Previous: Exercise 5.

Exercise 6.

We recall the syntax-directed definition for the translation of flow of control statements into three-address code, using the jump method with symbolic labels. In each of the above productions, E is a boolean expression to be translated. This boolean expression E is associated with two labels (which are inherited attributes) Similarly, S is a flow of control statement associated with 2 or 3 attributes among
Production Semantic Rule
S  $ \longmapsto$  $ \bf if$ E $ \bf then$ S1 E.true := newlabel
  E.false := S.next
  S1.next := S.next
  S.code := E.code | | generate(E.true ':') | | S1.code
S  $ \longmapsto$  $ \bf if$ E $ \bf then$ S1 $ \bf else$ S2 E.true := newlabel
  E.false := newlabel
  S1.next := S.next
  S2.next := S.next
  code1 := E.code | | generate(E.true ':') | | S1.code
  code2 := generate('goto' S.next) | |
  code3 := generate(E.false ':') | | S2.code
  S.code := code1 | | code2 | | code3
S  $ \longmapsto$  $ \bf while$ E $ \bf repeat$ S1 S.begin := newlabel
  E.true := newlabel
  E.false := S.next
  S1.next := S.begin
  code1 := generate(S.begin ':') | | E.code
  code2 := generate(E.true ':') | | S1.code
  code3 := generate('goto' S.begin)
  S.code := code1 | | code2 | | code3
In C, the for-statement has the following form.
    for ( s1 ; e ; s2 ) s3
Taking its meaning to be
    s1;
    while ( e ) {
         s3 ;
         s2 ;
    }
construct a syntax-directed definition to translate C-style for-statements into three-address code.

Answer 6  
\fbox{
\begin{minipage}{12 cm}
\mbox{ } \\
\mbox{ } \\
\mbox{ } \\
\mbox{ } \...
...\mbox{ } \\
\mbox{ } \\
\mbox{ } \\
\mbox{ } \\
\mbox{ } \\
\end{minipage}}


next up previous
Next: Exercise 7. Up: Final-2003 Previous: Exercise 5.
Marc Moreno Maza
2004-12-02