Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What makes a context free grammar ambiguous?

share|cite|improve this question
Have you looked at the examples here? – Zhen Lin Jan 16 '11 at 18:36
Are you asking for a definition or some kind of intuitive explanation? – Qiaochu Yuan Jan 16 '11 at 18:41
Despite being answered and all, don't you think it fits better on perhaps? – Asaf Karagila Jan 16 '11 at 21:56
I was unaware this stackexchange existed... thanks! :) – Ulkmun Jan 17 '11 at 0:30
Asaf: No, since this question is far from being research level. – Raphael Mar 4 '11 at 11:54
up vote 15 down vote accepted

And now, an answer to the actual question...

A grammar is ambiguous if there's a word which has two different derivation trees. You'll have to look up derivation tree in your textbook since drawing them is awkward, but the idea that it doesn't matter in which order you're doing the derivations as long as it's basically the same derivation.

For example, consider the grammar $S \rightarrow AS|a, A\rightarrow a$. Let's derive $aa$ twice: $$ S \rightarrow AS \rightarrow Aa \rightarrow aa $$ $$ S \rightarrow AS \rightarrow aS \rightarrow aa $$ These derivations share the same derivation tree:

Derivation tree

However, consider now the grammar $S\rightarrow SS|a$, generating the same language. There are now two "really" different derivations for $aaa$: $$S \rightarrow SS \rightarrow Sa \rightarrow SSa \rightarrow Saa \rightarrow aaa$$ $$S \rightarrow SS \rightarrow aS \rightarrow aSS \rightarrow aaS \rightarrow aaa$$

In the first derivation, the first two $a$'s are derived together, whereas in the second derivation, it is the second two $a$'s which are derived together. So it's $(aa)a$ vs. $a(aa)$.

First derivation tree

Second derivation tree

In this case, the language $a^+$ has an unambiguous grammar (the first one I exhibited), but in other cases, all grammars are ambiguous; such languages are called inherently ambiguous. See my other answer for more on that.

share|cite|improve this answer
This is exactly what I was looking for, Thanks. – Ulkmun Jan 16 '11 at 21:19
Note that "basically the same derivation" is also denoted by using the terms (left|right) derivation. – Raphael Mar 4 '11 at 11:56
There might be more than two derivations in general. – Yuval Filmus Mar 4 '11 at 16:55
@YuvalFilmus:Could you explain the difference between the two examples.I can't see any difference and it might be because I'm weak in CFG's. – justin Feb 12 at 9:26
@justin You have to look at the derivation tree. Find a source explaining them, and draw the trees. – Yuval Filmus Feb 12 at 9:30

Intuitively, what makes a language inherently ambiguous is that it is composed of distinct parts which have some intersection; the intersection cannot be "singled out", and therefore the only way to approach is is through the distinct parts.

For example, the language of words of the form $a^xb^yc^z$, where $x=y$ or $y=z$, is context-free, since it's the union of $a^nb^nc^*$ and $a^*b^nc^n$. However, words of the form $a^nb^nc^n$ cannot be "singled out" since that language is not context-free, so we would expect the language to be inherently ambiguous.

The formal proof (using Ogden's lemma) goes like this. Consider the word $a^nb^nc^{n!}$, where $n$ is "big enough". By Ogden's lemma, the $a^nb^n$ can be pumped up, and so we can derive $a^{n!}b^{n!}c^{n!}$. Similarly, we can derive the same word from $a^{n!}b^nc^n$. The two derivations overlap (over the middle part) and so must be "inherently" different (i.e. have different derivation trees).

Another example, from the world of regular languages, is as follows. Consider the language over the alphabet $\{a,b\}$ of all non-empty words having either an even number of $a$'s or an even number of $b$'s (or both). Does it have a disjoint representation as a union $L_1^+ \cup \cdots L_m^+$ (here $+$ is the Kleene plus)? Intuitively, the problem is that a word with both $a$ and $b$ even is "ambiguous".

The formal proof goes by the usual pumping lemma. Pick an odd $n$ that is greater than the pumping length of all $L_i^+$'s in a given representation. The word $a^n b^{n!}$ is generated by a unique $L_i^+$. By the pumping lemma, $L_i^+$ also generates $a^{n!} b^{n!}$. Similarly, the word $a^{n!} b^n$ is generated by a unique $L_j$, which also generates $a^{n!} b^{n!}$. If $i=j$ then $L_i^+$ generates the concatenation, which has both $a$ and $b$ odd - so $i \neq j$ and we get inherent ambiguity.

share|cite|improve this answer
Are you certain this is a necessary criterion? – Raphael Mar 4 '11 at 11:57
No, but all the examples I know are of this form. It is conceivable that there are other, wildly different specimens. – Yuval Filmus Mar 4 '11 at 16:54

A grammar said to be ambiguous if it has multiple derivations for a word. if there are 1000 word for which grammar has unique way , but even for one word it has more than one path it will said to be ambiguous. Lets look in the following examples :

Example 1 :

S -> XaaX/ aX

X -> Xa/Xb/ ^

let us drive one word "aabaa" from two ways:

S => XbaaX => Xa baaX => Xa abaa ^ => ^aabaa => aabaa

S => aX => a Xa => a Xa a => a Xb aa => a Xa baa => a ^ abaa => aabaa

Example 2:

S → AB | C A → aAb | ab B → cBd | cd C → aCd | aDd D → bDc | bc

now we will drive aabbccdd from two ways

S => AB => aAb cBd => aabbccdd

S=> C => aCd => aaDdd => aa bDc dd => aab bc cdd => aabbccdd

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.