I came across a very hard interview exam. It was asked wrote an unambiguous grammar for two following language, Who can hint it to solve it?
1) $L = \{a^n b^{2n} c: n\geq 0\} \cup \{a^{2n} b^n d: n\geq 0\}$
2) $L = \{a^n b a^{2n}: n \geq 0\} \cup \{a^{2n} b a^n: n\geq 0\}$
I know If $L_1,L_2$ are two disjoint context-free languages which are not inherently ambiguous, then $L_1 \cup L_2$ is also a context-free language which is not inherently ambiguous. but I couldent write grammar for these.
Spoiler alert.
My solutions.
1)
$$ L ::= Bc \mid Cd \\ B ::= aBbb \mid \epsilon \\ C ::= aaCb \mid \epsilon $$
2)
$$ L ::= A \mid B \\ A ::= b \mid aAaa \\ B ::= aaba \mid aaBa $$
Notice that the base case of the B recursion is $aaba$, not $\epsilon$. This removes ambiguity.