Skip to content
Official PYQ · 2016

GATE CS 2016 Set 2 Q45 — Parsing (LL, LR)

Explain with AI

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

 

 

Choose one option
View worked solution

Answer: B

A grammar has left recursion if there exists a non-terminal XX such that X⇒+XαX \Rightarrow^+ X\alpha for some string of grammar symbols α\alpha. Left recursion can be either immediate (e.g., X→XαX \rightarrow X\alpha) or indirect/mutual (e.g., X→Yβ⇒XαβX \rightarrow Y\beta \Rightarrow X\alpha\beta).

Let's analyze each option:

  • Option A: S→ABA→Aa∣bB→c\begin{aligned} S &\rightarrow AB \\ A &\rightarrow Aa \mid b \\ B &\rightarrow c \end{aligned} The production A→AaA \rightarrow Aa is an immediate left recursion. Thus, this grammar is not free from left recursion.
  • Option B: S→Ab∣Bb∣cA→Bd∣ϵB→e\begin{aligned} S &\rightarrow Ab \mid Bb \mid c \\ A &\rightarrow Bd \mid \epsilon \\ B &\rightarrow e \end{aligned} Let's trace all possible derivations starting with each non-terminal:
    • From SS: S→Ab→Bdb→edbS \rightarrow Ab \rightarrow Bdb \rightarrow edb, or S→Bb→ebS \rightarrow Bb \rightarrow eb, or S→cS \rightarrow c. No derivation starts with SS.
    • From AA: A→Bd→edA \rightarrow Bd \rightarrow ed, or A→ϵA \rightarrow \epsilon. No derivation starts with AA.
    • From BB: B→eB \rightarrow e. No derivation starts with BB.
    Since there is no sequence of derivations where X⇒+XαX \Rightarrow^+ X\alpha for any non-terminal XX, this grammar is free from left recursion.
  • Option C: S→Aa∣BA→Bb∣Sc∣ϵB→d\begin{aligned} S &\rightarrow Aa \mid B \\ A &\rightarrow Bb \mid Sc \mid \epsilon \\ B &\rightarrow d \end{aligned} We can derive: S→Aa→ScaS \rightarrow Aa \rightarrow Sca Since S⇒+ScaS \Rightarrow^+ Sca, this grammar has indirect left recursion.
  • Option D: S→Aa∣Bb∣cA→Bd∣ϵB→Ae∣ϵ\begin{aligned} S &\rightarrow Aa \mid Bb \mid c \\ A &\rightarrow Bd \mid \epsilon \\ B &\rightarrow Ae \mid \epsilon \end{aligned} We can derive: A→Bd→AedA \rightarrow Bd \rightarrow Aed Since A⇒+AedA \Rightarrow^+ Aed, this grammar has indirect left recursion.

Therefore, only the grammar in Option B is free from left recursion.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions