Three Address Code Generation Control Statements Scribe by
Three Address Code Generation - Control Statements Scribe by Rahul Panday (04 CS 3008)
Examples on control flow translation of boolean expersions • Example-1 • a < b or c < d and e <f • We first made a syntax tree from the given expression using the syntax directed definitions we learned in the last lecture.
The rules are: E -> E 1 or E 2 E -> E 1 and E 2 E -> not E 1 E -> (E 1) E -> id 1 relop id 2 E -> true E -> false
Syntax tree E E id a relop < or id E and E E b id relop c < id d id e relop id < f
• Going in a depth first manner and following the semantic rules we get: • if a < b goto Ltrue • goto L 1 • L 1: if c < d goto L 2 • goto Lfalse • L 2: if e < f goto Ltrue • goto Lfalse
• Example -2 • while a < b do If c < d then x=y+z else x=y-z
• Here the productions used are: • S -> if E then S 1 • { E. true = newlabel ; • E. false = S. next ; • S 1. next = S. next ; • S. code= E. code|| gen(E. true’: ’)||S 1. code }
• S -> if E then S 1 else S 2 • • • {E. true = newlabel; E. false = newlabel; S 1. next = S. next; S 2. next = S. next; S. code = E. code||gen(E. true’: ’)||S 1. code||gen(‘goto’S. next)|| gen(E. false’: ’)||S 2. code}
• S -> while E do S 1 • • • {S. begin = newlabel; E. true = newlabel; E. false = S. next; S 1. next = S. begin; S. code = gen(S. begin’: ’)||E. code||gen(E. true’: ’)||S 1. code ||gen(‘goto”S. begin)}
• Using the above rules and assignment statements we get the 3 address code as: • • • L 1: if a < b goto L 2 goto Lnext L 2: if c < d goto L 3 goto L 4 L 3: t 1 = y +z x = t 1 goto L 1 L 4: t 2 = y – z x = t 2 goto L 1 Lnext:
- Slides: 10