0

So I've been struggling for the past hour with $G=(\{S\},\{a,b\},P,S)$ with productions $S\to aaSb | abSbS | \varepsilon$. I need to prove whether this grammar is ambiguous or not. Thus far I think it is ambiguous, possibly cuz of abSbS, but not sure (mainly since I have no idea how to prove it's not) . Can anyone help? I tried several words to no avail.

Thomas Klimpel
  • 5,440
  • 29
  • 69

2 Answers2

2

Here is an outline of a parsing procedure. To prove that the grammar is unambiguous, you have to show that it works (parses the string), and furthermore that the parsing tree it produces is the unique one generating the string. If this question came up in the context of a course on compilers, then you should have learned the necessary tools.

The parser is a DPDA. The stack starts initialized with S, and then works according to the following rules, reading the input from left to right.

  1. If the top-of-stack is S:

    • If the string starts aa, replace S with AS (S on top).
    • If the string starts ab, replace S with BS.
    • If the string starts and ends a, reject.
    • If the string ends, accept.
    • If the string starts b, delete S and process b again.
  2. If the top-of-stack is A:

    • If the string starts b, delete A.
    • Otherwise, reject.
  3. If the top-of-stack is B:

    • If the string starts b, replace B with S.
    • Otherwise, reject.

(Unless explicitly stated, the automaton consumes the characters it reads from the string.) At the end of parsing, the automaton rejects unless the stack is empty.

I'll let you figure out how to construct the actual parse tree. You'll also have to figure out how to prove that this parsing tree is the unique one (if it is indeed the case).

Yuval Filmus
  • 280,205
  • 27
  • 317
  • 514
1

To show that this CFG is unambiguous you can try convert it into a LL(1) (preserving ambiguity). Then, if you can do it, the original CFG will be unambiguous too (o course, if you cannot, you won't show anything about ambiguity). If you use left-factoring on S → aaSb|abSbS|ε you get: S → aT|ε, T → aSb|bSbS. Show that this CFG is LL(1) and you will get your prove (because all LL(1) grammars are unambiguous and left-factoring preserves ambiguity).

Andres
  • 39
  • 2