Prove that language is context-free $C=\{x\#y \mid x,y\in \{a,b\}^*\wedge x\neq y\}$

3.4k Views Asked by At

Prove that this language is context-free: $C=\{x\#y|x,y\in \{a,b\}^*\wedge x\neq y\}$.
I try to construct a grammar:
$S\rightarrow C_a\#C_b|C_b\#C_a$
$C_a\rightarrow XC_aX|a$
$C_b\rightarrow XC_bX|b$
$X\rightarrow a|b$

Is it good ? I can try to prove it.

Edit
$S\rightarrow C_abY|C_baY$
$C_a\rightarrow XC_aX|aY\#$
$C_b\rightarrow XC_bX|bY\#$
$Y\rightarrow Ya|Yb|\epsilon$
$X\rightarrow a|b$
$S\rightarrow RT | TR$
$R\rightarrow aRa | aRb|bRa|bRb |\#$
$T\rightarrow a|b|Ta|Tb$

2

There are 2 best solutions below

12
On BEST ANSWER

I can’t come up with a good hint this time, but I still want to leave some work for you, so I’ve written the following context-free grammar:

$$\begin{align*} &S\to XA\mid YB\\ &X\to aXa\mid aXb\mid bXa\mid bXb\mid bZ\#\\ &Y\to aYa\mid aYb\mid bYa\mid bYb\mid aZ\#\\ &A\to aZ\\ &B\to bZ\\ &Z\to aZ\mid bZ\mid\lambda \end{align*}$$

It almost works, but not quite: there are some words in the language that it doesn’t generate. The missing ones can quite easily be generated separately, but first you’ll have to figure out what this one is doing. To get you started: basically it’s designed to ensure that the two parts of the word differ in at least one position.

1
On

Your current grammar will only produce string x#y where x and y both have odd length, and the only characters guaranteed to be different between x and y are in the exact middles of the two strings. There are more possibilities for x and y and more ways that the two strings could be different, e.g. being the same length but only guaranteed to be differing in another position besides the middle, or simply being different lengths even if x and y are all "a" or all "b". In fact if the string lengths of x and y are different then x and y could be anything.