Skip to content
Official PYQ · 2021

GATE CS 2021 Set 1 Q5 — Parsing (LL, LR)

Explain with AI

Consider the following statements.

  • S1:S_1: Every SLR(1)\text{SLR(1)} grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1)\text{SLR(1)}.
  • S2:S_2: For any context-free grammar, there is a parser that takes at most O(n3)O(n^3) time to parse a string of length nn.

Which one of the following options is correct?

Choose one option
View worked solution

Answer: C

Let us analyze the two statements individually:

Statement S1S_1: Every SLR(1)\text{SLR(1)} grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1)\text{SLR(1)}.

  • Part 1: Any grammar that is parsable by a deterministic parser (like LR(0)\text{LR(0)}, SLR(1)\text{SLR(1)}, LALR(1)\text{LALR(1)}, or LR(1)\text{LR(1)}) 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 SLR(1)\text{SLR(1)} grammars is a strict subset of unambiguous grammars. For example, the classic grammar: S→L=R∣RL→∗R∣idR→L\begin{aligned} S &\to L = R \mid R \\ L &\to *R \mid \text{id} \\ R &\to L \end{aligned} is unambiguous, but it is not SLR(1)\text{SLR(1)} because it contains a shift-reduce conflict in its SLR(1)\text{SLR(1)} parsing table.

Thus, S1S_1 is true.


Statement S2S_2: For any context-free grammar, there is a parser that takes at most O(n3)O(n^3) time to parse a string of length nn.

  • 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 O(n3)O(n^3) time, where nn is the length of the input string.

Thus, S2S_2 is true.

Since both statements S1S_1 and S2S_2 are true, the correct option is C.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions