# Grammar for all words other than $wq,qw$

I want to generate a grammar that can't generate the words $$qw$$ and $$wq$$ but can generate the word $$qwwq$$. In other words, $$L(G)=\{m ∈ \{q,w\}^* \mid m \neq wq,qw \}$$.

My grammar:

\begin{align} &S \to qSw \mid wSq \mid qXq \mid wXw\\ &S \to qYw \mid wYq \mid q \mid w\\ &X \to qX \mid wX \mid qXw \mid wXq \mid ε \\ &Y \to qw \mid wq \\ \end{align}

• @D.W. I don't a finite number of words, what I want is the whole words in {q,w}* except the "qw" and the "wq" Mar 26 at 23:38
• cs.stackexchange.com/q/1331/755
– D.W.
Mar 26 at 23:51
• Your language is regular as it's the complement of the finite (and hence regular) language $\{wq, qw\}$. Mar 26 at 23:53
• @Steven yupp, do you have any idea how we can write it i just wrote CFG because i want to convert it to CNF after Mar 27 at 0:46

How about \begin{align*} S&\to q\mid w\mid qqB \mid wwB \mid qwA\mid wqA\mid \varepsilon \\ A&\to qB\mid wB \\ B&\to qB\mid wB\mid \varepsilon \end{align*}
• @MandiJoseph You're right I forgot to include the $B$ after the $qq$ and $ww$. I've edited it. By the way you will want to add a rule $S\to \varepsilon$ to the grammar you've written. Mar 27 at 0:55