Context Free Grammer (CFG) for a language

68 Views Asked by At

Consider the language above $\Sigma = \{a,b,\$\}$: $$L = \left\{ x$y : x,y\in\{a,b\}^* \land \left|x\right| \ne \left|y\right| \right\}$$

I need define a CFG for this language. I've tried couple of CFGs but they all failed in one way or another.

I'd be glad for help.

1

There are 1 best solutions below

1
On BEST ANSWER

HINT: With $S$ as the initial symbol, start with these productions:

$$\begin{align*} &S\to aL\mid bL\mid Ra\mid Rb\\ &L\to aL\mid bL\mid X \end{align*}$$

Have $R$ do something similar to what $L$ does, and have $X$ generate the language

$$\big\{x\#y:x,y\in\{a,b\}^*\land |x|=|y|\big\}\;.$$

In other words, generate excess length on one side, then generate equal lengths on both sides from that point on.