GATE / CS ARCHIVE

GATE CSE 2007 / QUESTION 52

Dangling-else grammar is not LL(1)

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

GATE 2007, Q52 2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 52, GATE 2007: A grammar has nonterminals S,C,S1, terminals a,b,i,t,e, start S, and productions S→iCtSS1 | a; S1→eS | ε; C→b. Why is this grammar not LL(1)?
Options:
(A) It is left recursive
(B) It is right recursive
(C) It is ambiguous
(D) It is not context-free
Tap to enlarge
Your answer
Answer & solution

Answer: C (no official key was published)

1. Approach. Read i,t,e as if, then, else. In nested conditionals, an else can attach to either the inner or the outer if. Exhibit two parses of the same string to prove ambiguity.

2. Derivation and interpretation. The grammar is S→iCtSS₁ | a, S₁→eS | ε, C→b. It is the dangling-else grammar. The string i b t i b t a e a has two parse trees: the e a can belong to the inner conditional (inner S₁→eS, outer S₁→ε) or the outer conditional (inner S₁→ε, outer S₁→eS). Therefore it is ambiguous and cannot be LL(1), giving C. Equivalently e belongs both to FIRST(eS) and FOLLOW(S₁), causing a prediction-table conflict with S₁→ε. Right recursion by itself is not an obstruction to LL(1), and these productions are context-free and not left recursive.

Original paper, page 12