Consider the following statements.
- Every grammar is unambiguous but there are certain unambiguous grammars that are not .
- For any context-free grammar, there is a parser that takes at most time to parse a string of length .
Which one of the following options is correct?
View worked solution
Answer: C
Let us analyze the two statements individually:
Statement : Every grammar is unambiguous but there are certain unambiguous grammars that are not .
- Part 1: Any grammar that is parsable by a deterministic parser (like , , , or ) is guaranteed to be unambiguous because the parser can uniquely determine the derivation (and thus the parse tree) for any valid string.
- Part 2: The class of grammars is a strict subset of unambiguous grammars. For example, the classic grammar: is unambiguous, but it is not because it contains a shift-reduce conflict in its parsing table.
Thus, is true.
Statement : For any context-free grammar, there is a parser that takes at most time to parse a string of length .
- General context-free grammar parsing algorithms, such as the CYK (Cocke-Younger-Kasami) algorithm and Earley's algorithm, can parse any arbitrary context-free grammar in at most time, where is the length of the input string.
Thus, is true.
Since both statements and are true, the correct option is C.
Free practice · Your answers on this page are not saved.
