GATE / CS ARCHIVE

GATE CSE 2022 / QUESTION 65

GATE CSE 2022 Q65

Compiler Design · Semantic Analysis & Type Checking · Full GATE CSE 2022 paper

GATE 2022, Q65 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