The GATE Grind

GATE 2017 CS (CS2) – Question 42

Compiler Design · Parsing and Syntax Analysis · 2 marks · Multiple choice

G has E→E−T|T, T→T+F|F, F→(E)|id. Which equivalent grammar has no left recursion?

  1. E→E−T|T; T→T+F|F; F→(E)|id
  2. E→TE′; E′→−TE′|ε; T→T+F|F; F→(E)|id
  3. E→TX; X→−TX|ε; T→FY; Y→+FY|ε; F→(E)|id
  4. E→TX|(TX); X→−TX|+TX|ε; T→id

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (C) E→TX; X→−TX|ε; T→FY; Y→+FY|ε; F→(E)|id

Explanation

For A→Aα|β, eliminate immediate left recursion using A→βA′ and A′→αA′|ε. Applying this separately to E and T produces exactly option C. A retains both recursions and B retains T’s recursion. D changes the language and operator grouping.

More Parsing and Syntax Analysis questions

All Parsing and Syntax Analysis questions →