GateAI
Parsing

GATE CSE 2016 Set 2 · Q45

MCQ2 marksEasy

Time

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

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: B) S → Ab Bb c A → Bd B → e

Parsing

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

Let us analyze each option:

  • Option (A):

    • A→Aa∣bA \rightarrow Aa \mid \text{b}

    This contains immediate (direct) left recursion on AA (A→AaA \rightarrow Aa).

  • Option (B):

    • S→Ab∣Bb∣cS \rightarrow Ab \mid Bb \mid \text{c}

    • A→Bd∣εA \rightarrow Bd \mid \varepsilon

    • B→eB \rightarrow \text{e}

    Let us compute the leading non-terminals reachable from each non-terminal:

    • S⇒Ab⇒Bdb⇒edbS \Rightarrow Ab \Rightarrow Bdb \Rightarrow \text{edb} or S⇒bS \Rightarrow b

    • S⇒Bb⇒ebS \Rightarrow Bb \Rightarrow \text{eb}

    • S⇒cS \Rightarrow \text{c}

    • A⇒Bd⇒edA \Rightarrow Bd \Rightarrow \text{ed} or A⇒εA \Rightarrow \varepsilon

    • B⇒eB \Rightarrow \text{e}

    No non-terminal X∈{S,A,B}X \in \{S, A, B\} can derive a sentential form starting with itself ($X

ot\Rightarrow^+ X\alpha$). Hence, this grammar is free from left recursion.

  • Option (C):

    • S→Aa∣BS \rightarrow Aa \mid B

    • A→Bb∣Sc∣εA \rightarrow Bb \mid Sc \mid \varepsilon

    • B→dB \rightarrow \text{d}

    Derivation: S⇒Aa⇒ScaS \Rightarrow Aa \Rightarrow Sca. Since S⇒+ScaS \Rightarrow^+ Sca, there is indirect left recursion on SS.

  • Option (D):

    • S→Aa∣Bb∣cS \rightarrow Aa \mid Bb \mid \text{c}

    • A→Bd∣εA \rightarrow Bd \mid \varepsilon

    • B→Ae∣εB \rightarrow Ae \mid \varepsilon

    Derivation: A⇒Bd⇒AedA \Rightarrow Bd \Rightarrow Aed. Since A⇒+AedA \Rightarrow^+ Aed, there is indirect left recursion on AA (and similarly on BB).

Thus, only the grammar in (B) is free from left recursion.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

This Parsing previous year question is Q45 of the GATE CSE 2016 Set 2 paper. Find more GATE CSE PYQs on Parsing in Compiler Design, or browse all GATE CSE PYQs.

Free diagnostic

Where do you stand for GATE?

Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Parsing PYQs now with no account.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Parsing in GATE CSE

Parsing has 23 GATE CSE PYQs across 16 papers from 2012 to 2026. 11 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2016 Set 2.

How GATE tests Parsing

More from GATE CSE 2016 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2016 Set 2 · Q45 · MCQ

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

Topic-wise GATE CS PYQs with verified steps

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.