Skip to content
Official PYQ · 2017

GATE CS 2017 Set 2 Q32 — Parsing (LL, LR)

Explain with AI

Consider the following expression grammar GG:

  • E→E−T∣TE \rightarrow E-T \mid T
  • T→T+F∣FT \rightarrow T + F \mid F
  • F→(E)∣idF \rightarrow (E) \mid id

Which of the following grammars is not left recursive, but is equivalent to GG?

Choose one option
View worked solution

Answer: C

To eliminate immediate left recursion from a grammar, we use the standard transformation rule:

If we have productions of the form: A→Aα∣βA \rightarrow A\alpha \mid \beta where β\beta does not begin with AA, we can replace them with: A→βA′A′→αA′∣ϵ\begin{aligned} A &\rightarrow \beta A' \\ A' &\rightarrow \alpha A' \mid \epsilon \end{aligned}

Let's apply this rule step-by-step to the given grammar GG:

  1. For the non-terminal EE:

    The production is E→E−T∣TE \rightarrow E-T \mid T. Here, A=EA = E, α=−T\alpha = -T, and β=T\beta = T.

    Applying the transformation (using XX as the new non-terminal instead of E′E'):

    E→TXX→−TX∣ϵ\begin{aligned} E &\rightarrow TX \\ X &\rightarrow -TX \mid \epsilon \end{aligned}
  2. For the non-terminal TT:

    The production is T→T+F∣FT \rightarrow T + F \mid F. Here, A=TA = T, α=+F\alpha = +F, and β=F\beta = F.

    Applying the transformation (using YY as the new non-terminal instead of T′T'):

    T→FYY→+FY∣ϵ\begin{aligned} T &\rightarrow FY \\ Y &\rightarrow +FY \mid \epsilon \end{aligned}
  3. For the non-terminal FF:

    The production is F→(E)∣idF \rightarrow (E) \mid id. Since it is not left-recursive, it remains unchanged:

    F→(E)∣idF \rightarrow (E) \mid id

Combining all the transformed productions, we get:

E→TXX→−TX∣ϵT→FYY→+FY∣ϵF→(E)∣id\begin{aligned} E &\rightarrow TX \\ X &\rightarrow -TX \mid \epsilon \\ T &\rightarrow FY \\ Y &\rightarrow +FY \mid \epsilon \\ F &\rightarrow (E) \mid id \end{aligned}

This matches Option C.

Elimination of other options:

  • Option A: This is the original grammar, which contains left recursion in both EE and TT.
  • Option B: Left recursion is eliminated only for EE, but T→T+F∣FT \rightarrow T + F \mid F is still left-recursive.
  • Option D: This grammar is not equivalent to GG (e.g., it cannot generate the same set of strings, and the operator precedence/associativity is lost).

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

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