Question
Which one of the following grammars is free from left recursion?
Official answer and solution
- Official answer
- B
- From
- Official GATE 2016 answer key
Solution
Inspect each grammar for direct left recursion and indirect left recursion .
A grammar has left recursion if there exists a non-terminal such that for some string .
Analyze Option A:
The production contains immediate (direct) left recursion. Hence, grammar A is not free from left recursion.
Analyze Option B:
Let us check the leading derivations for each non-terminal:
- From : only , which starts with a terminal.
- From : , or .
- From : , or (via ), or , or .
None of the derivations from or can produce a sentential form starting with the same non-terminal. Hence, grammar B is free from both direct and indirect left recursion.
Analyze Option C:
Here , which gives . This is an indirect left recursion. Hence, grammar C is not free from left recursion.
Analyze Option D:
Here , which gives . This is an indirect left recursion between and . Hence, grammar D is not free from left recursion.
Therefore, only grammar B is free from left recursion.
B
Looking only for immediate left recursion and failing to trace indirect cycles like in C or in D.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2016 Computer Science and Information Technology (CS) Session 2
- Question
- 55 of 65, printed as CS Q.45
- 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.