2

I´m having trouble to see if this grammar it´s ambiguous or not. How can I check if it is ambiguous?

G = ({S,A,B}, {0,1}, P, S}

P:

S → 0B | 1A

A → 0 | 0S | 1AA

B → 1 | 1S | 0BB

Suri
  • 23
  • 1
  • 3
  • I will give you a small hint. Try for example to reproduce 0011 from the above grammar. Give it a try, and if you are still not able to decide, ask again. – Tinxuanna Nov 25 '21 at 13:07
  • 1
    So I´ve reproduced 0011 and the only derivation that I was able to do was this one: S->0B->00BB->00B1->0011 So if there is no other derivation for 0011 I assume that this grammar it´s unambigous. Is that right? @Tinxuanna – Suri Nov 25 '21 at 13:52
  • Good, I have tried this example too and seems that only one leftmost derivation or rightmost derivation (1100) is produced, if parsing this input string. So the grammar is unambigous. – Tinxuanna Nov 26 '21 at 07:16
  • I am going to answer and please give a mark if you think the answer was useful. – Tinxuanna Nov 26 '21 at 07:17

2 Answers2

9

What you'd like is an algorithm that, for a given context-free grammar, tells you whether it's ambiguous or not. However, it's been proven that no such algorithm can exist.

One approach is to apply a parser-construction technique that's known to only work for a subset of unambiguous grammars.

  • If the construction succeeds, then you know for sure that the grammar is unambiguous.
  • If the construction fails, then you still don't know: the grammar might be ambiguous, or it might be unambiguous but outside the subset that the technique can handle. However, the way in which the construction fails may give you insight into the problem.

For instance, if you construct the LR(0) automaton for your grammar, you get 2 states with conflicts, so that's inconclusive. But you know that if it is ambiguous, then any sentence that demonstrates the ambiguity would have to involve at least one of those states. (Any sentence that avoids those states could be parsed deterministically.) So you can concentrate on that area of the grammar.

Another approach is to use heuristics. E.g. the production B -> 0BB looks like it might cause ambiguity. (Having the two Bs right next to each other is kind of suspicious.) So you'd concentrate on the BB and ask if there's some way that that can derive a form XYZ where the two Bs 'fight' over the Y. I.e. if there's one derivation where

B -> X   and B -> YZ

and another where

B -> XY  and B -> Z

(and Y is non-empty, of course) then you can use that to demonstrate ambiguity.

Michael Dyck
  • 2,153
  • 1
  • 14
  • 18
0

A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. If the grammar is not ambiguous, then it is called unambiguous.

Thus, let's try to reproduce 0011 from the above grammar.

Example for 0011: S->OB->00BB->001B->0011

Example for 1100: S->1A->11AA->110A->1100

It seems that only one leftmost derivation or rightmost derivation is produced, if parsing this input string. So the grammar is unambigous.

Tinxuanna
  • 206
  • 1
  • 3
  • 16
  • Checking the derivation of one or two sentences isn't enough to prove that a grammar is unambiguous. – Michael Dyck Nov 27 '21 at 15:50
  • 1
    By definition, an ambiguous grammar has at least one sentence with multiple derivations, but it can have lots of sentences with single derivations. If you happen to pick a sentence with a single derivation, that doesn't tell you whether or not a sentence with multiple derivations exists. – Michael Dyck Nov 27 '21 at 15:57
  • You are right @Michael Dyck and thank you for your comment. I wonder how we can be sure in order to prove that. I am going to search more and come up with a suggestion. – Tinxuanna Nov 29 '21 at 06:47