GATE / CS ARCHIVE

GATE CSE 2003 / QUESTION 59

Three-address code from synthesized expression locations

Compiler Design · Intermediate Code · Full GATE CSE 2003 paper

GATE 2003, Q59 2 marks · MCQ
Report issue

Intermediate Code

Question 59, GATE 2003: A syntax-directed definition uses:
S→id := E {gen(id.place '=' E.place);}
E→E1+E2 {t=newtemp(); gen(t '=' E1.place '+' E2.place); E.place=t;}
E→id {E.place=id.place;}
gen emits code and newtemp returns a fresh temporary t1,t2,... on each call. For X=Y+Z, what three-address code is generated?
A. X=Y+Z
B. t1=Y+Z; X=t1
C. t1=Y; t2=t1+Z; X=t2
D. t1=Y; t2=Z; t3=t1+t2; X=t3
Tap to enlarge
Your answer
Answer & solution

Answer: B (no official key was published)

1. Reduce identifier Y using E→id: set its place to Y without generating an instruction. Reduce Z the same way, with place Z.
2. At the addition node, call newtemp once to obtain t1. Emit t1=Y+Z and make the addition expression's place t1.
3. At the assignment node, use X as the destination and the completed expression place as source. Emit X=t1.
4. The ordered output is precisely B. No identifier-copy actions occur in the definition, ruling out C and D; the addition action cannot be bypassed to get A.

Solution: An identifier expression uses the identifier’s existing place and emits no copy. The addition allocates one temporary and emits t₁=Y+Z, after which its place is t₁. The assignment then emits X=t₁. Thus the two-instruction sequence in B follows directly; no temporaries are needed merely to refer to Y and Z.

Original paper, page 8