# CFG for palindromes containing atleast 3 c's

Input alphabet = {a,b,c}

I tried with the following grammar :-

S -> aSa / bSb / T
T -> cTc / cSc / cBc
B -> c / cXc
X -> a / b / aXa / bXb / epsilon

This grammar is not able to generate 'cacac' .Is there any way to write this grammar with less number of productions or are any productions redundant here ?

Thanks.

## 1 Answer

You have a symbol S that needs to generate a palindrome with at least 3 c's. Create a symbol X that needs to generate a palindrome with at least 1 c, and a symbol Y that needs to generate a palindrome with no requirement for c's.

S -> aSa | bSb | cXc
X -> aXa | bXb | cYc | c
Y -> aYa | bYb | cYc | a | b | c | eps


I think the essential step was not to start writing down productions, but to write down which different symbols you need to produce which kinds of output. The productions are then trivial.