I did not understand how a unambiguous grammar is derived from a ambiguous grammar? Consider the example on site: Example. How was the grammar derived is confusing to me.
Can anyone please guide me ?
And, to answer your question, there are context-free languages for which there is no unambiguous grammar. Such languages are said to be inherently ambiguous. Hence no all ambiguous grammar cannot be converted to unambiguous grammar.
In computer science, an ambiguous grammar is a context-free grammar for which there exists a string that can have more than one leftmost derivation or parse tree, while an unambiguous grammar is a context-free grammar for which every valid string has a unique leftmost derivation or parse tree.
The example has two grammars:
E → E + E | E ∗ E | (E) | a
E → E + T | T T → T ∗ F | F F → (E) | a
The unambiguous grammar was derived from the ambiguous one using information not specified in the ambiguous grammar:
With the external information, we can tell that:
a * a + b * b
is grouped as if written:
(a * a) + (b * b)
rather than as:
a * ((a + b) * b)
The second assumes that '+' binds tighter than '*', and that the operators bind from right to left rather than left to right.
How would associativity come into the picture for examples like:
S → aA | Ba A → BA | a B → aB | epsilon
This is an ambiguous grammar, so how to go about converting it to unambiguous?
I wonder if the 'epsilon' is ε, the empty string; let's analyze the grammar both ways.
The rule for B says a B is either an empty string or an a followed by a valid B, which amounts to an indefinitely long string of 0 or more a's.
The rule for A says an A is either an a or a B followed by an a. So, an indefinitely long string of a's could be an A too. So, there is no way for the grammar to choose whether a string of a's is either an A or B.
And the rule for S is no help; an S is either an a followed by an indefinitely long string of a's or an indefinitely long string of a's followed by an a. It requires at least one a, but any number of a's from one upwards is OK, but the grammar has no basis to decide between the left and right alternatives.
So, this grammar is inherently ambiguous and cannot, in my estimation, be made unambiguous; it certainly cannot be made unambiguous without other information not in our possession.
What about if ε is not the empty string?
In this case, the grammar is unambiguous as it stands (though not necessarily LR(1)). Clearly, a lot hinges on the meaning of 'epsilon' in the comment/question.
I don't think associativity affects this grammar. It generally comes into play with infix operators (such as the '+' in 'a + b').
From Wikipedia (on Recognizing ambiguous grammars):
Some ambiguous grammars can be converted into unambiguous grammars, but no general procedure for doing this is possible just as no algorithm exists for detecting ambiguous grammars.
In order to come up with the second grammar, you have to find a grammar that is
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With