next up previous
Next: Exercise 6. Up: Final-13-04-2004 Previous: Exercise 4.

Exercise 5.

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
Some languages allow repeat-until loop statements of the form
    repeat {
         s1;
    } 
    until (e);
where s1 is a statement and e is a boolean expression. (Iterations of the loop body are executed until e becomes true. Construct a syntax-directed definition for the translation of these repeat-until loop statements into three-address code.

Answer 5  


next up previous
Next: Exercise 6. Up: Final-13-04-2004 Previous: Exercise 4.
Marc Moreno Maza
2004-12-02