•The term “inherently ambiguous language” is used instead of “ambiguous
language”.
•This is because all the grammars which generate that language are
ambiguous.
Important Points-
If a grammar is ambiguous, it does not imply that its
language will be ambiguous too.
If a grammar is ambiguous, its language may be
unambiguous.
If a grammar is ambiguous, its language will be
unambiguous when there exists at least one
unambiguous grammar which generates that
language.
Example Of Not An Inherently Ambiguous
Language-
Consider the following language-
L = an , n>=0
•This language consists of strings having any number of
a’s.
•Few grammars which generate this language are-
Grammar-01:
S → aS / ∈
(Unambiguous Grammar)
Grammar-02:
S → aS / a / ∈
(Ambiguous Grammar)
• Grammar-03:
• S → aS / Sa / ∈
• (Ambiguous Grammar)
• Grammar-04:
•
• S → aS / a / Sa / ∈
• (Ambiguous Grammar)
• All the above grammars generate the same language L = an ,
n>=0.
• Grammar-01 is unambiguous.
• Since there exists at least one unambiguous grammar which
generates language L.
• Therefore, L is not an inherently ambiguous language.
Example Of An Inherently Ambiguous Language-
Consider the following language-
L = { anbncm } ∪ { anbmcm }
There exists only one grammar which generates this language L.
The grammar is-
S → S1 / S2
S1 → S1C / A
A → aAb / ∈
S2 → aS2 / B
B → bBc / ∈
• This grammar is ambiguous in nature.
• There exists no language that generates this language
L and is unambiguous.
• Therefore, L is an inherently ambiguous language.
Ambiguous Grammar Unambiguous Grammar
A grammar is said to be ambiguous if for at least A grammar is said to be unambiguous if for all the
one string generated by it, it produces more than
strings generated by it, it produces exactly one-
one-
•parse tree
•parse tree
•or derivation tree
•or derivation tree •or syntax tree
•or syntax tree
•or leftmost derivation
•or leftmost derivation
•or rightmost derivation •or rightmost derivation
For ambiguous grammar, leftmost derivation and For unambiguous grammar, leftmost derivation
rightmost derivation represents different parse and rightmost derivation represents the same
trees. parse tree.
Ambiguous grammar contains less number of Unambiguous grammar contains more number of
non-terminals. non-terminals.
For ambiguous grammar, length of parse tree is For unambiguous grammar, length of parse tree
less. is large.
Ambiguous grammar is faster than unambiguous
grammar in the derivation of a tree. Unambiguous grammar is slower than ambiguous
grammar in the derivation of a tree.
(Reason is above 2 points)
Example- Example-
E → E + T / T
E → E + E / E x E / id T → T x F / F
(Ambiguous Grammar) F → id
(Unambiguous Grammar)