Chapter 8: Intermediate Code Generation

S ? if E then S 1

E.true = newlabel

E.false = S.next

S1 .next = S.next

S.code = E.code ||

gen(E.true ':') ||

S1 .code

Now we will consider the translation of boolean expressions into three address code generated by the following grammar:

S -> if E then S1

| if E then S1 else S2

| while E do S1

where E is the boolean expression to be translated.

Consider the following:

newlabel - returns a new symbolic label each time it is called.

E.true - the label to which control flows if E is true.

E.false - the label to which control flows if E is false.

For if-then

S -> if E then S1

E.true = newlabel      //generate a new label for E.true

E.false = S.next      //jump to S.next if E is false

S1.next = S.next

S.code = E.code || gen(E.true ':') || S1.code      // associate the label created for E.true with the

// three address code for the first statement for S1