The grammar A → AA | (A) | ε is not suitable for predictive-parsing because…

2005

The grammar A → AA | (A) | ε is not suitable for predictive-parsing because the grammar is

  1. A.

    ambiguous

  2. B.

    left-recursive

  3. C.

    right-recursive

  4. D.

    an operator-grammar

Attempted by 197 students.

Show answer & explanation

Correct answer: A

Answer: ambiguous

Explanation:

  • The grammar is ambiguous. Simple counterexample: the empty string ε can be derived in at least two different ways:

    • Derivation 1: A ⇒ ε (use A → ε).

    • Derivation 2: A ⇒ AA ⇒ ε ε (use A → AA, then each A → ε).

  • Because there are multiple distinct parse trees for the same string, the grammar is ambiguous. Predictive (deterministic top-down) parsing requires a single, unambiguous parse for each sentence, so ambiguity makes the grammar unsuitable.

  • The grammar is also immediately left-recursive (A → AA). Immediate left recursion causes infinite recursion in naive top-down parsers and must be removed or transformed for predictive parsing.

  • Summary: The primary stated reason is ambiguity (the grammar allows multiple parse trees for some strings). Additionally, immediate left recursion is another problem for predictive parsing.

  • A common unambiguous, non-left-recursive grammar for balanced parentheses is:

    • S → (S)S | ε

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir