Question

Which one of the following grammars is free from left recursion?

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2016 answer key

Solution

Inspect each grammar for direct left recursion A→AαA \to A\alpha and indirect left recursion A⇒+AαA \Rightarrow^+ A\alpha.

A grammar has left recursion if there exists a non-terminal AA such that A⇒+AαA \Rightarrow^+ A\alpha for some string α\alpha.

Analyze Option A:
S→ABS \to AB
A→Aa∣bA \to Aa \mid b
B→cB \to c
The production A→AaA \to Aa contains immediate (direct) left recursion. Hence, grammar A is not free from left recursion.

Analyze Option B:
S→Ab∣Bb∣cS \to Ab \mid Bb \mid c
A→Bd∣εA \to Bd \mid \varepsilon
B→eB \to e
Let us check the leading derivations for each non-terminal:

  • From BB: only B→eB \to e, which starts with a terminal.
  • From AA: A→Bd  ⟹  edA \to Bd \implies ed, or A→εA \to \varepsilon.
  • From SS: S→Ab  ⟹  Bdb  ⟹  edbS \to Ab \implies Bdb \implies edb, or S→Ab  ⟹  bS \to Ab \implies b (via A→εA \to \varepsilon), or S→Bb  ⟹  ebS \to Bb \implies eb, or S→cS \to c.
    None of the derivations from S,A,S, A, or BB can produce a sentential form starting with the same non-terminal. Hence, grammar B is free from both direct and indirect left recursion.

Analyze Option C:
S→Aa∣BS \to Aa \mid B
A→Bb∣Sc∣εA \to Bb \mid Sc \mid \varepsilon
B→dB \to d
Here S→Aa  ⟹  ScaS \to Aa \implies Sca, which gives S⇒+ScaS \Rightarrow^+ Sca. This is an indirect left recursion. Hence, grammar C is not free from left recursion.

Analyze Option D:
S→Aa∣Bb∣cS \to Aa \mid Bb \mid c
A→Bd∣εA \to Bd \mid \varepsilon
B→Ae∣εB \to Ae \mid \varepsilon
Here A→Bd  ⟹  AedA \to Bd \implies Aed, which gives A⇒+AedA \Rightarrow^+ Aed. This is an indirect left recursion between AA and BB. Hence, grammar D is not free from left recursion.

Therefore, only grammar B is free from left recursion.

Final answer

B

Common trap

Looking only for immediate left recursion and failing to trace indirect cycles like S→Aa⇒ScaS \to Aa \Rightarrow Sca in C or A→Bd⇒AedA \to Bd \Rightarrow Aed in D.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
55 of 65, printed as CS Q.45
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.