GATE / CS ARCHIVE

GATE CSE 2019 / QUESTION 3

Which derivation an LR parser reconstructs

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

GATE 2019, Q3 1 mark · MCQ
Report issue

Parsing & Syntax Analysis

Question 3, GATE 2019: An LR parser corresponds to which derivation order? Choices: A leftmost; B leftmost in reverse; C rightmost; D rightmost in reverse.
Tap to enlarge
Your answer
Answer & solution

Official answer: D

Method: distinguish the left-to-right scan of the input from the order of grammar productions reconstructed by reductions.

1. LR parsing is bottom-up. A reduction replaces a handle, a right-hand side of a production, by its left-hand-side nonterminal.
2. This operation undoes a production application rather than applying it from the start symbol outward. Therefore the derivation is reconstructed in reverse.
3. The handles reduced by an LR parser are exactly those obtained by reversing a rightmost derivation. For example, with S → AB, A → a, B → b, a rightmost derivation is S ⇒ AB ⇒ Ab ⇒ ab. Reversing it gives ab ⇒ Ab ⇒ AB ⇒ S, the bottom-up reduction sequence.
4. In the name LR, L denotes scanning input left-to-right and R denotes reconstructing a rightmost derivation. The L does not mean leftmost derivation.

Result: D, a rightmost derivation in reverse.

Original paper, page 4 · Answer key