Skip to content
Official PYQ · 1999

GATE CS 1999 Q2.15 — Parsing (LL, LR)

Explain with AI

A grammar that is both left and right recursive for a non-terminal, is

Choose one option
View worked solution

Answer: C

To determine if a grammar that is both left and right recursive for a non-terminal is ambiguous, we must consider whether the recursive non-terminal is useful (i.e., reachable from the start symbol and capable of deriving a terminal string).

For a grammar to be ambiguous, there must exist at least one string in its language L(G)L(G) that has more than one distinct parse tree. Consider the following cases:

  • Case 1 (Ambiguous): If the non-terminal AA is reachable and can derive terminal strings, such as in:
    S→AS \rightarrow A
    A→aA∣Aa∣bA \rightarrow aA \mid Aa \mid b
    The string abaaba has multiple distinct parse trees, making the grammar ambiguous.
  • Case 2 (Unambiguous): If the recursive non-terminal is unreachable or cannot derive any terminal string, such as in:
    S→xS \rightarrow x
    A→aA∣AaA \rightarrow aA \mid Aa
    Here, the language is L(G)={x}L(G) = \{x\}. Since the only string in the language has a unique parse tree, the grammar is unambiguous.

Because the ambiguity depends on whether the grammar is reduced and the non-terminal is useful, the given information is not sufficient to decide.

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

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