edited by
9,623 views
39 votes
39 votes

Which one of the following grammars is free from left recursion?

  1. $S \rightarrow AB$
    $A \rightarrow Aa \mid b$
    $B \rightarrow c$
  2. $S \rightarrow Ab \mid Bb \mid c$
    $A \rightarrow Bd \mid \epsilon$
    $B \rightarrow e$
  3. $S \rightarrow Aa \mid B$
    $A \rightarrow Bb \mid Sc \mid \epsilon$
    $ B \rightarrow d$
  4. $S \rightarrow Aa \mid Bb \mid c$
    $A \rightarrow Bd \mid \epsilon$
    $B \rightarrow Ae \mid \epsilon$
edited by

4 Answers

Best answer
60 votes
60 votes

Option (A) has immediate left recursion."$A \rightarrow Aa$"

Option (C) has indirect left recursion "$S\rightarrow Aa \stackrel{A\rightarrow Sc}{\Longrightarrow} Sca$"

Option (D) has indirect left recursion "$A\rightarrow Bd \stackrel{B\rightarrow Ae}{\Longrightarrow} Aed$"

Option (B) is free from left recursion. No direct left recursion. No indirect left recursion.

Correct Option: B

edited by
10 votes
10 votes
make the dependency graph on basis of variable if there is any no cycle then it is free from left recursion
0 votes
0 votes
A has left recursion directly in the second production.

 

For other options, remove the epsilon production from them. In the resulting set of productions try to take each Terminal and see if substituting it directly in other places will lead to left recursion.

 

This ends up happening In options C and D, where Terminal A produces left recursion in terminal S, and Terminal B produces left recursion in terminal  A respectively.

 

Thus B is the answer
0 votes
0 votes
Take some sample input, 1 to 2 random language would work, then try making a parse tree either in mind or pen and paper would work. You will get to see that apart from option (B), the rest all are showing left recursion some or the other way. While in option B if we simply substitute single productions into the respective productions, it’s visible that left recursion doesn’t exist. I hope this helps.
Answer:

Related questions

22 votes
22 votes
1 answer
2
Akash Kanase asked Feb 12, 2016
5,067 views
Match the following:$$\begin{array}{ll|ll}\hline \text{(P)} & \text{Lexical analysis} & \text{(i)} & \text{Leftmost derivation} \\\hline \text{(Q)} & \text{Top down pars...
21 votes
21 votes
4 answers
3
Akash Kanase asked Feb 12, 2016
7,328 views
The Floyd-Warshall algorithm for all-pair shortest paths computation is based onGreedy paradigm.Divide-and-conquer paradigm.Dynamic Programming paradigm.Neither Greedy no...
15 votes
15 votes
4 answers
4