The GATE Grind

GATE 2023 CS – Question 60

Compiler Design · Syntax-Directed Translation · 2 marks · Numerical answer

Consider the syntax directed translation given by the following grammar and semantic rules. Here N, I, F and B are non-terminals. N is the starting non-terminal, and #, 0 and 1 are lexical tokens corresponding to input letters "#", "0" and "1", respectively. X.val denotes the synthesized attribute (a numeric value) associated with a non-terminal X. $I_1$ and $F_1$ denote occurrences of I and F on the right hand side of a production, respectively. For the tokens 0 and 1, 0.val = 0 and 1.val = 1.
N → I # F : N.val = I.val + F.val
I → I1 B : I.val = (2 · I1.val) + B.val
I → B : I.val = B.val
F → B F1 : F.val = ½ (B.val + F1.val)
F → B : F.val = ½ B.val
B → 0 : B.val = 0.val
B → 1 : B.val = 1.val
The value computed by the translation scheme for the input string 10#011 is ______. (Rounded off to three decimal places)

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: 2.375

Explanation

Evaluate the translation for the input `10#011` (parse: $N\to I\#F$).

**$I$ for `10`:** $I\to I_1B$ with $I_1=$ `1`, $B=$ `0`.
$I(\texttt1)=B.val=1$. Then $I(\texttt{10})=2\cdot1+0=2$.

**$F$ for `011`:** $F\to BF_1$ gives $F.val=\frac12(B.val+F_1.val)$ and $F\to B$ gives $\frac12B.val$.
- $F(\texttt1)=\frac12\cdot1=0.5$
- $F(\texttt{11})=\frac12(1+0.5)=0.75$
- $F(\texttt{011})=\frac12(0+0.75)=0.375$

$N.val=I.val+F.val=2+0.375=\mathbf{2.375}$.

More Syntax-Directed Translation questions

All Syntax-Directed Translation questions →