Lably
Skip to content

Context-sensitive grammar

Type of formal grammar

  • 7 min read
  • 4 chapters

Related on Lably

Through Context-free grammar and Natural language

A formal language that can be described by a context-sensitive grammar, or, equivalently, by a noncontracting grammar or a linear bounded automaton, is called a context-sensitive language. Some textbooks actually define CSGs as non-contracting, although this is not how Noam Chomsky defined them in 1959. This choice of definition makes no difference in terms of the languages generated (i.e. the two definitions are weakly equivalent), but it does make a difference in terms of what grammars are structurally considered context-sensitive; the latter issue was analyzed by Chomsky in 1963.

Chomsky introduced context-sensitive grammars as a way to describe the syntax of natural language where it is often the case that a word may or may not be appropriate in a certain place depending on the context. Walter Savitch has criticized the terminology "context-sensitive" as misleading and proposed "non-erasing" as better explaining the distinction between a CSG and an unrestricted grammar.

Although it is well known that certain features of languages (e.g. cross-serial dependency) are not context-free, it is an open question how much of CSGs' expressive power is needed to capture the context sensitivity found in natural languages. Subsequent research in this area has focused on the more computationally tractable mildly context-sensitive languages. The syntaxes of some visual programming languages can be described by context-sensitive graph grammars.

Formal definition

Formal grammar

Let us notate a formal grammar as G=(N,Σ,P,S), with N a set of nonterminal symbols, Σ a set of terminal symbols, P a set of production rules, and S∈N the start symbol.

A string u∈(N∪Σ)∗ directly yields, or directly derives to, a string v∈(N∪Σ)∗, denoted as u⇒v, if v can be obtained from u by an application of some production rule in P, that is, if u=γLδ and v=γRδ, where (L→R)∈P is a production rule, and γ,δ∈(N∪Σ)∗ is the unaffected left and right part of the string, respectively. More generally, u is said to yield, or derive to, v, denoted as u⇒∗v, if v can be obtained from u by repeated application of production rules, that is, if u=u0⇒...⇒un=v for some n ≥ 0 and some strings u1,...,un−1∈(N∪Σ)∗. 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: L(G)={w∈Σ∗∣S⇒∗w}. 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 of either of the forms

αAβ → αγβ
A → ε

with ε being the empty string, A∈N, α,β,γ∈(N∪Σ)∗,γ≠ε, i.e., α, β and γ are strings of nonterminals and terminals, but γ is not allowed to be empty. Without this restriction, the resulting grammars become equal in power to unrestricted grammars.

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.

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

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. The equivalence was established by Penttonen normal form.

Sign in to keep reading

3 more chapters are free with a Lably account.

More in Language and linguistics