• edited by
14,657 views
49 49 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$

 

 

4 Answers

Best answer
73 73 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
11 11 votes
make the dependency graph on basis of variable if there is any no cycle then it is free from left recursion
0 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 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:
Position:
Show:

Related questions

54 54 votes
1 answers 1 answer
15.9k
15.9k views
Akash Kanase asked Feb 12, 2016
15,947 views
A student wrote two context-free grammars G1 and G2 for generating a single C-like array declaration. The dimension of the array is at least one. For example,int a[10] [3...
35 35 votes
1 answers 1 answer
8.6k
8.6k views
Akash Kanase asked Feb 12, 2016
8,586 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 pa...
33 33 votes
4 answers 4 answers
11.4k
11.4k views
Akash Kanase asked Feb 12, 2016
11,388 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...
25 25 votes
5 answers 5 answers
10.5k
10.5k views
Akash Kanase asked Feb 12, 2016
10,489 views
In which one of the following page replacement algorithms it is possible for the page fault rate to increase even when the number of allocated frames increases?LRU (Least...