43.0k views
5 votes
In each case below, show that the grammar is ambiguous, and find an equivalent unambiguous grammar.

(a) S→SS∣ab∣a
(b) S→ABA,A→aA∣ϵ,B→bB∣ϵ

1 Answer

4 votes

Final answer:

The provided ambiguous grammars can be shown to be ambiguous by deriving the same string in more than one way. Equivalent unambiguous grammars are then presented to enforce a single, clear way of deriving each string, thus removing the ambiguity.

Step-by-step explanation:

Demonstrating Ambiguity in Grammars and Creating Unambiguous Versions

The grammars presented have ambiguity, which can be shown by deriving the same string from the grammar in more than one way, leading to multiple parse trees for the same string.

Ambiguous Grammar

  1. Original Grammar (a): S → SS | ab | a

    This grammar is ambiguous because the string 'aabb' can be derived in two distinct ways: by parsing as (SS)ab where S → a and S → ab, or as a(SS)b where S → ab and S → b (there is no 'b' production but for the sake of argument).

  2. Equivalent Unambiguous Grammar (a): S → Xab | a
    X → aX | ε

    In this unambiguous version, the grammar enforces a specific order of derivation, removing ambiguity.

  3. Original Grammar (b): S → ABA
    A → aA | ε
    B → bB | ε

    For grammar (b), the string 'aabab' can be derived with A producing 'aa', B producing 'b', and A producing 'ab', or with A producing 'a', B producing 'ab', and A producing 'a', leading to ambiguity.

  4. Equivalent Unambiguous Grammar (b): S → aSb | ε

    Here, each 'a' is forced to be followed by a 'b', matching them one-to-one and eliminating ambiguity.

User Richard Tingle
by
9.0k points