GATE / CS ARCHIVE

GATE CSE 2024 SET 1 / QUESTION 37

Evaluate an SDD including epsilon attributes

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2024 Set 1 paper

GATE 2024 Set 1, Q37 2 marks · MCQ
Report issue

Semantic Analysis & Type Checking

Question 37, GATE 2024: An SDD uses S→DHTU with S.val equal to the sum of the four child values. D→M D1 adds 5 to D1.val, while D→ε has value −5. H→L H1 adds 50, while H→ε has value −10. T→C T1 adds 500, while T→ε has value −5. U→K has value 5. For input MMLK, find S.val. A 45; B 50; C 55; D 65.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Step through it (14 steps)

Approach: Split the input according to the grammar and evaluate synthesized attributes from each terminating production upward.
1. D consumes the two Ms: D→M D1→M M D2→M M ε. The bottom value is −5, so D1.val=5−5=0 and D.val=5+0=5.
2. H consumes the single L. Its recursive rule contributes 50, then its epsilon rule contributes −10. Hence H.val=50−10=40.
3. There is no C between L and K, so T immediately uses T→ε. This still assigns T.val=−5; generating no terminal symbols does not mean an attribute value of zero.
4. U consumes K and has U.val=5. The start rule sums the values: S.val=5+40−5+5=45.
Answer: A. The three distinct epsilon base values are part of the semantics and must be applied even when their terminal run is empty.

Original paper, page 23 · Answer key