Context-Free Grammar for Palindromes over {a,b}\{a,b\}

Context-Free Grammar for Palindromes over {a,b}\{a,b\}

Verified Sources
Sep 13, 2026

A palindrome over alphabet {a,b}\{a,b\} is a string ww such that w=wRw = w^R (where wRw^R is the reversal of ww). A standard way to write a context-free grammar (CFG) for palindromes is to build strings from the outside inward: if the palindrome starts and ends with the same symbol, then the middle must also be a palindrome.

In CFG terms, introduce a start symbol SS that generates exactly the language Palindrome Language of palindromes. The grammar uses productions that “wrap” a smaller palindrome with matching terminal symbols:

  • SaSaS \to aSa generates palindromes with outer letters aa,
  • SbSbS \to bSb generates palindromes with outer letters bb,
  • base cases generate the odd/even centers: SaS \to a, SbS \to b, and SϵS \to \epsilon (empty string is a palindrome).

Key learning terms

Context-Free Grammar (CFG)
Start symbol
Derivation
Palindrome

Important: The grammar must be context-free, meaning each rule replaces a single nonterminal by a string of terminals/nonterminals.

CFG for Palindromes (Outside-in construction)

A clean CFG (one nonterminal)

A correct CFG for palindromes over {a,b}\{a,b\} is:

Nonterminals: {S}\{S\}
Terminals: {a,b}\{a,b\}
Productions:

  1. SaSaS \to aSa
  2. SbSbS \to bSb
  3. SaS \to a
  4. SbS \to b
  5. SϵS \to \epsilon

Start symbol: SS

This grammar generates all palindromes because:

  • Any palindrome is either empty, of length 11, or has form a(palindrome)aa(\text{palindrome})a, or b(palindrome)bb(\text{palindrome})b.
  • The productions mirror this structure exactly.

Constructing the palindrome CFG (outside-in method)

  1. 1
    Step 1

    Use SS to generate all palindromes over {a,b}\{a,b\}.

  2. 2
    Step 2

    Add SaSaS \to aSa and SbSbS \to bSb so the grammar enforces the first and last symbols match.

  3. 3
    Step 3

    Add SaS \to a, SbS \to b, and SϵS \to \epsilon to stop recursion at length 11 or 00.

  4. 4
    Step 4

    Even-length palindromes come from repeated wrapping ending in ϵ\epsilon; odd-length palindromes come from wrapping ending in aa or bb.

  5. 5
    Step 5

    Every derivation produces a string whose first and last symbols match at each wrapping, so the final string equals its reverse.

Pro Tip

When building CFGs for palindromes, it’s almost always easiest to enforce equality of the outer symbols using rules like SaSaS\to aSa and SbSbS\to bSb—then handle length 0/10/1 via base cases.

Common mistake

Do not use rules that allow mismatched ends (e.g., SaSbS\to aSb). That would generate strings that are not palindromes.

Example derivations

  1. Even-length palindrome: abbaabba
  • SaSaS \Rightarrow aSa
  • Sa(bSb)aS \Rightarrow a(bSb)a
  • Sa(bϵb)aS \Rightarrow a(b\epsilon b)a
  • Yield: abbaabba
  1. Odd-length palindrome: abaaba
  • SaSaS \Rightarrow aSa
  • Sa(ϵ)aS \Rightarrow a(\epsilon)a is not directly possible with only ϵ\epsilon center between aa’s unless you choose base properly; instead:
  • SaSaS \Rightarrow aSa
  • Sa(bSb)aS \Rightarrow a(bSb)a is too long; use:
  • SaSaS \Rightarrow aSa
  • Sa(ϵ)aS \Rightarrow a(\epsilon)a via SϵS \to \epsilon
  • Yield: aaaa is wrong if you intended abaaba, so instead derive:
  • SbSbS \Rightarrow bSb then wrap:
  • SaSaS \Rightarrow aSa then choose center SbS \Rightarrow b:
  • SaSaS \Rightarrow aSa
  • SabaS \Rightarrow ab a
  • Yield: abaaba

This highlights that base productions (aa, bb, ϵ\epsilon) are what determine the exact center.

How the grammar generates palindromes

$S \Rightarrow \epsilon, a, b$

Base

Centers for even/odd palindromes."

Apply $S \to aSa$ or $S \to bSb$

Grow outward

Each step adds one matching symbol to both ends."

Return to base case

Stop

Final string length and exact center are fixed."

Palindrome length parity vs. center choice

How even vs. odd lengths are produced by the CFG.

FAQ: CFG details

Knowledge Check

Question 1 of 3
Q1Single choice

Which production in the CFG is responsible for palindromes that start and end with 'a'?