4.1k views

Which of the following suffices to convert an arbitrary CFG to an LL(1) grammar?

1. Removing left recursion alone
2. Factoring the grammar alone
3. Removing left recursion and factoring the grammar
4. None of the above
asked | 4.1k views
+3
S->Sa/a  here to convert into LL(1) need to be remove left recursive

for this S->ab/a here to convert LL(1) remove left factoring

then if i take a grammer which not having both left recursive and left factoring for example S->AB/a ;A->a,B->epsilon  then but still not parse by LL(1) bcz it is ambiguous so for that we have to be fulfill all three condition then we can convert into LL(1) surely  so D

LL(1) parser is top down parser.

For top down parsers, the grammar should be unambiguous, deterministic and should not be left recursive.

All the $3$ conditions must be satisfied for LL(1) parsers.

Now, even if all $3$ conditions are satisfied we cannot get an LL(1) or even LL(k) (for any $k$) grammar for even a DCFG. This is because there are DCFLs which does not have an LL(k) grammar (see ref below). On the other hand for any DCFL, we can always have an LR(1) grammar.

http://mathoverflow.net/questions/31733/can-i-have-an-ll-grammar-for-every-deterministic-context-free-language

So, option D is correct.

answered by Loyal (9.4k points)
edited by
0
can left-factoring remove all non-determinism?
+1
I think so, I can shift all the symbols to next steps that are causing non-determinism.

I have one more question. While searching something on internet, I have found that all the deterministic grammars are unambiguous. I have alsi read that some grammars are inherently ambiguous. Little confused.

I have read somewhere before that removal of non determinism does not guarantee removal of ambiguity.

?
+5
Yes, determinism guarantees unambiguity. And language of some grammar is inherently ambiguous- meaning we cannot remove ambiguity from such grammars.
0
what about LR(k) grammars?? they also need deterministic grammar right?
0
See answer now, it wasnt correct earlier..
+1
Thanx a lot arjun sir. In LL(k) or LL(1) we get conflict in the parsing table when first of two different productions having same LHS symbol have common symbol and hence in that case it wont be parsed by ll1. right?
+1
We look as First for LL(1) only - the 1 and first mean the same here. For LL(2) and on, it becomes more hard but even for any k, LL(k) parser is not possible for all DCFLs.
+1
@ Arjun sir

When we left factor a grammar and make it deterministic, it doesnt make the grammar unambiguous if it was  ambiguous. Then how can we say that every deterministic grammar is unambiguous?? Its confusing :(
0
isnt left factoring done to remove left recursion ?
+1
No, to remove non determinism
0
@vishal

then how do you remove left recurssion ?
0
A->Aa/b =>

A->bA'

A'->aA'/epsilon

Thats how.
0
@vishal

isnt this method called left factoring ?
+1
No, it is removing left recursion.

Left factoring is removing non-determinism or common prefixes.

e.g. A-->aP/aQ/aR, all three have common prefix, so we remove it by left factoring.

A-->aA'

A'-->P/Q/R.

hope that helps.
0
Someone explain the above question by @sushmitha

D. Removing left recursion and factoring the grammar do not suffice to convert an arbitrary CFG to LL(1) grammar.

answered by Boss (13.6k points)
0
why not C?Could u give me an example?
+5
If the Grammar is ambiguous, option C would fail. Removing Left factoring / Left recursion doesn't remove ambiguity in the grammar.
LL(1) properties:

1. Un ambiguous

2. Not left recursive

3. Deterministic

I think C does not make the CFG unambiguous all the time.
answered by Junior (835 points)
Answer is d . Removing left recursion and left factoring is necessary but not sufficent condition for LL(1)
answered by Loyal (9.8k points)
Yes Option D is correct.

But even if the grammar satisfies all three conditions: non left recursive, deterministic and unambiguous even then it may not be LL(1).

E.g.

S -> aAa / ^

A -> abS / ^

Here ^ is epsilon.

This grammar satisfies all three conditions but still not LL(1) because predict set for S -> aAa and S -> ^  is not disjoint.
answered by Active (1.1k points)
0
can you give me example.

even if we remove left recursion,left factoring still there ambiguity
answered by Loyal (9k points)
+3
I think D should be the answer. As not all CFG can be converted into LL(1) even if after removing left recursion and factoring.
An ambiguous grammar can't be LL(1).

Removing left recursion and factoring the grammar not always guarantees the un-ambiguity of grammar.

i.e.  Ans (D)
answered by (239 points)
LL(1)  grammar  should be unambigous

even by eliminating leftfactoring we cannot eliminate the ambiguity

so ambigous will always there even if we convert the ambigous grammar into determistic grammar.

example:- s->iEts /iEtses/a                         s->iEtss'/a

E->b                              =>          s'->epsilon/es

non determistic grammar                              E->b

determisticgrammar

so by leftrecursive and factoring will not make the grammar unammbigous ....option d  is correct
answered ago by (23 points)