IAE Academy

GATE 2024 CS1 Q.37: solution and answer

GATE 2024 · CS1Q.37 · 2 marks · MCQ

Consider the following syntax-directed definition (SDD).

Given "MMLK" as the input, which one of the following options is the CORRECT value computed by the SDD (in the attribute )?

GATE 2024 CS1 question 37: A table of eight productions with semantic actions: S -> D H T U S.val = D.val + H.val + T.val + U.val;;Tap to open full size
Figure from the GATE 2024 CS1 paper. It is part of the question.
Figure description

A table of eight productions with semantic actions: S -> D H T U S.val = D.val + H.val + T.val + U.val;; D -> "M" D1 D.val = 5 + D1.val;; D -> epsilon D.val = -5;; H -> "L" H1 H.val = 5*10 + H1.val;; H -> epsilon H.val = -10;; T -> "C" T1 T.val = 5*100 + T1.val;; T -> epsilon T.val = -5;; U -> "K" U.val = 5;.

  1. (A)

    45

  2. (B)

    50

  3. (C)

    55

  4. (D)

    65

Answer and solution

ANSWER

, option (A).

Official answer: ASource: Official answer key, GATE 2024 CS1 (IISc Bengaluru)

  1. Parse "MMLK" with : derives "MM", derives "L", derives (there is no "C" in the input), and derives "K".

  2. Every attribute is synthesised, so evaluate bottom-up: each parent takes its value from its children.

  3. : the innermost gives . Then applies twice: , then . So .

  4. : gives . Then gives . So .

  5. : gives .

  6. : gives .

  7. Root: , which is option (A).

KEY IDEA

Each nonterminal's value is computed bottom-up from its parse subtree. An production still contributes its own constant (here a negative base value), and that constant is added back into the parent's total.

WATCH OUT FOR

Forgetting that the productions contribute , and . For example, treating the missing as instead of gives . Counting as , with no base, is another slip.

From the GATE 2024 CS1 paper. Every question of this paper · Download the full paper · All 7 Syntax-directed translation questions, with notes

More Syntax-directed translation questions