# Designing CFG that accepts $a^n b^m c^p$ where $n=m+p+2$

I have generated the CFG of $$a^n b^m c^p$$ where $$m = n+p+2$$:

$$S \rightarrow ASC \mid \varepsilon$$

$$A \rightarrow aAb \mid \varepsilon$$

$$C \rightarrow bCc \mid \varepsilon$$

I have been trying $$a^n b^m c^p$$ where $$n=m+p+2$$ but cannot figure out how to represent $$n=m+p+2$$. Any hint would really be appreciated.

• Does this answer your question? How to prove that a language is context-free? You should look at examples. – Nathaniel May 22 at 23:27
• I tried it myself and this was my solution Is this correct? S->abSA | a , C -> Ac | NULL – M. Hasnat Raza May 22 at 23:39
• No, because 1) $A$ is not rewritten 2) it can create $abaA$. – Nathaniel May 22 at 23:51
• Thanks a lot. I think I found the answer. Here it is: $S->$C$S | aa ,$C->$A$C | NULL , $A->$B$A | a , B->a B b | NULL. Please, do tell me if its correct. Thanks a lot – M. Hasnat Raza May 23 at 0:16 • The grammar in your question recognises$a^nb^mc^p$where$m = n + p$, not$m = n + p + 2\$. Although it's easy to fix. – rici May 23 at 4:43