# Context-sensitive grammar

A context-sensitive grammar (CSG) is a formal grammar in which the left-hand sides and right-hand sides of any production rules may be surrounded by a context of terminal and nonterminal symbols. Context-sensitive grammars are more general than context-free grammars, in the sense that there are languages that can be described by CSG but not by context-free grammars. Context-sensitive grammars are less general (in the same sense) than unrestricted grammars. Thus, CSG are positioned between context-free and unrestricted grammars in the Chomsky hierarchy.[1]

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,[2][3][4][5] although this is not how Noam Chomsky defined them in 1959.[6][7] 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.[8][9]

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.[10]

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 CSG's 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.[citation needed] The syntaxes of some visual programming languages can be described by context-sensitive graph grammars.[11]

## Formal definition

A formal grammar G = (N, Σ, P, S), with N a set of nonterminal symbols, Σ a set of terminal symbols, P a set of production rules, and S the start symbol, is context-sensitive if all rules in P are of the form

αAβ → αγβ

with AN,[note 1] α,β ∈ (N∪Σ)* [note 2] and γ ∈ (N∪Σ)+.[note 3]

A string u ∈ (N∪Σ)* directly yields, or directly derives to, a string v ∈ (N∪Σ)*, denoted as uv, if u can be written as lαAβr, and v can be written as lαγβr, for some production rule (αAβ→αγβ) ∈ P, and some context strings l, r ∈ (N∪Σ)*. More generally, u is said to yield, or derive to, v, denoted as u* v, if u = u1 ⇒ ... ⇒ un = v for some n≥0 and some strings u2, ..., un-1 (N∪Σ)*. That is, 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 don't contribute to L(G).

The only difference between this definition of Chomsky and that of unrestricted grammars is that γ can be empty in the unrestricted case.[10]

Some definitions of a context-sensitive grammar only require that for any production rule of the form u → v, the length of u shall be less than or equal to the length of v. This seemingly weaker requirement is in fact weakly equivalent,[12] see Noncontracting grammar#Transforming into context-sensitive grammar.

In addition, a rule of the form

S → λ

where λ represents the empty string and S does not appear on the right-hand side of any rule 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 context-sensitive 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 context-free grammar where the context of a nonterminal is not taken into consideration. Indeed, every production of a context-free grammar is of the form Vw 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.

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]

## Examples

### anbncn

The following context-sensitive grammar, with start symbol S, generates the canonical non-context-free language { anbncn : n ≥ 1 } :[citation needed]

 1 S → a B C 2 S → a S B C 3 C B → C Z 4 C Z → W Z 5 W Z → W C 6 W C → B C 7 a B → a b 8 b B → b b 9 b C → b c 10 c C → c c

