1

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.

  • I think a trial and error method is fine for this exercise. It's a search problem. Either you write a parser to search for you, or you search yourself. It's only a short sentence so it's not a lot of work. The proof consists of two valid derivation sequences which are different yet yield the same sentence. I won't do it for you; this is what practise is for! – Jurgen Vinju Sep 02 '20 at 19:12
  • Ok, can there be multiple answers(unambiguous grammars) to this question? – Vaibhav Chopra Sep 02 '20 at 19:19
  • I think i have found another answer to this question and there they have defined a different unambiguous grammar – Vaibhav Chopra Sep 02 '20 at 19:20
  • Really SO is not for homework stuff. I like grammars so I answered, but perhaps it's best that you remove this question and use SO for real coding issues. Typically people ask questions that have source code as an answer. Have fun! – Jurgen Vinju Sep 25 '20 at 17:17

2 Answers2

0

S → U | T
U → aS | aTbU
T → aTbT | Ɛ

  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Jun 19 '22 at 16:56
0
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.

Björn Lindqvist
  • 19,221
  • 20
  • 87
  • 122