Context-Sensitive Grammar (CSG) is a formal grammar where production rules depends on the context of a symbols, a non-terminal can be replaced only when it appears in a specific context.
Production Rules
α X β → α γ β
- X ➜ non-terminal symbol
- α and β ➜ strings of terminals and/or non-terminals
- γ ➜ non-empty string
- X is replaced by γ only in the context of α and β
Context-sensitive grammars are more powerful than context-free grammars because there are some languages that can be described by CSG but not by context-free grammars and CSL are less powerful than Unrestricted grammar. That's why context-sensitive grammars are positioned between context-free and unrestricted grammars in the Chomsky hierarchy.

A Context-Sensitive Grammar is defined as a 4-tuple:
G = (N, Σ, P, S )
- N = Set of non-terminal symbols
- Σ = Set of terminal symbols
- P = Finite set of production rules
- S = Start symbol
All productions in P are of form:
α1 A α2 --> α1 β α2
Context-sensitive Language: The language that can be defined by context-sensitive grammar is called CSL. Properties of CSL are :
- Union, intersection and concatenation of two context-sensitive languages is context-sensitive.
- Complement of a context-sensitive language is context-sensitive.
Example:
Consider the following Context-Sensitive Grammar (CSG):
S → abc | aAbc
Ab → bA
Ac → Bbcc
bB → Bb
aB → aa | aaA
What is the language generated by this grammar?
Solution:
S
⇒ aAbc
⇒ abAc
⇒ abBbcc
⇒ aBbbcc
⇒ aaAbbcc
⇒ aabAbcc
⇒ aabbAcc
⇒ aabbBbccc
⇒ aabBbbccc
⇒ aaBbbbccc
⇒ aaabbbccc
The language generated by this grammar is: L={ anbncn ∣ n≥1 }
L={ anbncn ∣ n≥1 }
- Practicing the following questions will help you test your knowledge. All questions have been asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you practice them.