GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Evaluate synthesized attributes on a parse tree

4 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Parse the given string and evaluate child-to-parent attribute equations in dependency order. Grammar tokens need not have usual arithmetic meanings; different strings/operators/epsilon constants prohibit repeat folding.

2 marks · MCQ
Report issue

Evaluate an SDD including epsilon attributes

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

2 marks · NAT
Report issue

Semantic Analysis & Type Checking

Question 65, GATE 2022: Translation grammar: S→S1#T gives S.val=S1.val*T.val; S→T copies T.val; T→T1%R gives T.val=T1.val/R.val; T→R copies R.val; R→id copies integer id.val. Evaluate root S.val for 20#10%5#8%2%2. The source lists an unused nonterminal P as well.
Tap to enlarge
Answer & solution

Official answer: 80

Step through it (28 steps)

The grammar gives % higher precedence than #, with both operators left-associative. Their semantic operations are division and multiplication, respectively, rather than C remainder. The three T values are 20, 10/5=2, and (8/2)/2=2. The left-associative S combination therefore gives (20*2)*2=80. The printed nonterminal list includes an unused P, but no production or calculation depends on it.

The parse groups as (20 # (10 % 5)) # ((8 % 2) % 2). The left recursion in T fixes the latter association, so its value is (8/2)/2=2 rather than 8/(2/2)=8. S’s translation multiplies the three T values: 20×2×2=80.

Original paper, page 41 · Answer key

2 marks · MCQ
Report issue

Translation-rule precedence

Semantic Analysis & Type Checking

Question 45, GATE 2004: Grammar and semantic rules, start E: E→E1 # T with E.value=E1.value×T.value, or E→T with E.value=T.value; T→T1 & F with T.value=T1.value+F.value, or T→F with T.value=F.value; F→num with F.value=num.value. Find the root value for 2 # 3 & 5 # 6 & 4. The addition rule is visible in GFG; GateForum instead prints multiplication for that rule as well.
Options:
(A) 200
(B) 180
(C) 160
(D) 40
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. The grammar groups & inside T, hence it has higher precedence than #. Image the T→T1 & F semantic rule is T.value=T1.value+F.value, while E→E1 # T multiplies.

2. Therefore 2 # 3 & 5 # 6 & 4 evaluates as 2×(3+5)×(6+4)=160, option C.

4. Teaching check: Precedence comes from the grammar, not the symbols’ usual meanings. T binds & before the E-level #. Thus the parse groups 3&5 and 6&4 before combining the factors with #.

Original paper, page 6

Descriptive
Report issue

Parse unusual arithmetic precedence

Parsing & Syntax Analysis

Question 19(a), GATE 2000: Grammar/SDTS: E→E1*T {E.val=E1.val*T.val}; E→T {E.val=T.val}; T→F−T1 {T.val=F.val−T1.val}; T→F {T.val=F.val}; F→2 {F.val=2}; F→4 {F.val=4}. Attributes are evaluated immediately after bottom-up reductions. Construct the parse tree for 4−2−4*2 and evaluate it.
Question 19(a), GATE 2000: Grammar/SDTS: E→E1*T {E.val=E1.val*T.val}; E→T {E.val=T.val}; T→F−T1 {T.val=F.val−T1.val}; T→F {T.val=F.val}; F→2 {F.val=2}; F→4 {F.val=4}. Attributes are evaluated immediately after bottom-up reductions. Construct the parse tree for 4−2−4*2 and evaluate it.
Tap to enlarge
Answer & solution

Answer: (4−(2−4))*2 = 12 (no official key was published)

Step through it (19 steps)

The grammar makes subtraction part of T and multiplication part of E. Thus subtraction binds more tightly than multiplication. Moreover T→F−T is right-recursive, so the subtraction chain groups to the right, not to the left. The annotated parse tree is:

E[12]
├── E[6]
│   └── T[6]
│       ├── F[4] → 4
│       ├── −
│       └── T[−2]
│           ├── F[2] → 2
│           ├── −
│           └── T[4]
│               └── F[4] → 4
├── *
└── T[2]
    └── F[2] → 2

Its left-to-right terminal yield is exactly 4−2−4*2. Bottom-up, the innermost T has value 4; its parent computes 2−4=−2; the next T computes 4−(−2)=6. E→T copies 6. The final right T has value 2, so the root computes 6*2=12. Ordinary arithmetic precedence would give a different result; here the supplied grammar, not familiar operator conventions, defines the grouping.

Original paper, page 9