Let us notate a formal grammar as
, with
a set of nonterminal symbols,
a set of terminal symbols,
a set of production rules, and
the start symbol.
A string
directly yields, or directly derives to, a string
, denoted as
, if v can be obtained from u by an application of some production rule in P, that is, if
and
, where
is a production rule, and
is the unaffected left and right part of the string, respectively.
More generally, u is said to yield, or derive to, v, denoted as
, if v can be obtained from u by repeated application of production rules, that is, if
for some n ≥ 0 and some strings
. In other words, the relation
is the reflexive transitive closure of the relation
.
The language of the grammar G is the set of all terminal-symbol strings derivable from its start symbol, formally:
.
Derivations that do not end in a string composed of terminal symbols only are possible, but do not contribute to L(G).
Context-sensitive grammar
A formal grammar is context-sensitive if each rule in P is either of the form
where
is the empty string, or of the form
- αAβ → αγβ
with A ∈ N,[note 1]
,[note 2] and
.[note 3]
The name context-sensitive is explained by the α and β that form the context of A and determine whether A can be replaced with γ or not.
By contrast, in a context-free grammar, no context is present: the left hand side of every production rule is just a nonterminal.
The string γ is not allowed to be empty. Without this restriction, the resulting grammars become equal in power to unrestricted grammars.[10]
(Weakly) equivalent definitions
A noncontracting grammar is a grammar in which for any production rule, of the form u → v, the length of u is less than or equal to the length of v.
Every context-sensitive grammar is noncontracting, while every noncontracting grammar can be converted into an equivalent context-sensitive grammar; the two classes are weakly equivalent.[12]
Some authors use the term context-sensitive grammar to refer to noncontracting grammars in general.
The left-context- and right-context-sensitive grammars are defined by restricting the rules to just the form αA → αγ and to just Aβ → γβ, respectively. The languages generated by these grammars are also the full class of context-sensitive languages.[13] The equivalence was established by Penttonen normal form.[14]