2

Here is a question that I encountered in one of my exams:

Find one context-free grammar that recognizes the language: $\qquad L = \{a^n(b^mc^m)^pd^n \mid m, n, p \geq 0\} $

Can you find such a grammar? I even doubt its existence, but I have not been able to prove that.

Raphael
  • 73,212
  • 30
  • 182
  • 400
Huynh
  • 21
  • 2

2 Answers2

3

No, this language is not context-free. This is because the language requires to iterate $b^mc^m$ with the same number $m$ each time. Even if we restrict to $n=0$ and $p=2$ then the resulting language $\{b^mc^mb^mc^m \mid m\ge 0\}$ is easily recognized as non-context-free. Formally this restriction is obtained by intersecting with a regular language, a known closure property of context-free languages. To prove a language formally non-context-free one might use the pumping lemma discussed elsewhere in this forum.

Perhaps that is a mistake in its specification. If for each iteration the number $m$ can be different, the resulting language is context-free. That language would be $L_1 = \{ a^n w d^n \mid n\ge 0, w\in \{\; b^mc^m \mid m\ge 0\}^* \;\}$. As an example, $bbbcccbc$ is an element of $L_1$, but not of $L$.

How to find a grammar (for $L_1$)? Observe the nesting structure. Context-free grammars are good in nesting, and iteration. For instance the part $\{\; b^mc^m \mid m\ge 0\}^*$ is generated by $T\to AT, T\to A, A\to bAc, A\to\lambda$, starting with $T$. Now add a matching number of $a$'s and $d$'s on both sides by nesting: $S \to aSd, S\to T$.

Hendrik Jan
  • 31,459
  • 1
  • 54
  • 109
0

As @Hendrik mentioned, if m is not to be remembered ie it can be different than I think following grammar will work .. $$ A \rightarrow a A D | a B D | \lambda $$

$$ B \rightarrow b B c | b B c P|P $$ $$ P\rightarrow bPc|bPcB|\lambda $$ $$ D \rightarrow d $$

Parag
  • 340
  • 1
  • 2
  • 6