GATE / CS ARCHIVE

GATE CSE 1998 / QUESTION 6(b)

Eliminate indirect and direct left recursion

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

GATE 1998, Q6(b) Descriptive
Report issue

Parsing & Syntax Analysis

Question 6(b), GATE 1998: For S→Aa|b and A→Ac|Sd|ε, construct an equivalent grammar with no left recursion and the minimum number of productions. Count each alternative as a production.
Question 6(b), GATE 1998: For S→Aa|b and A→Ac|Sd|ε, construct an equivalent grammar with no left recursion and the minimum number of productions. Count each alternative as a production.
Tap to enlarge
Answer & solution

Answer: A valid six-alternative grammar is S→Ta|bdTa|b; T→cT|adT|ε. Global minimum among arbitrary CFGs is not certified. (no official key was published)

Substitute S→Aa|b into the occurrence S in A→Sd. Then A→Ac|Aad|bd|ε. Each recursive use appends either c or ad on the right; the base is either bd or ε. Thus L(A)=(ε∪{bd}){c,ad}*, and

L(S)={b} ∪ (ε∪{bd}){c,ad}*{a}.

Introduce T generating {c,ad}*. The following equivalent grammar has six individual alternatives:

S → Ta | bdTa | b
T → cT | adT | ε

Every recursive T production starts with a terminal, so it cannot be left recursive. S is not reachable from T, and although S→Ta starts with a nonterminal, its nullable prefix cannot lead back to S. This also rules out indirect left recursion. The first two S alternatives select the two possible prefixes before a word from {c,ad}* and its final a; the third supplies the isolated word b. Conversely, each original derivation has exactly one of these forms, proving language equivalence.

For comparison, the usual mechanical elimination gives S→Aa|b, A→bdT|T, T→cT|adT|ε, with seven alternatives. Inlining A into S removes its two rules and replaces one S alternative by two, saving one production. The six-rule grammar is therefore smaller than that routine result.

The printed requirement “minimum number” needs care. This construction establishes an upper bound of six. It is not a proof that no differently structured, arbitrary CFG can use fewer alternatives. Counting a line with several bars as one “rule” would also give a different metric. The requested global minimum is left uncertified rather than asserted without a lower-bound proof; the equivalent non-left-recursive grammar itself is fully established.

Original paper, page 6