This question already has an answer here:

Construct grammar given the following language!

$ L = \{(ab)^{n+1}u(ba)^n|n>0, l_c(u) = 1, u\in\{a,c,d\}^* \}$

My interpretation in a less accurate way:

  1. $(ab)^{n+1}$ says we need to concatenate $(ab)$ at leat one time with itself (since $n > 0$)
  2. $u$ represents one word from the set of $\{a,c,d\}^*$, (except the words, that contain the letter $c$ more than once). The set $\{a,c,d\}^*$ is equivalent to $\bigcup \limits _{i=0}^\infty \{a,c,d\}^i$
  3. $(ba)^n$ means we need to use $(ba)$ at least once.

My soloution for the rules so far:

$S\to abA$
$A\to abB|abA$
$B\to aB|dB|c|cC$
$C\to baD$
$D\to baD|ε$


  • $S$ - start variable
  • $ε$ - null string, end of the word
  • $l_c(u)$ - the exact numbers of $c$'s in $u$

I'm having difficulties figuring out the rule(s) for the part $l_c(u) = 1$. I need clarification on my interpretation of the given $L$ language and thoughs how to construct the rule(s) for the part $l_c(u) = 1$.
In addition, I'm a bit unconfortable on this field, so there might be other issues too.


marked as duplicate by D.W., David Richerby, Juho, Wandering Logic, vonbrand May 22 '14 at 20:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Your grammar is wrong. Note that your grammar can generate the word $(ab)^3c$ which is clearly not in $L$. (More over, you don't handle the request of the number of $ab$ and $ba$. a word in $L$ must have at least $(ab)^2$ as prefix and $ba$ as suffix (as $n>0$).

Note that we could write $L$ in the following way:

$L=\left \{ (ab)^{n+1} xcy(ba)^n | n>0, x,y \in \left \{ a ,d \right \} ^* \right \} $

A grammar for this language:

$S \rightarrow ababAba$

$A \rightarrow abAba \, | \, B$

$B \rightarrow DCD $

$C \rightarrow c$

$D \rightarrow aD \, |\, dD \, |\, \epsilon$

The grammar works as follow: First, using the first and second rules we generate $(ab)^{n+1}A(ba)^n$ for $n>0$ . Then we replace $A$ with $B$. $C$ is responsible to generate $c$ (and by that promising that $l_c = 1$ , and $D$ generates the language $\left \{ a ,d \right \} ^*$


Not the answer you're looking for? Browse other questions tagged or ask your own question.