Context Sensitive Grammar

1 min read Last updated Sat Jun 27 2026 08:46:52 GMT+0000 (Coordinated Universal Time)

Aka. 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

where:

  • AVA \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

Aka. CSL. Languages generated by a CSG. Accepted by linear-bounded automata.

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

CFLCSL\text{CFL} \subsetneq \text{CSL}

Example

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

Productions:

  • SaSBCaBCS \to aSBC \mid aBC
  • CBBCCB \to BC
  • aBabaB \to ab
  • bBbbbB \to bb
  • bCbcbC \to bc
  • cCcccC \to cc

The key production CBBCCB \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.

Was this helpful?