edited by
5,311 views
13 votes
13 votes
Consider the following statements:-

S1 : for LL(1) parsing of n tokens ,time complexity in the best case is O(n) and worst case is O(n4) .

S2 : for LR parsing of n tokens ,time complexity in the best case is O(n) and worst case is O(n3) .

which of the following statements are true?

a)Only S1 is correct.

b)Only S2 is correct.

c) Both S1 and S2 is correct.

d)Neither S1 nor S2 is correct.
edited by

1 Answer

Best answer
19 votes
19 votes

First let us know about the 1st statement :

The best case corresponds to the case when the grammar is an S grammar ..

S- grammar means :

A grammar where all productions are of the form  A --> TV* and also there is a restriction that (A,T) pair is used at most once considering the entire set of productions of the grammar..This is also known as linear parsing grammar.

S - grammar is a subset of LL(1) grammars as all LL(1) does not follow the 2nd restriction of S grammar which is concerning (A,T) pair..So the best case of LL(1) will be when it is an S grammar..

So in this case we will have best case of O(n) , where n is the number of tokens..

The worst and general case corresponds to dynamic programming solution which takes O(n4) for left recursive grammars and O(n3) for right recursive grammar..So worst case is O(n4)  ..

Hence statement 1 is true..

[Reference : https://en.wikipedia.org/wiki/Top-down_parsing ] 

Now let us come to second statement..

LR grammars on the other hand are more efficient than LL(1) grammars..

 Knuth proved that LR parsers were the most general-purpose parsers possible that would still be efficient in the worst cases.

"LR(k) grammars can be efficiently parsed with an execution time essentially proportional to the length of the string."

For every k≥1, "a language can be generated by an LR(k) grammar if and only if it is deterministic [and context-free], if and only if it can be generated by an LR(1) grammar."

So for LR grammars we have both best and worst case complexity of O(n) ..So statement 2 is false actually..

Hence A) option is correct ..

selected by
Answer:

Related questions

2 votes
2 votes
1 answer
1
sripo asked Nov 10, 2018
3,247 views
Can you give an example which is not LL(1) but is CLR(1)
1 votes
1 votes
5 answers
2
syncronizing asked Sep 22, 2018
1,658 views
Every LL(1) grammar is ______A.SLR(1)B.LALR(1)C.LR(1)D.Both B & C
0 votes
0 votes
2 answers
3
0 votes
0 votes
2 answers
4
eyeamgj asked May 16, 2018
306 views
it is confirmed that every LL(1) is LR(1) i.e CLR(1),but i want to know that is every LL(1) grammar is also LALR????? becz LALR is subset of CLR(1).