Skip to main content

GATE CSE 2024Compiler Design › Syntax-Directed Translation & Intermediate CodeVerified

GATE CSE 2024 PYQ: ​Consider the following syntax-directed definition (SDD) \text{(SDD)} (SDD) . S → D H T U { S . v a l = D .…

​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)?

  1. A
    45
    Correct answer
  2. B
    50
  3. C
    55
  4. D
    65

Answer

(A) 45 45 45

Approach

Parse the input MMLK according to S -> D H T U. Since the string is M M L K, D handles the two M's, H handles the L, T uses its ε-production (no C is present), and U handles the K. Evaluate the synthesized attributes bottom-up and add them at S.

Only the first two M's are consumed by D; the L is consumed by H; T consumes no input and takes its ε-production value -5; K is consumed by U. Summing D.val=5, H.val=40, T.val=-5, U.val=5 yields 45.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.