Context Sensitive Grammar

Also known as CSG. A grammar G=(V,Σ,S,P)G = (V, \Sigma, S, P) where every production α→β\alpha \to \beta satisfies ∣β∣≥∣α∣|\beta| \geq |\alpha|.

Equivalent form: every production has the form

αAβ→αXβ\alpha A \beta \to \alpha X \beta
Here:
A∈VA \in V
single non-terminal on LHS surrounded by context
X∈(V∪Σ)+X \in (V \cup \Sigma)^+
non-empty replacement
α,β∈(V∪Σ)∗\alpha, \beta \in (V \cup \Sigma)^*
arbitrary context strings

The context α\alpha and β\beta must be identical on both sides. The non-terminal AA is replaced only in that specific context.

Context-Sensitive Language

Also known as CSL. Languages generated by a CSG. Accepted by linear-bounded automata.

Every context-free language is context-sensitive. Not every CSL is context-free.

CFL⊊CSL\text{CFL} \subsetneq \text{CSL}

Example

The language L={anbncn∣n≥1}L = \{a^n b^n c^n \mid n \ge 1\} is CSL but not CFL. A CSG generating LL:

Productions:

  • S→aSBC∣aBCS \to aSBC \mid aBC
  • CB→BCCB \to BC
  • aB→abaB \to ab
  • bB→bbbB \to bb
  • bC→bcbC \to bc
  • cC→cccC \to cc

The key production CB→BCCB \to BC allows BBs and CCs to swap positions, a context-sensitive rewrite that no CFG production A→αA \to \alpha can express, because the LHS has 2 non-terminals.

Written by November 3, 2026 1 min read
Was this helpful?