GATE CS 2017 Set 2 — Question 32
Go beyond PYQs with Success TrackerAI-powered personalised practice and doubt support. Unlimited practice on eligible plans; AI usage limits apply.MCQ+2 / -0.67MediumLeft Recursion EliminationSyntax AnalysisCompiler Design
Compiler Design → Syntax Analysis → Left Recursion Elimination
Last updated
Question
Consider the following expression grammar G:
Which of the following grammars is not left recursive, but is equivalent to G?
E -> E - T | T
T -> T + F | F
F -> (E) | id
Which of the following grammars is not left recursive, but is equivalent to G?
Correct answer
(C) [code]
Solution
The problem asks for a grammar that is not left recursive and is equivalent to the given grammar G.First, let's analyze the given grammar G:
This grammar has direct left recursion in the productions for E (
Now, let's check for equivalence. This grammar is derived from G by applying the standard algorithm for eliminating left recursion.
For
For
Since this grammar is obtained by the standard left recursion elimination algorithm, it is equivalent to G.
Thus, option (C) is not left recursive and is equivalent to G.
E -> E - T | T
T -> T + F | F
F -> (E) | id
This grammar has direct left recursion in the productions for E (
E -> E - T) and T (T -> T + F). A grammar is left recursive if it has a non-terminal A such that there is a derivation for some string . Both E and T satisfy this.The question requires a grammar that is not left recursive. Let's check the options:- Option (A): This is the same as grammar G. It is left recursive. So, (A) is incorrect.
- Option (B): This grammar contains the production
T -> T + F. This is a direct left recursion for the non-terminal T. Therefore, this grammar is left recursive. So, (B) is incorrect. - Option (C): Let's check for left recursion.
-
E -> TX: The right side starts with a terminal or a different non-terminal (T). -
X -> -TX: The right side starts with a terminal-. -
T -> FY: The right side starts with a different non-terminal (F). -
Y -> +FY: The right side starts with a terminal+. -
F -> (E) | id: The right side starts with terminals(orid.
A -> Aα. This grammar is not left recursive.Now, let's check for equivalence. This grammar is derived from G by applying the standard algorithm for eliminating left recursion.
For
E -> E - T | T, we have . The transformation is , . This matches the productions for E and X in option (C) (with X being ).For
T -> T + F | F, we have . The transformation is , . This matches the productions for T and Y in option (C) (with Y being ).Since this grammar is obtained by the standard left recursion elimination algorithm, it is equivalent to G.
Thus, option (C) is not left recursive and is equivalent to G.
- Option (D): This grammar is not left recursive. However, it is not equivalent to G. For example, in G, T can derive
id + id(T -> T+F -> F+F -> id+id). In grammar (D), T can only deriveid(T -> id). Therefore, the language generated by (D) is a small subset of the language generated by G. So, (D) is not equivalent.
Continue learning with Success Tracker
A step still unclear? Work through it with support
Use Success Tracker to ask about the reasoning, then try another GATE CS question to check your understanding.
AI-powered practice· Unlimited practice on eligible plans
- PYQs with solutions
- Attempt available previous-year questions, then compare your reasoning with the worked solution. Coverage varies by stream.
- Practice that adapts
- Choose a topic, work on weaker areas and bookmark questions to revisit. Your attempts feed your progress tracking.
- AI doubt support
- Ask follow-up questions about a step or concept while practising, instead of stopping at the final answer.
Unlimited practice is available on eligible plans. Free practice and AI usage have limits; check the current plan allowances before choosing.
This page stays readable without an account. AI responses can be wrong; check them against the solution and source material.
More questions on Syntax Analysis
2026 Set 2 Q17In C runtime environment, which one of the following is stored in heap?2026 Set 2 Q19Consider the following three ANSI-C programs, P1, P2, and P3. P1 [code] P2 [code] **P3**…2026 Set 1 Q27Consider the following C statements: Which of the following options is/are correct? [figure]2026 Set 1 Q28Which of the following statements is/are true?2026 Set 2 Q35A lexical analyzer uses the following token definitions - - …