GATE / CS ARCHIVE

GATE CSE 2001 / QUESTION 18(a)

Eliminate direct left recursion

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

GATE 2001, Q18(a) Descriptive
Report issue

Parsing & Syntax Analysis

Question 18(a), GATE 2001: Grammar S→Sa | Sb | a | b.
Question 18(a), GATE 2001: Grammar S→Sa | Sb | a | b.
Tap to enlarge
Answer & solution

Answer: S→aR | bR; R→aR | bR | ε. (no official key was published)

In the standard transformation S→Sα1 | Sα2 | β1 | β2, choose α1=a, α2=b and β1=a, β2=b. Introduce a new nonterminal R and write S→aR | bR, R→aR | bR | ε. The initial S production contributes one mandatory letter; R generates any remaining sequence of a’s and b’s, including the empty suffix. Thus both grammars generate exactly all nonempty strings over {a,b}. No production now begins with its own left-hand nonterminal, directly or indirectly. The empty string remains excluded from S, as it was in the original grammar.

Original paper, page 10