Skip to content
Official PYQ · 2023

GATE CS 2023 Q50 — Syntax-directed Translation

Explain with AI
Consider the syntax directed translation given by the following grammar and semantic rules. Here N,I,FN, I, F and BB are non-terminals. NN is the starting non-terminal, and #,0\#, \mathbf{0} and 1\mathbf{1} are lexical tokens corresponding to input letters “#", “0" and “1",\text{“\#", “0" and “1"}, respectively. X.valX.val denotes the synthesized attribute (a numeric value) associated with a non-terminal X.  I1X. \;I_{1} and F1F_{1} denote occurrences of II and FF on the right hand side of a production, respectively. For the tokens 0\mathbf{0} and 1,0.val=0\mathbf{1}, \mathbf{0} . v a l=0 and 1.val=1\mathbf{1}.val =1.
N→I#FN.val=I.val+F.valI→I1BI.val=(2I1.val)+B.valI→BI.val= B.val F→BF1F.val=12(B.val+F1.val)F→B F.val =12B.valB→0B.val=0.valB→1B.val=1.val\begin{array}{ll} N \rightarrow I \# F & N.val =I . v a l+F . v a l \\ I\rightarrow I_{1} B & I.val =\left(2 I_{1} . v a l\right)+B.val \\ I \rightarrow B & I.val =\text{ B.val } \\ F \rightarrow B F_{1} & F.val =\frac{1}{2}\left(B . v a l+F_{1} . v a l\right) \\ F \rightarrow B & \text{ F.val }=\frac{1}{2} B.val \\ B \rightarrow \mathbf{0} & B . v a l=\mathbf{0} . v a l \\ B \rightarrow \mathbf{1} & B . v a l=\mathbf{1} . v a l \\ \end{array}
The value computed by the translation scheme for the input string
10#01110\#011
is ____________. (Rounded off to three decimal places)
View worked solution

Answer: 2.375

The input string is 10#01110\#011, which is split by the delimiter #\# into the integer part I=10I = 10 and the fractional part F=011F = 011. We evaluate the synthesized attributes bottom-up.

1. Evaluation of the integer part II for "10":

  • For the first character '1': I1→B→1  ⟹  I1.val=1I_1 \rightarrow B \rightarrow \mathbf{1} \implies I_1.val = 1
  • For the second character '0': I→I1B→I10  ⟹  I.val=2×I1.val+0=2×1+0=2I \rightarrow I_1 B \rightarrow I_1 \mathbf{0} \implies I.val = 2 \times I_1.val + 0 = 2 \times 1 + 0 = 2

2. Evaluation of the fractional part FF for "011":

The derivation tree for F⇒011F \Rightarrow 011 is F→B1F1→B1B2F2→B1B2B3F \rightarrow B_1 F_1 \rightarrow B_1 B_2 F_2 \rightarrow B_1 B_2 B_3, where B1.val=0B_1.val = 0, B2.val=1B_2.val = 1, and B3.val=1B_3.val = 1.

  • F2→B3  ⟹  F2.val=12B3.val=12×1=0.5F_2 \rightarrow B_3 \implies F_2.val = \frac{1}{2} B_3.val = \frac{1}{2} \times 1 = 0.5
  • F1→B2F2  ⟹  F1.val=12(B2.val+F2.val)=12(1+0.5)=0.75F_1 \rightarrow B_2 F_2 \implies F_1.val = \frac{1}{2} (B_2.val + F_2.val) = \frac{1}{2} (1 + 0.5) = 0.75
  • F→B1F1  ⟹  F.val=12(B1.val+F1.val)=12(0+0.75)=0.375F \rightarrow B_1 F_1 \implies F.val = \frac{1}{2} (B_1.val + F_1.val) = \frac{1}{2} (0 + 0.75) = 0.375

3. Evaluation of NN:

N.val=I.val+F.val=2+0.375=2.375N.val = I.val + F.val = 2 + 0.375 = 2.375

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

Published
Report a correction
Keep exploringMore Syntax-directed Translation questions