Rules 1 and 2 allow for blowing-up S to anBC(BC)n-1; rules 3 to 6 allow for successively exchanging each CB to BC (four rules are needed for that since a rule CBBC wouldn't fit into the scheme αAβ → αγβ); rules 7–10 allow replacing a non-terminals B and C with its corresponding terminals b and c respectively, provided it is in the right place. A generation chain for aaabbbccc is:

S
2 aSBC
2 aaSBCBC
1 aaaBCBCBC
3 aaaBCZCBC
4 aaaBWZCBC
5 aaaBWCCBC
6 aaaBBCCBC
3 aaaBBCCZC
4 aaaBBCWZC
5 aaaBBCWCC
6 aaaBBCBCC
3 aaaBBCZCC
4 aaaBBWZCC
5 aaaBBWCCC
6 aaaBBBCCC
7 aaabBBCCC
8 aaabbBCCC
8 aaabbbCCC
9 aaabbbcCC
10 aaabbbccC
10 aaabbbccc

### anbncndn, etc.

More complicated grammars ${\displaystyle CSG}$can be used to parse { anbncndn: n ≥ 1 }, and other languages with even more letters. Here we show a simpler approach using non-contracting grammars:[citation needed] Start with a kernel of regular productions generating the sentential forms ${\displaystyle (ABCD)^{n}abcd}$ and then include the non contracting productions ${\displaystyle p_{Da}:Da\rightarrow aD}$, ${\displaystyle p_{Db}:Db\rightarrow bD}$, ${\displaystyle p_{Dc}:Dc\rightarrow cD}$, ${\displaystyle p_{Dd}:Dd\rightarrow dd}$, ${\displaystyle p_{Ca}:Ca\rightarrow aC}$, ${\displaystyle p_{Cb}:Cb\rightarrow bC}$, ${\displaystyle p_{Cc}:Cc\rightarrow cc}$, ${\displaystyle p_{Ba}:Ba\rightarrow aB}$, ${\displaystyle p_{Bb}:Bb\rightarrow bb}$, ${\displaystyle p_{Aa}:Aa\rightarrow aa}$.

### ambncmdn

A non contracting grammar (for which there is an equivalent ${\displaystyle CSG}$) for the language ${\displaystyle L_{Cross}=\{a^{m}b^{n}c^{m}d^{n}:m\geq 1,n\geq 1\}}$ is defined by ${\displaystyle p_{1}:R\rightarrow aRC|aC}$ and ${\displaystyle p_{3}:T\rightarrow BTd|Bd}$, ${\displaystyle p_{5}:CB\rightarrow BC}$, ${\displaystyle p_{0}:S\rightarrow RT}$, ${\displaystyle p_{6}:aB\rightarrow ab}$, ${\displaystyle p_{7}:bB\rightarrow bb}$, ${\displaystyle p_{8}:Cd\rightarrow cd}$, ${\displaystyle p_{9}:Cc\rightarrow cc}$.

With these definitions, a derivation for ${\displaystyle a^{3}b^{2}c^{3}d^{2}}$ is: ${\displaystyle S\Rightarrow _{p_{0}}RT\Rightarrow _{p_{1}^{2}p_{2}}a^{3}C^{3}T\Rightarrow _{p_{3}p_{4}}a^{3}C^{3}B^{2}d^{2}\Rightarrow _{p_{5}^{6}}a^{3}B^{2}C^{3}d^{2}\Rightarrow _{p_{6}p_{7}}a^{3}b^{2}C^{3}d^{2}\Rightarrow _{p_{8}p_{9}^{2}}a^{3}b^{2}c^{3}d^{2}}$.[citation needed]

### a2i

A noncontracting grammar for the language { a2i : i ≥ 1 } is constructed in Example 9.5 (p. 224) of (Hopcroft, Ullman, 1979):[15]

1. ${\displaystyle S\rightarrow [ACaB]}$
2. ${\displaystyle {\begin{cases}\ [Ca]a\rightarrow aa[Ca]\\\ [Ca][aB]\rightarrow aa[CaB]\\\ [ACa]a\rightarrow [Aa]a[Ca]\\\ [ACa][aB]\rightarrow [Aa]a[CaB]\\\ [ACaB]\rightarrow [Aa][aCB]\\\ [CaB]\rightarrow a[aCB]\end{cases}}}$
3. ${\displaystyle [aCB]\rightarrow [aDB]}$
4. ${\displaystyle [aCB]\rightarrow [aE]}$
5. ${\displaystyle {\begin{cases}\ a[Da]\rightarrow [Da]a\\\ [aDB]\rightarrow [DaB]\\\ [Aa][Da]\rightarrow [ADa]a\\\ a[DaB]\rightarrow [Da][aB]\\\ [Aa][DaB]\rightarrow [ADa][aB]\end{cases}}}$
6. ${\displaystyle [ADa]\rightarrow [ACa]}$
7. ${\displaystyle {\begin{cases}\ a[Ea]\rightarrow [Ea]a\\\ [aE]\rightarrow [Ea]\\\ [Aa][Ea]\rightarrow [AEa]a\end{cases}}}$
8. ${\displaystyle [AEa]\rightarrow a}$

## Kuroda normal form

Every context-sensitive grammar which does not generate the empty string can be transformed into a weakly equivalent one in Kuroda normal form. "Weakly equivalent" here means that the two grammars generate the same language. The normal form will not in general be context-sensitive, but will be a noncontracting grammar.[16][17]

The Kuroda normal form is an actual normal form for non-contracting grammars.

## Properties and uses

### Equivalence to linear bounded automaton

A formal language can be described by a context-sensitive grammar if and only if it is accepted by some linear bounded automaton (LBA).[18] In some textbooks this result is attributed solely to Landweber and Kuroda.[7] Others call it the Myhill–Landweber–Kuroda theorem.[19] (Myhill introduced the concept of deterministic LBA in 1960. Peter S. Landweber published in 1963 that the language accepted by a deterministic LBA is context sensitive. Kuroda introduced the notion of non-deterministic LBA and the equivalence between LBA and CSGs in 1964.[20][21])

As of 2010 it is still an open question whether every context-sensitive language can be accepted by a deterministic LBA.[22]

### Closure properties

Context-sensitive languages are closed under complement. This 1988 result is known as the Immerman–Szelepcsényi theorem.[19] Moreover, they are closed under union, intersection, concatenation, substitution,[note 4] inverse homomorphism, and Kleene plus.[23]

Every recursively enumerable language L can be written as h(L) for some context-sensitive language L and some string homomorphism h.[24]

### Computational problems

The decision problem that asks whether a certain string s belongs to the language of a given context-sensitive grammar G, is PSPACE-complete. Moreover, there are context-sensitive grammars whose languages are PSPACE-complete. In other words, there is a context-sensitive grammar G such that deciding whether a certain string s belongs to the language of G is PSPACE-complete (so G is fixed and only s is part of the input of the problem).[25]

The emptiness problem for context-sensitive grammars (given a context-sensitive grammar G, is L(G)=∅ ?) is undecidable.[26][note 5]

### As model of natural languages

Savitch has proven the following theoretical result, on which he bases his criticism of CSGs as basis for natural language: for any recursively enumerable set R, there exists a context-sensitive language/grammar G which can be used as a sort of proxy to test membership in R in the following way: given a string s, s is in R if and only if there exists a positive integer n for which scn is in G, where c is an arbitrary symbol not part of R.[10]

It has been shown that nearly all natural languages may in general be characterized by context-sensitive grammars, but the whole class of CSG's seems to be much bigger than natural languages.[citation needed] Worse yet, since the aforementioned decision problem for CSG's is PSPACE-complete, that makes them totally unworkable for practical use, as a polynomial-time algorithm for a PSPACE-complete problem would imply P=NP.

It was proven that some natural languages are not context-free, based on identifying so-called cross-serial dependencies and unbounded scrambling phenomena. However this does not necessarily imply that all the class CSG is necessary to capture "context sensitivity" in the colloquial sense of these terms in natural languages. For example, the strictly weaker (than CSG) linear context-free rewriting systems (LCFRS) can account for the phenomenon of cross-serial dependencies; one can write a LCFRS grammar for {anbncndn | n ≥ 1} for example.[27][28][29]

Ongoing research on computational linguistics has focused on formulating other classes of languages that are "mildly context-sensitive" whose decision problems are feasible, such as tree-adjoining grammars, combinatory categorial grammars, coupled context-free languages, and linear context-free rewriting systems. The languages generated by these formalisms properly lie between the context-free and context-sensitive languages.

More recently, the class PTIME has been identified with range concatenation grammars, which are now considered to be the most expressive of the mild-context sensitive language classes.[29]

## Notes

1. ^ i.e., A a single nonterminal
2. ^ i.e., α and β strings of nonterminals and terminals
3. ^ i.e., γ is a nonempty string of nonterminals and terminals
4. ^ more formally: if L ⊆ Σ* is a context-sensitive language and f maps each a∈Σ to a context-sensitive language f(a), the f(L) is again a context-sensitive language
5. ^

## References

1. ^ (Hopcroft, Ullman, 1979); Sect.9.4, p.227
2. ^ Linz, Peter (2011). An Introduction to Formal Languages and Automata. Jones & Bartlett Publishers. p. 291. ISBN 978-1-4496-1552-9.
3. ^ Meduna, Alexander (2000). Automata and Languages: Theory and Applications. Springer Science & Business Media. p. 730. ISBN 978-1-85233-074-3.
4. ^ Davis, Martin; Sigal, Ron; Weyuker, Elaine J. (1994). Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science (2nd ed.). Morgan Kaufmann. p. 189. ISBN 978-0-08-050246-5.
5. ^ Martin, John C. (2010). Introduction to Languages and the Theory of Computation (4th ed.). New York, NY: McGraw-Hill. p. 277. ISBN 9780073191461.
6. ^ Levelt, Willem J. M. (2008). An Introduction to the Theory of Formal Languages and Automata. John Benjamins Publishing. p. 26. ISBN 978-90-272-3250-2.
7. ^ a b Davis, Martin; Sigal, Ron; Weyuker, Elaine J. (1994). Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science (2nd ed.). Morgan Kaufmann. pp. 330–331. ISBN 978-0-08-050246-5.
8. ^ Chomsky, N. (1963). "Formal properties of grammar". In Luce, R. D.; Bush, R. R.; Galanter, E. (eds.). Handbook of Mathematical Psychology. New York: Wiley. pp. 360–363.
9. ^ Levelt, Willem J. M. (2008). An Introduction to the Theory of Formal Languages and Automata. John Benjamins Publishing. pp. 125–126. ISBN 978-90-272-3250-2.
10. ^ a b c Carlos Martín Vide, ed. (1999). Issues in Mathematical Linguistics: Workshop on Mathematical Linguistics, State College, Pa., April 1998. John Benjamins Publishing. pp. 186–187. ISBN 90-272-1556-1.
11. ^ Zhang, Da-Qian, Kang Zhang, and Jiannong Cao. "A context-sensitive graph grammar formalism for the specification of visual languages." The Computer Journal 44.3 (2001): 186–200.
12. ^ Hopcroft, John E.; Ullman, Jeffrey D. (1979). Introduction to Automata Theory, Languages, and Computation. Addison-Wesley. ISBN 9780201029888.; p. 223–224; Exercise 9, p. 230. In the 2003 edition, the chapter on CSG has been omitted.
13. ^ Hazewinkel, Michiel (1989). Encyclopaedia of Mathematics. Vol. 4. Springer Science & Business Media. p. 297. ISBN 978-1-55608-003-6. also at https://www.encyclopediaofmath.org/index.php/Grammar,_context-sensitive
14. ^ Ito, Masami; Kobayashi, Yūji; Shoji, Kunitaka (2010). Automata, Formal Languages and Algebraic Systems: Proceedings of AFLAS 2008, Kyoto, Japan, 20–22 September 2008. World Scientific. p. 183. ISBN 978-981-4317-60-3. citing Penttonen, Martti (Aug 1974). "One-sided and two-sided context in formal grammars". Information and Control. 25 (4): 371–392. doi:10.1016/S0019-9958(74)91049-3.
15. ^ They obtained the grammar by systematic transformation of an unrestricted grammar, given in Exm. 9.4, viz.:
1. ${\displaystyle S\rightarrow ACaB}$,
2. ${\displaystyle Ca\rightarrow aaC}$,
3. ${\displaystyle CB\rightarrow DB}$,
4. ${\displaystyle CB\rightarrow E}$,
5. ${\displaystyle aD\rightarrow Da}$,
6. ${\displaystyle AD\rightarrow AC}$,
7. ${\displaystyle aE\rightarrow Ea}$,
8. ${\displaystyle AE\rightarrow \varepsilon }$.
In the context-sensitive grammar, a string enclosed in square brackets, like ${\displaystyle [ACaB]}$, is considered a single symbol (similar to e.g. <name-part> in Backus–Naur form). The symbol names are chosen to resemble the unrestricted grammar. Likewise, rule groups in the context-sensitive grammar are numbered by the unrestricted-grammar rule they originated from.
16. ^ Kuroda, Sige-Yuki (June 1964). "Classes of languages and linear-bounded automata". Information and Control. 7 (2): 207–223. doi:10.1016/s0019-9958(64)90120-2.
17. ^ Mateescu, Alexandru; Salomaa, Arto (1997). "Chapter 4: Aspects of Classical Language Theory". In Rozenberg, Grzegorz; Salomaa, Arto (eds.). Handbook of Formal Languages. Volume I: Word, language, grammar. Springer-Verlag. pp. 175–252. ISBN 3-540-61486-9., Here: Theorem 2.2, p. 190
18. ^ (Hopcroft, Ullman, 1979); Theorem 9.5, 9.6, p. 225–226
19. ^ a b Sutner, Klaus (Spring 2016). "Context Sensitive Grammars" (PDF). Carnegie Mellon University. Archived from the original (PDF) on 2017-02-03. Retrieved 2019-08-29.
20. ^ Meduna, Alexander (2000). Automata and Languages: Theory and Applications. Springer Science & Business Media. p. 755. ISBN 978-1-85233-074-3.
21. ^ Levelt, Willem J. M. (2008). An Introduction to the Theory of Formal Languages and Automata. John Benjamins Publishing. pp. 126–127. ISBN 978-90-272-3250-2.
22. ^ Martin, John C. (2010). Introduction to Languages and the Theory of Computation (4th ed.). New York, NY: McGraw-Hill. p. 283. ISBN 9780073191461.
23. ^ (Hopcroft, Ullman, 1979); Exercise S9.10, p. 230–231
24. ^ (Hopcroft, Ullman, 1979); Exercise S9.14, p. 230–232. h maps each symbol to itself or to the empty string.
25. ^ An example of such a grammar, designed to solve the QSAT problem, is given in Lita, C. V. (2016-09-01). "On Complexity of the Detection Problem for Bounded Length Polymorphic Viruses". 2016 18th International Symposium on Symbolic and Numeric Algorithms for Scientific Computing (SYNASC): 371–378. doi:10.1109/SYNASC.2016.064. ISBN 978-1-5090-5707-8. S2CID 18067130.
26. ^ (Hopcroft, Ullman, 1979); Exercise S9.13, p. 230–231
27. ^ Kallmeyer, Laura (2011). "Mildly Context-Sensitive Grammar Formalisms: Natural Languages are not Context-Free" (PDF).
28. ^ Kallmeyer, Laura (2011). "Mildly Context-Sensitive Grammar Formalisms: Linear Context-Free Rewriting Systems" (PDF).
29. ^ a b Kallmeyer, Laura (2010). Parsing Beyond Context-Free Grammars. Springer Science & Business Media. pp. 1–5. ISBN 978-3-642-14846-0.