Skip to content
Official PYQ · 2003

GATE CS 2003 Q59 — Syntax-directed Translation

Explain with AI

Consider the syntax directed definition shown below.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;}\begin{array}{ll} S \rightarrow \mathbf{ id :=} E&\qquad \{gen(\mathbf{ id}.place = E.place;);\}\\ E \rightarrow E_1 + E_2 &\qquad \{t = newtemp();\\ &\qquad gen(t = E_1.place + E_2.place;);\\ &\qquad E.place = t;\}\\ E \rightarrow id&\qquad \{E.place = \mathbf{id}.place;\} \end{array}Here, gengen is a function that generates the output code, and newtempnewtemp is a function that returns the name of a new temporary variable on every call. Assume that ti′t_i's are the temporary variable names generated by newtempnewtemp. For the statement ‘X:=Y+Z’,\text{‘}X : = Y + Z\text{'}, the 33-address code sequence generated by this definition is

Choose one option
View worked solution

Answer: B

To find the 3-address code generated for the statement X:=Y+ZX := Y + Z, we construct the parse tree and evaluate the semantic actions bottom-up.

1. Parse Tree Construction

The parse tree for the expression X:=Y+ZX := Y + Z is:

  • S→id:=ES \rightarrow \mathbf{id} := E (where id\mathbf{id} is XX)
    • E→E1+E2E \rightarrow E_1 + E_2
      • E1→idE_1 \rightarrow \mathbf{id} (where id\mathbf{id} is YY)
      • E2→idE_2 \rightarrow \mathbf{id} (where id\mathbf{id} is ZZ)

2. Bottom-Up Evaluation of Semantic Actions

  1. For the leaf nodes:
    • E1→idE_1 \rightarrow \mathbf{id} (with id=Y\mathbf{id} = Y):
      E1.place=YE_1.place = Y
    • E2→idE_2 \rightarrow \mathbf{id} (with id=Z\mathbf{id} = Z):
      E2.place=ZE_2.place = Z
  2. For the production E→E1+E2E \rightarrow E_1 + E_2:
    • A new temporary variable is created: t1=newtemp()t_1 = newtemp()
    • The code generation function is called:
      gen(t1=E1.place+E2.place;)  ⟹  t1=Y+Z;gen(t_1 = E_1.place + E_2.place;) \implies \mathbf{t_1 = Y + Z;}
    • The place attribute is synthesized:
      E.place=t1E.place = t_1
  3. For the production S→id:=ES \rightarrow \mathbf{id} := E (with id=X\mathbf{id} = X):
    • The code generation function is called:
      gen(X=E.place;)  ⟹  X=t1;gen(X = E.place;) \implies \mathbf{X = t_1;}

3. Final Generated Code

Combining the generated statements in order of their execution, we get:

t1=Y+Z;X=t1;t_1 = Y + Z; \quad X = t_1;

This matches Option B.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Syntax-directed Translation questions