Context free grammar for palindrome over ALL terminals?

$\begingroup$

I've found various examples of context-free grammars for palindromes but they all seem to hardcode the rules to be of the form "terminal Statement (same) terminal"

For example, if $T \rightarrow a | b | c$ and we wanted to generate any-length palindromes, the grammar rules would be $S \to aSa \mid bSb \mid cSc \mid T \mid \epsilon$

Is there way to make a generalized rule rather than individually listing $aSa$, $bSb$, ... for every terminal character?

$\endgroup$ 7

1 Answer

$\begingroup$

As stated by @saulspatz:

"If you say, "One can substitute a for the second X provided one has substituted a for the first X", then the permitted substitutions depend on the context, don't they?"

By nature of being context free, a general rule for such a thing is not possible. It must be explicitly specified by doing something like "aSa"

$\endgroup$

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like