B OOLEAN EXPRESSIONS are constructed using boolean operators. We will consider here the following rules.
E E E |
E E E |
E E |
E E |
E |
E |
E |
W HILE STATEMENTS WITH THE NUMERICAL METHOD.
The following syntax-directed translation generates code for a while statement.Production | Semantic Rule |
S E S 1 | S . begin := newlabel |
S . after := newlabel | |
code 1 := generate ( S . begin ' : ') | | E . code | | | |
code 2 := generate (' if ' E . place ' = 0 ' ' goto ' S . after ) | | S 1 . code | |
code 3 := generate (' goto ' S . begin ) | | generate ( S . after ) | |
S . code := code 1 | | code 2 | | code 3 |
F LOW OF CONTROL STATEMENTS WITH THE JUMP METHOD. We will consider here the following rules.
S | E S 1 |
S | E S 1 S 2 |
S | E S 1 |
Production | Semantic Rule |
S E S 1 | E . true := newlabel |
E . false := S . next | |
S 1 . next := S . next | |
S . code := E . code | | generate ( E . true ' : ') | | S 1 . code | |
S E S 1 S 2 | E . true := newlabel |
E . false := newlabel | |
S 1 . next := S . next | |
S 2 . next := S . next | |
code 1 := E . code | | generate ( E . true ' : ') | | S 1 . code | |
code 2 := generate (' goto ' S . next ) | | | |
code 3 := generate ( E . false ' : ') | | S 2 . code | |
S . code := code 1 | | code 2 | | code 3 | |
S E S 1 | S . begin := newlabel |
E . true := newlabel | |
E . false := S . next | |
S 1 . next := S . begin | |
code 1 := generate ( S . begin ' : ') | | E . code | |
code 2 := generate ( E . true ' : ') | | S 1 . code | |
code 3 := generate (' goto ' S . begin ) | |
S . code := code 1 | | code 2 | | code 3 |
T RANSLATION OF BOOLEAN EXPRESSIONS WITH THE JUMP METHOD. We will consider here the following rules.
E | E 1 E 2 |
E | E 1 E 2 |
E | E 1 |
E | ( E 1 ) |
E | |
E | |
E |
Production | Semantic Rule |
E E 1 E 2 | E 1 . true := E . true |
E 1 . false := newlabel | |
E 2 . true := E . true | |
E 2 . false := E . false | |
E . code := E 1 . code | | generate ( E 1 . false ' : ') | | E 2 . code | |
E E 1 E 2 | E 1 . true := newlabel |
E 1 . false := E . false | |
E 2 . true := E . true | |
E 2 . false := E . false | |
E . code := E 1 . code | | generate ( E 1 . true ' : ') | | E 2 . code | |
E E 1 | E 1 . true := E . false |
E 1 . false := E . true | |
E . code := E 1 . code | |
E ( E 1 ) | E 1 . true := E . true |
E 1 . false := E . false | |
E . code := E 1 . code | |
E | code 1 := generate (' if ' . place . place ' goto ' E . true ) |
code 2 := generate (' goto ' E . false ) | |
E . code := code 1 | | code 2 | |
E | E . code := generate (' goto ' E . true ) |
E | E . code := generate (' goto ' E . false ) |
if a < b goto Ltrue |
goto Lfalse |
Again assume that the labels Ltrue and Lfalse have been set for the entire expression. Then the translation is
if a < b goto Ltrue | |
goto L1 | |
L1: | if c < d goto Ltrue |
goto Lfalse |
if a < b goto Ltrue | |
goto L1 | |
L1: | if c < d goto L2 |
goto Lfalse | |
L2: | if e < f goto Ltrue |
goto Lfalse |
Of course the generated code is not optimal! Indeed the second statement can be eliminated.
Example 8 Finally consider the expressionwhile a < b do if c < d then x := y + z else x := y - zThen the translation is
L1: | if a < b goto L2 |
goto Lnext | |
L2: | if c < d goto L3 |
goto L4 | |
L3: | t1 := y + z |
x := t1 | |
goto L1 | |
L4: | t2 := y - z |
x := t2 | |
goto L1 | |
Lnext: |