GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 45

Translation-rule precedence

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

GATE 2004, Q45 2 marks · MCQ
Report issue

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