I went through a question asking me to choose the inherently ambiguous language among a set of options.
$$L_1 = \{a^nb^mc^md^n \;|\; m,n \geq 1\}\cup \{a^nb^nc^md^m \;|\; m,n \geq 1\}$$ $$and$$ $$L_2 = \{a^nb^mc^m \;|\; m,n \geq 1\}\cup \{a^nb^nc^m \;|\; m,n \geq 1\}$$
The solution said that $L_1$ is ambiguous while $L_2$ isn't. It generated the following grammar for $L_1$
$S \rightarrow S_1\;|\;S_2$
$S_1 \rightarrow AB$
$A \rightarrow aAb\;|\;ab$
$B \rightarrow cBd\;|\;cd$
$S_2 \rightarrow aS_2d\;|\;aCd$
$C \rightarrow bCc\;|\;bc$
Now for the string abcd
, it will generate two parse trees; so it is ambiguous.
But a similar grammar can be created for $L_2$ too
$S \rightarrow S_1|S_2$
$S_1 \rightarrow Ac$
$A \rightarrow aAb\;|\;\epsilon$
$S_2 \rightarrow aB$
$B \rightarrow bBc\;|\;\epsilon$
And it will also generate two parse trees for abc
. Why isn't it ambiguous then?
If you need, $L_2$ can be written as $\{a^nb^pc^m\;|\; n=p \;\; or \;\; m=p\}$