Skip to content
Official PYQ · 2004

GATE CS 2004 Q45 — Syntax-directed Translation

Explain with AI

Consider the grammar with the following translation rules and EE as the start symbolE→E1# T{E.value=E1.value∗T.value}∣T{E.value=T.value}T→T1& F{T.value=T1.value+F.value}∣F{T.value=F.value}F→num{F.value=num.value}\begin{array}{lll} E \rightarrow E_ 1\# \: T & \qquad\left\{E.value = E_1.value * T.value\right\}\\ \qquad\mid T & \qquad \{E.value = T.value\}\\ T \rightarrow T_1 \& \: F &\qquad \{T.value = T_1.value + F.value\}\\ \qquad\mid F&\qquad \{T.value = F.value\}\\ F \rightarrow \text{num}&\qquad \{F.value=num.value\} \end{array}Compute E.value for the root of the parse tree for the expression:22 # 33 & 55 # 66 & 44

Choose one option
View worked solution

Answer: C

To find the value of the expression 2#3&5#6&42 \# 3 \& 5 \# 6 \& 4, we first determine the precedence, associativity, and operations associated with the operators #\# and &\& from the given syntax-directed translation (SDT) rules.

1. Precedence and Associativity

  • Precedence: Since E→E#TE \rightarrow E \# T and E→TE \rightarrow T, the operator #\# is evaluated at a higher level in the parse tree (closer to the root), meaning it has lower precedence. The operator &\& is defined under T→T&FT \rightarrow T \& F, meaning it has higher precedence.
  • Associativity: Both productions E→E1#TE \rightarrow E_1 \# T and T→T1&FT \rightarrow T_1 \& F are left-recursive, which means both operators #\# and &\& are left-associative.

2. Associated Operations

  • The rule for #\# is E.value=E1.value∗T.valueE.value = E_1.value * T.value, which means #\# performs multiplication (∗*).
  • The rule for &\& is T.value=T1.value+F.valueT.value = T_1.value + F.value, which means &\& performs addition (++).

3. Evaluation of the Expression

The given expression is: 2#3&5#6&42 \# 3 \& 5 \# 6 \& 4

Since &\& (addition) has higher precedence than #\# (multiplication), we evaluate the &\& operations first:

  • 3&5→3+5=83 \& 5 \rightarrow 3 + 5 = 8
  • 6&4→6+4=106 \& 4 \rightarrow 6 + 4 = 10

Substituting these values back, the expression simplifies to: 2#8#102 \# 8 \# 10

Since #\# is left-associative, we evaluate from left to right:

  • (2#8)#10(2 \# 8) \# 10
  • =(2∗8)∗10= (2 * 8) * 10
  • =16∗10=160= 16 * 10 = 160

Thus, the final value at the root of the parse tree is 160.

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

Published
Report a correction
Keep exploringMore Syntax-directed Translation questions