Reputation: 105
I have this question:
Show that the grammar. S->aS|aSbS|Ɛ is ambiguous and find the unambiguous grammar.
I tried learning from the internet whatever I could about ambiguous grammars but most of those try on the same old examples and I feel that they don't convey the approach properly about converting an ambiguous grammar to an unambiguous one. I know there is no one definite method to go about it. I tried a hit an trial approach of sorts and here's what I got:
First of all proof that the given Grammar is ambiguous : try getting aab from the above grammar
You will find that there are at least two ways to go about it.
So I thought about it and came up with a solution using hit and trial.
S -> aT|epsilon T -> aTbT | epsilon
I have no proof of correctness or too much of a solid thought behind why I came up with this but I at least could not make two different parse trees for aab using this new grammar.
Is this answer correct. I would appreciate it a lot if someone tells me how this is actually done and the rationale behind it instead of doing a hit and trial attempt like me.
Upvotes: 0
Views: 3467
Reputation: 20391
S -> aT | eps
T -> aSbS | eps
Use induction. The only way for a rule to introduce ambiguities is if itself is ambiguous or if any of the rules it depends on are ambiguous. S does not introduce ambiguity so T does not introduce ambiguity since it only depends on S and is itself not ambiguous.
Upvotes: 0