GATE / CS ARCHIVE

GATE CSE 2011 / QUESTION 27

Precedence and associativity parse tree

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2011 paper

GATE 2011, Q27 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 27, GATE 2011: For binary operators ↑ and ↓, ↑ has higher precedence and is right-associative, whereas ↓ is left-associative. Which parse tree represents 7↓3↑4↑3↓2? Trees are encoded below as fully parenthesized expressions.

(A) 7↓(3↑(4↑(3↓2)))
(B) (7↓(3↑(4↑3)))↓2
(C) 7↓(((3↑4)↑3)↓2)
(D) (((7↓3)↑4)↑3)↓2
Tap to enlarge
Your answer
Answer & solution

Official answer: B

Higher-precedence ↑ groups the middle chain, and its right associativity gives 3↑(4↑3). Lower-precedence ↓ then associates left, yielding(7↓(3↑(4↑3)))↓2. The displayed tree with that structure is B.

Associativity is applied only after precedence identifies which operators compete at the same level. First make the ↑ subtree with root ↑, left leaf 3 and right subtree (4↑3). The remaining expression has the form 7↓T↓2; left associativity makes its root the final ↓, with right child 2 and left child (7↓T). A gives ↓2 too high a binding priority; C and D left-group the ↑ chain or combine the initial ↓ too early.

Original paper, page 7 · Answer key