Skip to content
Official PYQ · 2024

GATE CS 2024 Set 1 Q27 — Syntax-directed Translation

Explain with AI

​Consider the following syntax-directed definition (SDD)\text{(SDD)}.

S→DHTU{S.val=D.val+H.val+T.val+U.val;}D→"M"D1{D.val=5+D1.val;}D→ϵ{D.val=−5;}H→"L"H1{H.val=5∗10+H1.val;}H→ϵ{H.val=−10;}T→"C" T1{T.val=5∗100+T1.val;}T→ϵ{T.val=−5;}U→"K"{U.val=5;}\begin{array}{|l|l|} \hline \text{$S \rightarrow D H T U$} & \text{$ \left\{S.v a l = D.val + H.val + T.val + U.val;\right\}$} \\ \hline D \rightarrow ^{"} \mathrm{M}^{"} D_1 & \left\{D.val =5+D_{1}.val; \right\} \\ \hline D \rightarrow \epsilon & \left\{D.val = -5; \right\} \\ \hline H \rightarrow ^{"}\mathrm{L}^{"} H_1 & \left\{H.val = 5^{*} 10+H_{1}.v a l;\right\} \\ \hline H \rightarrow \epsilon & \left\{H.val = -10; \right\} \\ \hline T \rightarrow \text{"C" } T_1 & \left\{T.v a l=5^{*} 100+T_{1}. val;\right\} \\ \hline T \rightarrow \epsilon & \left\{T . v a l= - 5;\right\} \\ \hline U \rightarrow \text{"K"} & \left\{U . v a l= 5;\right\} \\ \hline \end{array}

Given "MMLK"\text{"MMLK"} as the input, which one of the following options is the CORRECT\text{CORRECT} value computed by the SDD\text{SDD} (in the attribute S.valS.val)?

Choose one option
View worked solution

Answer: A

To find the value of the attribute S.valS.val for the input string "MMLK", we first construct the parse tree based on the grammar rules and then compute the synthesized attributes bottom-up.

1. Parsing the Input "MMLK"

The start symbol is SS, which derives DHTUD H T U. We match the input string "MMLK" to these non-terminals:

  • DD matches "MM": D→"M"D1→"M""M"D2→"M""M"ϵD \rightarrow \text{"M"} D_1 \rightarrow \text{"M"} \text{"M"} D_2 \rightarrow \text{"M"} \text{"M"} \epsilon
  • HH matches "L": H→"L"H1→"L"ϵH \rightarrow \text{"L"} H_1 \rightarrow \text{"L"} \epsilon
  • TT matches ϵ\epsilon: T→ϵT \rightarrow \epsilon
  • UU matches "K": U→"K"U \rightarrow \text{"K"}

2. Evaluating the Attributes

Now, we compute the synthesized attributes for each subtree:

Evaluating D.valD.val:

  • For D2→ϵD_2 \rightarrow \epsilon: D2.val=−5D_2.val = -5
  • For D1→"M"D2D_1 \rightarrow \text{"M"} D_2: D1.val=5+D2.val=5+(−5)=0D_1.val = 5 + D_2.val = 5 + (-5) = 0
  • For D→"M"D1D \rightarrow \text{"M"} D_1: D.val=5+D1.val=5+0=5D.val = 5 + D_1.val = 5 + 0 = 5

Evaluating H.valH.val:

  • For H1→ϵH_1 \rightarrow \epsilon: H1.val=−10H_1.val = -10
  • For H→"L"H1H \rightarrow \text{"L"} H_1: H.val=5×10+H1.val=50+(−10)=40H.val = 5 \times 10 + H_1.val = 50 + (-10) = 40

Evaluating T.valT.val:

  • For T→ϵT \rightarrow \epsilon: T.val=−5T.val = -5

Evaluating U.valU.val:

  • For U→"K"U \rightarrow \text{"K"}: U.val=5U.val = 5

3. Computing S.valS.val

Using the semantic rule for S→DHTUS \rightarrow D H T U:

S.val=D.val+H.val+T.val+U.val=5+40+(−5)+5=45\begin{align*} S.val &= D.val + H.val + T.val + U.val \\ &= 5 + 40 + (-5) + 5 \\ &= 45 \end{align*}

Thus, the correct value computed by the SDD is 45.

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

Published
Report a correction
Keep exploringMore Syntax-directed Translation questions