• edited by
17,416 views
43 43 votes

A grammar that is both left and right recursive for a non-terminal, is

  1. Ambiguous

  2. Unambiguous

  3. Information is not sufficient to decide whether it is ambiguous or unambiguous

  4. None of the above

3 Answers

Best answer
89 89 votes

Let grammar be like this :
$S \rightarrow a$
$A \rightarrow AbA$
This grammar is left as well as right recursive but still unambiguous. A is a useless production but still part of grammar..
A grammar having both left as well as right recursion may or may not be ambiguous.



Let's take a grammar say 

$S\rightarrow SS$

Now, according to the link https://en.wikipedia.org/wiki/Formal_grammar

For a grammar G, if we have L(G) then all the strings/sentences in this language can be produced after some finite number of steps .

But, for the grammar $$S\rightarrow SS$$

Can we produce any string after a finite number of steps? NO, and hence the language of this grammar is empty set {} .

Hence, If a grammar is having both left and right recursion, then grammar may or may not be ambiguous.

Correct Answer: $C$

• edited by
6 6 votes

Information is insufficient ie C would be the correct option. 

Grammar that is both left recursive and right recursive can be ambiguous. 
Consider the grammar,

E -> E - E | T
T -> T / T | F
F -> num | ( E )

There can be 2 parse trees for "5-3-2"
1) one with correct associativity ((5-3)-2) 
2) one with wrong associativity (5-(3-2)).

So its clear that B can never be the correct option.

Source: http://pages.cs.wisc.edu/~fischer/cs536.s08/course.hold/html/NOTES/3.CFG.html#exp
1 1 vote

Information is insufficient ie C would be the correct option. 

Grammar that is both left recursive and right recursive may or may not be ambiguous. 

example :

A → A a | b A | ε

(both left + right recursive but unambiguous)

Even though it has recursion on both sides, it can still be made to produce unique parse trees depending on the structure.

Answer:
Position:
Show:

Related questions

43 43 votes
6 answers 6 answers
17.7k
17.7k views
Kathleen asked Sep 23, 2014
17,662 views
The number of binary strings of $n$ zeros and $k$ ones in which no two ones are adjacent is$^{n-1}C_k$$^nC_k$$^nC_{k+1}$None of the above
2 2 votes
0 0 answers
2.3k
2.3k views
Kathleen asked Sep 23, 2014
2,326 views
Let synthesized attribute val give the value of the binary number generated by S in the following grammar. For example, on input 101.101, S.val = 5.625.$S \rightarrow L.L...
19 19 votes
1 answers 1 answer
8.4k
8.4k views
Kathleen asked Sep 23, 2014
8,357 views
What will be the output of the following program assuming that parameter passing iscall by valuecall by referencecall by copy restoreprocedure P{x, y, z}; begin y:y+1; z:...
47 47 votes
5 answers 5 answers
14.3k
14.3k views
Kathleen asked Sep 23, 2014
14,332 views
The number of articulation points of the following graph is$0$$1$$2$$3$