CFG and PDA for w1#w2

4k Views Asked by At

Looking for a Context Free Grammar and Push Down Automata to describe a language made of two words, separated by a #, where the first words is not equal to the second word. For this example, we can use the binary alphabet of {0,1}.

$$L = \{ w_1\#w_2 \mid w_1,w_2 \in \{0,1\}^*, w_1 \ne w_2 \}$$

A PDA or CFG for $L = \{ w_1\#w_2 \mid w_1,w_2 \in \{0,1\}^*, w_2 \ne w_1^R \}$, where the second word is not the inverse of the first is relatively easy. For the PDA, the first word will come off the stack in reverse order and you need only compare it to the second word as it is stepped through. Similarly, with a CFG, we can construct it from the outside in, ensuring that the same character is not used on both sides of the divider.

Without a second stack or using a queue, I do not see a way to construct this as a PDA. I am having similar trouble envisioning the CFG.


Partial Push-Down Automata Solution:

PDA Solution This solution handles $|L|>=2$,$|w_1|=0$, $|w_2|=0$ and |w_1|=|w_2| but does not account for $|w_1|!=|w_2|$ where $|w_1|$ & $|w_2| > 0$

1

There are 1 best solutions below

7
On

Corrected.

Here is a general descriptions of a PDA; I leave the details to you.

Have the PDA push the input onto the stack. At any point before reading a $\#$ — say the $n$-th symbol — it can ‘decide’ to stop pushing and just flush the rest of the input up through the $\#$; its state at that point should indicate whether the last input before the flush was $0$ or $1$. It can then use the stack to count $n$ symbols through the part of the input that follows the $\#$, and use the state to determine whether the $n$-th symbol of this part differs from the $n$-th symbol of the string. It can accept by state if this is the case, or if there is no $n$-th symbol after the $\#$, or if it reads a second $\#$.

Added: Here’s the hard part of a CFG; the mismatched symbols will be a green one to the right of the $\#$ and a purple one to the left of the $\#$. All you have to do is work out what it’s doing and then add the appropriate productions for $X$ and $Y$.

$$\begin{align*} &S\to 0A\color{green}0Y\mid 1A\color{green}0Y\mid 0B\color{green}1Y\mid 1B\color{green}1Y\\ &A\to 0A0\mid 0A1\mid 1A0\mid 1A1\mid \color{purple}1X0\mid \color{purple}1X1\\ &B\to 0B0\mid 0B1\mid 1B0\mid 1B1\mid \color{purple}0X0\mid \color{purple}0X1\\ \end{align*}$$