A contextsensitive grammar (CSG) is a formal grammar in which the lefthand sides and righthand sides of any production rules may be surrounded by a context of terminal and nonterminal symbols. Contextsensitive grammars are more general than contextfree grammars but still orderly enough to be parsed by a linear bounded automaton.
The concept of contextsensitive grammar was introduced by Noam Chomsky in the 1950s as a way to describe the syntax of natural language where it is indeed often the case that a word may or may not be appropriate in a certain place depending upon the context. A formal language that can be described by a contextsensitive grammar is called a contextsensitive language.
Contents
Formal definition
A formal grammar G = (N, Σ, P, S) ( this is the same as G = (V, T, P, S) , just that the Non  Terminal V(ariable) is replaced by N and T(erminal) is replaced by Σ ) is contextsensitive if all rules in P are of the form
where A ∈ N (i.e., A is a single nonterminal), α,β ∈ (N U Σ)* (i.e., α and β are strings of nonterminals and terminals) and γ ∈ (N U Σ)^{+} (i.e., γ is a nonempty string of nonterminals and terminals).
Some definitions also add that for any production rule of the form u → v of a contextsensitive grammar, it shall be true that u≤v. Here u and v denote the length of the strings respectively.
In addition, a rule of the form
where λ represents the empty string is permitted. The addition of the empty string allows the statement that the context sensitive languages are a proper superset of the context free languages, rather than having to make the weaker statement that all context free grammars with no →λ productions are also context sensitive grammars.
The name contextsensitive is explained by the α and β that form the context of A and determine whether A can be replaced with γ or not. This is different from a contextfree grammar where the context of a nonterminal is not taken into consideration. (Indeed, every production of a context free grammar is of the form V → w where V is a single nonterminal symbol, and w is a string of terminals and/or nonterminals (w can be empty)).
If the possibility of adding the empty string to a language is added to the strings recognized by the noncontracting grammars (which can never include the empty string) then the languages in these two definitions are identical.
Examples
This grammar generates the canonical noncontextfree language :
Full article ▸
