GATE / CS ARCHIVE

GATE CSE 2021 SET 1 / QUESTION 5

GATE CSE 2021 Set 1 Q5

Compiler Design · Parsing & Syntax Analysis · Full GATE CSE 2021 Set 1 paper

GATE 2021 Set 1, Q5 1 mark · MCQ
Report issue

Parsing & Syntax Analysis

Question 5, GATE 2021: S1: Every SLR(1) grammar is unambiguous, but some unambiguous grammars are not SLR(1). S2: For every context-free grammar there is a parser taking at most O(n³) time for an input of length n. Which is correct? A S1 true/S2 false; B S1 false/S2 true; C both true; D both false.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

A conflict-free SLR(1) table yields a unique parse, so every SLR(1) grammar is unambiguous. The converse fails: the standard grammar S→L=R|R, L→*R|id, R→L is unambiguous but has an SLR shift-reduce conflict. General CFG recognition and construction of a parse can be done in O(n³) for a fixed grammar, for example by Earley parsing or CYK after normalization. Thus both statements hold, C; this does not claim that exponentially many parse trees can all be listed in cubic time.

In the counterexample grammar, the LR(0) state after recognizing L contains S→L·=R and R→L·. Since '=' belongs to FOLLOW(R), SLR would both shift '=' and reduce R→L there. Thus unambiguity alone does not remove SLR conflicts. For cubic parsing, CYK stores whether each nonterminal derives each substring and considers O(n) split points for O(n²) substrings; the fixed grammar contributes only a constant factor. A parse witness can be recovered by stored backpointers.

Original paper, page 14 · Answer key