Question

Consider the following expression grammar GG:

E→E−T∣TT→T+F∣FF→(E)∣id\begin{aligned} E &\to E - T \mid T \\ T &\to T + F \mid F \\ F &\to (E) \mid \text{id} \end{aligned}

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

Choose one option
Official answer and solution
Official answer
C
From
Official GATE 2017 answer key

Solution

Eliminate immediate left recursion from each non-terminal in grammar GG using the standard transformation rule.

The standard rule to eliminate immediate left recursion A→Aα1∣⋯∣Aαm∣β1∣⋯∣βnA \to A\alpha_1 \mid \dots \mid A\alpha_m \mid \beta_1 \mid \dots \mid \beta_n is:

A→β1A′∣⋯∣βnA′A \to \beta_1 A' \mid \dots \mid \beta_n A'
A′→α1A′∣⋯∣αmA′∣ϵA' \to \alpha_1 A' \mid \dots \mid \alpha_m A' \mid \epsilon

Apply this to the given grammar GG:

  1. For production E→E−T∣TE \to E - T \mid T:
    Here A=EA = E, α=−T\alpha = -T, β=T\beta = T. Replacing E′E' with variable name XX:
    E→TXE \to T X
    X→−TX∣ϵX \to -T X \mid \epsilon
  2. For production T→T+F∣FT \to T + F \mid F:
    Here A=TA = T, α=+F\alpha = +F, β=F\beta = F. Replacing T′T' with variable name YY:
    T→FYT \to F Y
    Y→+FY∣ϵY \to +F Y \mid \epsilon
  3. For production F→(E)∣idF \to (E) \mid \text{id}:
    This rule contains no left recursion and remains unchanged.

The resulting grammar without left recursion is:

E→TXE \to T X
X→−TX∣ϵX \to -T X \mid \epsilon
T→FYT \to F Y
Y→+FY∣ϵY \to +F Y \mid \epsilon
F→(E)∣idF \to (E) \mid \text{id}

This is completely equivalent to GG and identical to option C.

Final answer

C

Common trap

Option B eliminates left recursion only from EE, leaving T→T+F∣FT \to T + F \mid F still left-recursive.

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

Source

Paper
GATE 2017 Computer Science and Information Technology (CS) Session 2
Question
32 of 65, printed as CS Q.32
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.