GATE / CS ARCHIVE

GATE CSE 1995 / QUESTION 9(b)

Explain cycles and LL(1) parsing

Theory of Computation · Context-Free Grammars & Languages · Full GATE CSE 1995 paper

GATE 1995, Q9(b) Descriptive
Report issue

Context-Free Grammars & Languages

Question 9(b), GATE 1995: A grammar has a cycle if A⇒⁺A for some nonterminal A. Show that a grammar with cycles cannot be LL(1), stating any necessary qualification.
Tap to enlarge
Answer & solution

Answer: A useful/productive cycle is incompatible with LL(1); the unrestricted claim needs a reduced-grammar assumption. (no official key was published)

Assume the cyclic nonterminal A is reachable in a successful derivation and can derive a terminal string w (possibly ε). Starting at A, one may derive w directly, or first traverse the nonempty cycle A⇒⁺A and then derive the same w. Inserting the cycle into a successful start-symbol derivation gives two distinct parse trees for the same terminal string. Every LL(1) grammar is unambiguous, so this useful cyclic grammar cannot be LL(1).

Operationally, a predictive parser cannot decide from the same next input symbol whether to begin another traversal of a nullable cycle or leave it to produce the terminal string. The proof concerns a cycle deriving A itself, not ordinary right recursion such as A→aA, which consumes input and can be LL(1).

The statement as printed omits the usual reduced-grammar qualification. As a literal counterexample, S→a with an unreachable rule A→A generates only a and has a conflict-free predictive table for all successful parses from S; the unreachable cycle contributes no competing table entry. A nonproductive cycle likewise cannot be inserted in a successful parse. Thus the intended theorem is valid for reduced grammars, or for cycles on useful nonterminals, rather than arbitrary added useless rules.

Original paper, page 6