Given the following two grammars : G1 : S → AB | aaB A → a | Aa B → b G2 : S →…

2014

Given the following two grammars :

G1 : S → AB | aaB

        A → a | Aa

        B → b

G2 : S → a S b S | b S a S | λ

Which statement is correct ?

  1. A.

    G1 is unambiguous and G2 is unambiguous.

  2. B.

    G1 is unambiguous and G2 is ambiguous.

  3. C.

    G1 is ambiguous and G2 is unambiguous.

  4. D.

    G1 is ambiguous and G2 is ambiguous.

Attempted by 124 students.

Show answer & explanation

Correct answer: D

Answer: Both grammars are ambiguous.

Proof for the first grammar (G1):

  1. Consider the string "aab".

  2. Derivation 1 (using S → AB): S ⇒ AB ⇒ AaB ⇒ aaB ⇒ aab

  3. Derivation 2 (using S → aaB): S ⇒ aaB ⇒ aab

  4. Since there are two distinct derivations for the same string, G1 is ambiguous.

Proof for the second grammar (G2):

  1. Consider the string "abab".

  2. Derivation 1 (root expands with a S b S, left S → λ, right S → a S b S with both inner S → λ):

  3. S ⇒ a S b S ⇒ a (λ) b (a S b S) ⇒ a b a b

  4. Derivation 2 (root expands with a S b S, left S ⇒ b S a S with both inner S ⇒ λ, right S ⇒ λ):

  5. S ⇒ a S b S ⇒ a (b S a S) b S ⇒ a (b λ a λ) b λ ⇒ a b a b

  6. These are two different parse trees/derivations yielding the same string, so G2 is ambiguous.

Conclusion: Both grammars are ambiguous because we have exhibited specific strings with two distinct derivations in each grammar.

Explore the full course: Mppsc Assistant Professor