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.
-
2Try to use the definition of ambiguous. – Yuval Filmus Feb 06 '16 at 14:31
-
1Much helpful, so advice. Not like I've been trying to find a word that would prove it's ambiguous for the past hour. – Bumblebee Feb 06 '16 at 14:40
-
3To show that the grammar is ambiguous, you need to find a word with two different derivation trees. If you understand all of this, just do that. Don't ask others to do it for you. – Yuval Filmus Feb 06 '16 at 16:33
-
1@tisasecret A grammar this size is likely to exhibit ambiguity for some small world, or not at all. So trying all the words ordered by length should work in under an hour. – Raphael Feb 06 '16 at 16:59
-
@YuvalFilmus are you sure about $abbaab$? I tried to solve that, it looks unambiguous – 3SAT Feb 06 '16 at 21:48
-
In contrast, to show that the grammar is unambiguous, give a parsing procedure, and show that the parse tree it produces is the unique parse tree generating the given word. – Yuval Filmus Feb 06 '16 at 22:04
-
@YuvalFilmus I'm not sure we are allowed to give a parsing procedure. Ambiguity is indepent of how you do the parsing. A smart parser may do away with ambiguities in the grammar (say, rule precedences). – Raphael Feb 07 '16 at 17:45
-
See here for techniques for showing that a grammar is unambiguous. Something not mentioned there (yet): prove by induction that all sentential forms have exactly one derivation. (This seems possible here. Hint: what can words resp. sentential forms start with?) – Raphael Feb 07 '16 at 17:50
-
@Raphael We're allowed to use whatever we want, as long as it constitutes a proof. – Yuval Filmus Feb 07 '16 at 20:39
-
I ended up figuring it out on my own. I did not want the exact word, I wanted an explanation of why it would be unambiguous if it's not ambiguous, since I could not figure it out at the time. In the end I found it on my own. When you're only told the theory in class and no examples, this gets a bit hard. Anyway I concluded that it's unambiguous as you can't start with the different nonterminals on all the rules and have more than one parse tree, at least that was my reasoning. Thanks raphael for the induction proof, good read, thankfully that's a bit too much than what they want. – Bumblebee Feb 08 '16 at 01:26
-
I also thought it was undecidable , so I'll have to ask my teacher if he would accept something like that. – Bumblebee Feb 08 '16 at 01:27
-
Good job! (Undecidable doesn't mean that it's impossible to find a proof for any given instance!) – Raphael Feb 08 '16 at 07:13
-
@YuvalFilmus Of course. "Allowed" in the sense of "can lead to a proof at all". – Raphael Feb 08 '16 at 07:14
2 Answers
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.
If the top-of-stack is
S
:- If the string starts
aa
, replaceS
withAS
(S
on top). - If the string starts
ab
, replaceS
withBS
. - If the string starts and ends
a
, reject. - If the string ends, accept.
- If the string starts
b
, deleteS
and processb
again.
- If the string starts
If the top-of-stack is
A
:- If the string starts
b
, deleteA
. - Otherwise, reject.
- If the string starts
If the top-of-stack is
B
:- If the string starts
b
, replaceB
withS
. - Otherwise, reject.
- If the string starts
(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).

- 276,994
- 27
- 311
- 503
-
"The parser is a DPDA." -- that's crucial, but not sufficient. The automaton has to "implement" the exact grammar (whatever that means) if this is supposed to show anything. – Raphael Feb 07 '16 at 17:46
-
@Raphael This is not intended to be a complete proof – after all it's not my exercise. If you want you can spend some time fleshing it out. – Yuval Filmus Feb 07 '16 at 20:38
-
2I don't think this leads to a proof at all. Not because of the automaton you sketch, or what follows, but because of the logic of it. The problem is that this "technique" can be used to "prove" that ambiguous grammars are unambiguous by giving a deterministic automaton for the same language. How would you allow one automaton but not another for the proof? – Raphael Feb 08 '16 at 07:16
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).

- 39
- 2