GATE / CS ARCHIVE

GATE CSE 2010 / QUESTION 38

Predictive and LR parsing of a grammar

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

GATE 2010, Q38 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 38, GATE 2010: Classify the grammar S→aSa | bS | c.
Options: (A) LL(1) but not LR(1); (B) LR(1) but not LL(1); (C) Both LL(1) and LR(1); (D) Neither LL(1) nor LR(1).
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

Step through it (3 parts)

The productions S→aSa, S→bS and S→c begin with distinct terminals a,b,c and none derives epsilon. Their FIRST sets are disjoint, so one lookahead symbol uniquely selects the production and the grammar is LL(1). Every LL(1) grammar is LR(1), so it is both, C. The trailing a in aSa does not cause a predictive conflict because the initial a already selects that alternative.

The predictive parsing table contains exactly S→aSa under lookahead a, S→bS under b and S→c under c. There are no epsilon productions, so no FIRST/FOLLOW conflict arises. Recursion does not by itself prevent LL(1); left recursion would, but here each recursive call is preceded by a terminal consumed from the input.

Original paper, page 9