GATE / CS ARCHIVE

GATE CSE 2018 / QUESTION 38

Reading precedence from a parse tree

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

GATE 2018, Q38 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 38, GATE 2018: For a#b$c$d#e#f, the supplied tree represents a#(((b$c)$d)#(e#f)). Choose A $ higher and left-associative, # right-associative; B # higher and left-associative, $ right-associative; C $ higher and left-associative, # left-associative; D # higher and right-associative, $ left-associative.
Tap to enlarge
Your answer
Answer & solution

Official answer: A

Method

Read each internal node as an operation on its two child subtrees. The $ subtree first combines b,c and then combines that result with d, demonstrating left association. Treating this whole subtree as one operand gives a#(X#(e#f)), which nests # to the right. Because the dollar chain is grouped into an operand before surrounding hash operations, $ has higher precedence in the intended grammar. Do not infer precedence from the visual height of leaf letters.

Solution

The tree groups the expression as a#(((b$c)$d)#(e#f)). The two dollar operations group left, and their subtree is below the hash operations, showing higher precedence. The hash chain groups to the right. Thus A.

Original paper, page 15 · Answer key