Consider the following statements. S1 : Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1). S2 : For any context-free grammar, there is a parser that takes at most O(n3) time to parse a string of length n. Which one of the following option is correct?
Consider the following statements. S1 : Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1). S2 : For any context-free grammar, there is a parser that takes at most O(n3) time to parse a string of length n. Which one of the following option is correct? Correct Answer S<sub>1</sub> is true and S<sub>2</sub> is true
Answer: Option 1
Explanation:
Statement 1:Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1).
[ alt="F1 Raju.S 01-04-21 Savita D3" src="//storage.googleapis.com/tb-img/production/21/04/F1_Raju.S_01-04-21_Savita_D3.png" style="width: 463px; height: 243px;">
As you can see in the diagram Not all Unambiguous grammars are SLR(1) but All SLR(1) grammars are Unambiguous.
This Statement is True.
Statement 2:For any context-free grammar, there is a parser that takes at most O(n3) time to parse a string of length n.
This Statement is also True. There exists a standard algorithm for general Context-free grammars known as the CYK algorithm whose time complexity is O(n3).
Note:
- LR parsers have linear time(O(n)) complexity.