GATE / CS ARCHIVE

SIMILAR QUESTIONS / COMPILER DESIGN

Transform grammars to remove left recursion

3 questions solved the same way, newest first. Back to Compiler Design

How these are solved

Substitute leading nonterminals where necessary, then replace direct left recursion with an auxiliary tail nonterminal and epsilon. 1998 needs indirect substitution/minimality; others direct.

2 marks · MCQ
Report issue

Parsing & Syntax Analysis

Question 32, GATE 2017: Grammar G is E→E−T|T, T→T+F|F, F→(E)|id. Choose an equivalent grammar without left recursion. A:the same grammar. B:E→TE′, E′→−TE′|ε, T→T+F|F, F→(E)|id. C:E→TX, X→−TX|ε, T→FY, Y→+FY|ε, F→(E)|id. D:E→TX|(TX), X→−TX|+TX|ε, T→id.
Tap to enlarge
Your answer
Answer & solution

Official answer: C

1. Apply the standard direct-left-recursion transformation A→Aα|β becomes A→βA′, A′→αA′|ε. For E, α is −T and β is T, giving E→TX and X→−TX|ε.

2. Apply it separately to T: α is +F and β is F, giving T→FY and Y→+FY|ε. Keep F→(E)|id so arbitrarily nested factors remain possible. These are exactly option C.

3. A leaves both original recursions intact. B fixes E but leaves T→T+F directly left-recursive. D restricts T to id and cannot generate nested expressions such as ((id)) or parentheses as arbitrary factors. The transformation preserves the generated strings, although semantic associativity must be handled when constructing evaluation actions.

Original paper, page 12 · Answer key

Descriptive
Report issue

Eliminate direct left recursion

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

Descriptive
Report issue

Eliminate indirect and direct left recursion

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