GATE / CS ARCHIVE

GATE CSE 2005 / QUESTION 59

Handles in an expression reduction

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

GATE 2005, Q59 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 59, GATE 2005: Grammar E→E+n | E×n | n generates n+n×n. List the handles in bottom-up reduction order. A n, E+n, E+n×n; B n, E+n, E+E×n; C n, n+n, n+n×n; D n, E+n, E×n.
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

Step through it (10 steps)

The rightmost derivation is E ⇒ E×n ⇒ E+n×n ⇒ n+n×n. Reversing it reduces the leftmost n first, then E+n, then E×n. These handles are n, E+n, E×n, option D. The fixed terminal n on the right of each operator means this is not the usual ambiguous E→E+E|E×E grammar.

A handle is the RHS whose reduction undoes one step of a rightmost derivation, not an arbitrary matching substring. Reverse the displayed derivation: n+n×n→E+n×n→E×n→E. The successive reduced RHS strings are n, E+n and E×n. Reducing the rightmost terminal n first would not reverse this rightmost derivation. The grammar’s fixed terminal right operands force the association, unlike E→E+E.

Original paper, page 7