2

I am not able to come up with the solution.

EDIT : I was trying something like this :

S $\to$ AB

A $\to$ abB | ^

B $\to$ Acd

But clearly it does not makes any sense. For every call to A there must be a call to B. I am not just able to express it.

1 Answers1

1

If this solution seems strange, it may seem less so if you tackle this problem first. We introduce two non-terminals, $L_{1}$ and $L_{2}$ - the first will produce all legal strings with more $a$s than $d$s, and the second will produce more $d$s than $a$s. For the first we give the production rules \begin{align} &L_{1} \to aL_{1}d\\ &L_{1} \to M_{1} \\ &M_{1} \to aM_{1}c \\ &M_{1} \to N_{1} \\ &N_{1} \to bN_{1}c \end{align} Similarly, \begin{align} &L_{2} \to aL_{2}d\\ &L_{2} \to M_{2} \\ &M_{2} \to bM_{2}d \\ &M_{2} \to N_{2} \\ &N_{2} \to bN_{2}c \end{align} Clearly distinguishing between $N_{1}$ and $N_{2}$ is unnecessary, but I think it makes the presentation more readable. Finally, to formally put this into a proper CFG we add in \begin{align} S \to L_{1}\\ S \to L_{2}\\ N_{1} \to \varepsilon\\ N_{2} \to \varepsilon \end{align} where $S$ is the start symbol, and $\varepsilon$ is the empty string.