Context-sensitive grammar explained

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 a CSG but not by a context-free grammar. Context-sensitive grammars are less general (in the same sense) than unrestricted grammars. Thus, CSGs 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 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.[11]

Formal definition

Formal grammar

Let us notate a formal grammar as

G=(N,\Sigma,P,S)

, with

N

a set of nonterminal symbols,

\Sigma

a set of terminal symbols,

P

a set of production rules, and

S\inN

the start symbol.

A string

u\in(N\cup\Sigma)*

directly yields, or directly derives to, a string

v\in(N\cup\Sigma)*

, denoted as

uv

, if v can be obtained from u by an application of some production rule in P, that is, if

u=\gammaL\delta

and

v=\gammaR\delta

, where

(L\toR)\inP

is a production rule, and

\gamma,\delta\in(N\cup\Sigma)*

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\in(N\cup\Sigma)*

. 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\in\Sigma*\midS*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 either of the form

S\to\varepsilon

where

\varepsilon

is the empty string, or of the form

αAβ → αγβwith AN,[12]

\alpha,\beta\in(N\cup\Sigma\setminus\{S\})*

,[13] and

\gamma\in(N\cup\Sigma\setminus\{S\})+

.[14]

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.

(Weakly) equivalent definitions

A noncontracting grammar is a grammar in which for any production rule, of the form uv, 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.[15]

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

Examples

anbncn

The following context-sensitive grammar, with start symbol S, generates the canonical non-context-free language :

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-terminal B or C with its corresponding terminal b or c, respectively, provided it is in the right place.A generation chain for is:

S

2

2

1

3

4

5

6

3

4

5

6

3

4

5

6

7

8

8

9

10

10

anbncndn, etc.

More complicated grammars can be used to parse, and other languages with even more letters. Here we show a simpler approach using non-contracting grammars:Start with a kernel of regular productions generating the sentential forms

(ABCD)nabcd

and then include the non contracting productions

pDa:DaaD

,

pDb:DbbD

,

pDc:DccD

,

pDd:Dddd

,

pCa:CaaC

,

pCb:CbbC

,

pCc:Cccc

,

pBa:BaaB

,

pBb:Bbbb

,

pAa:Aaaa

.

ambncmdn

A non contracting grammar (for which there is an equivalent CSG) for the language

LCross=\{ambncmdn\midm\ge1,n\ge1\}

is defined by

p1:RaRC|aC

,

p3:TBTd|Bd

,

p5:CBBC

,

p0:SRT

,

p6:aBab

,

p7:bBbb

,

p8:Cdcd

, and

p9:Cccc

.

With these definitions, a derivation for

a3b2c3d2

is:
S
p0
RT
2
pp2
1

a3C

3T
p3p4

a3C3B2d

2
6
p
5

a3B2C3d

2
p6p7

a3b2C3d

2
p
2
p
9
8

a3b2c3d2

.

a2i

A noncontracting grammar for the language is constructed in Example 9.5 (p. 224) of (Hopcroft, Ullman, 1979):[18]

S[ACaB]

\begin{cases} [Ca]aaa[Ca]\ [Ca][aB]aa[CaB]\ [ACa]a[Aa]a[Ca]\ [ACa][aB][Aa]a[CaB]\ [ACaB][Aa][aCB]\ [CaB]a[aCB] \end{cases}

[aCB][aDB]

[aCB][aE]

\begin{cases} a[Da][Da]a\\ [aDB][DaB]\ [Aa][Da][ADa]a\ a[DaB][Da][aB]\\ [Aa][DaB][ADa][aB] \end{cases}

[ADa][ACa]

\begin{cases} a[Ea][Ea]a\\ [aE][Ea]\\ [Aa][Ea][AEa]a \end{cases}

[AEa]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.[19] [20]

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

Properties and uses

See also: context-sensitive language.

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).[21] In some textbooks this result is attributed solely to Landweber and Kuroda.[7] Others call it the Myhill–Landweber–Kuroda theorem.[22] (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.[23] Kuroda introduced the notion of non-deterministic LBA and the equivalence between LBA and CSGs in 1964.[24] [25])

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

Closure properties

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

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

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).[30]

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

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 CSGs seems to be much bigger than natural languages. Worse yet, since the aforementioned decision problem for CSGs 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 the class of CSGs is necessary to capture "context sensitivity" in the colloquial sense of these terms in natural languages. For example, linear context-free rewriting systems (LCFRSs) are strictly weaker than CSGs but can account for the phenomenon of cross-serial dependencies; one can write a LCFRS grammar for for example.[33] [34] [35]

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

See also

Further reading

External links

Notes and References

  1. (Hopcroft, Ullman, 1979); Sect.9.4, p.227
  2. Book: Linz, Peter. An Introduction to Formal Languages and Automata. 2011. Jones & Bartlett Publishers. 978-1-4496-1552-9. 291.
  3. Book: Meduna, Alexander. Alexander Meduna. Automata and Languages: Theory and Applications. 2000. Springer Science & Business Media. 978-1-85233-074-3. 730.
  4. Book: Martin. Davis. Martin Davis (mathematician). Ron. Sigal. Elaine J.. Weyuker. Elaine Weyuker. Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science. 1994. Morgan Kaufmann. 978-0-08-050246-5. 189. 2nd.
  5. Book: Martin, John C.. Introduction to Languages and the Theory of Computation. 2010. McGraw-Hill. New York, NY. 9780073191461. 4th. 277.
  6. Book: Levelt, Willem J. M.. Willem Levelt. An Introduction to the Theory of Formal Languages and Automata. 2008. John Benjamins Publishing. 978-90-272-3250-2. 26.
  7. Book: Martin. Davis. Martin Davis (mathematician). Ron . Sigal. Elaine J.. Weyuker. Elaine Weyuker. Computability, Complexity, and Languages: Fundamentals of Theoretical Computer Science. 1994. Morgan Kaufmann. 978-0-08-050246-5. 330–331. 2nd.
  8. Book: Chomsky, N. . Noam Chomsky. 1963. Formal properties of grammar. Handbook of Mathematical Psychology. R. D.. Luce. R. R.. Bush. E.. Galanter. New York. Wiley. 360–363. https://archive.org/details/handbookofmathem017893mbp.
  9. Book: Levelt, Willem J. M.. Willem Levelt. An Introduction to the Theory of Formal Languages and Automata. 2008. John Benjamins Publishing. 978-90-272-3250-2. 125–126.
  10. Book: Carlos Martín Vide. Issues in Mathematical Linguistics: Workshop on Mathematical Linguistics, State College, Pa., April 1998. 1999. John Benjamins Publishing. 90-272-1556-1. 186–187.
  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. i.e., A a single nonterminal
  13. i.e., α and β strings of nonterminals (except for the start symbol) and terminals
  14. i.e., γ is a nonempty string of nonterminals (except for the start symbol) and terminals
  15. Book: Hopcroft . John E. . Introduction to Automata Theory, Languages, and Computation . Ullman . Jeffrey D. . Addison-Wesley . 1979 . 9780201029888 . John Hopcroft . Jeffrey Ullman . registration.
    p. 223–224; Exercise 9, p. 230. In the 2003 edition, the chapter on CSGs has been omitted.
  16. Book: Hazewinkel, Michiel . Encyclopaedia of Mathematics . Springer Science & Business Media . 1989 . 978-1-55608-003-6 . 4 . 297 . Michiel Hazewinkel. also at https://www.encyclopediaofmath.org/index.php/Grammar,_context-sensitive
  17. Book: Ito . Masami . Automata, Formal Languages and Algebraic Systems: Proceedings of AFLAS 2008, Kyoto, Japan, 20–22 September 2008 . Kobayashi . Yūji . Shoji . Kunitaka . World Scientific . 2010 . 978-981-4317-60-3 . 183. citing Penttonen . Martti . Aug 1974 . One-sided and two-sided context in formal grammars . . 25 . 4 . 371–392 . 10.1016/S0019-9958(74)91049-3 . free.
  18. They obtained the grammar by systematic transformation of an unrestricted grammar, given in Exm. 9.4, viz.:

    SACaB

    ,

    CaaaC

    ,

    CBDB

    ,

    CBE

    ,

    aDDa

    ,

    ADAC

    ,

    aEEa

    ,

    AE\varepsilon

    .In the context-sensitive grammar, a string enclosed in square brackets, like

    [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.
  19. Sige-Yuki . Kuroda . S.-Y. Kuroda . Classes of languages and linear-bounded automata . Information and Control . 7 . 2 . 207–223 . June 1964 . 10.1016/s0019-9958(64)90120-2. free .
  20. Book: Mateescu . Alexandru . Salomaa. Arto . Arto Salomaa. Grzegorz. Rozenberg. Grzegorz Rozenberg . Arto. Salomaa . Arto Salomaa. Handbook of Formal Languages. Volume I: Word, language, grammar . Springer-Verlag . 1997 . 175–252 . Chapter 4: Aspects of Classical Language Theory . 3-540-61486-9., Here: Theorem 2.2, p. 190
  21. (Hopcroft, Ullman, 1979); Theorem 9.5, 9.6, p. 225–226
  22. Web site: Klaus . Sutner . Context Sensitive Grammars . . Spring 2016 . 2019-08-29 . 2017-02-03 . https://web.archive.org/web/20170203081505/http://www.cs.cmu.edu/~flac/pdf/ContSens.pdf . dead .
  23. P.S. Landweber . Three Theorems on Phrase Structure Grammars of Type 1 . . 6 . 2 . 131 - 136 . 1963 . 10.1016/s0019-9958(63)90169-4 . free .
  24. Book: Meduna, Alexander. Alexander Meduna. Automata and Languages: Theory and Applications. 2000. Springer Science & Business Media. 978-1-85233-074-3. 755.
  25. Book: Levelt, Willem J. M.. Willem Levelt. An Introduction to the Theory of Formal Languages and Automata. 2008. John Benjamins Publishing. 978-90-272-3250-2. 126–127.
  26. Book: Martin, John C.. Introduction to Languages and the Theory of Computation. 2010. McGraw-Hill. New York, NY. 9780073191461. 4th. 283.
  27. 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
  28. (Hopcroft, Ullman, 1979); Exercise S9.10, p. 230–231
  29. (Hopcroft, Ullman, 1979); Exercise S9.14, p. 230–232. h maps each symbol to itself or to the empty string.
  30. An example of such a grammar, designed to solve the QSAT problem, is given in Book: Lita, C. V.. 2016 18th International Symposium on Symbolic and Numeric Algorithms for Scientific Computing (SYNASC) . On Complexity of the Detection Problem for Bounded Length Polymorphic Viruses . 2016-09-01. 371–378. 10.1109/SYNASC.2016.064. 978-1-5090-5707-8. 18067130.
  31. (Hopcroft, Ullman, 1979); Exercise S9.13, p. 230–231
  32. This also follows from (1) context-free languages being also context-sensitive, (2) context-sensitive language being closed under intersection, but (3) disjointness of context-free languages being undecidable.
  33. Web site: Mildly Context-Sensitive Grammar Formalisms: Natural Languages are not Context-Free. https://web.archive.org/web/20140819085139/http://user.phil-fak.uni-duesseldorf.de/~kallmeyer/GrammarFormalisms/4nl-cfg.pdf . 2014-08-19 . live. Laura. Kallmeyer. 2011.
  34. Web site: Mildly Context-Sensitive Grammar Formalisms: Linear Context-Free Rewriting Systems. https://web.archive.org/web/20140819085928/http://user.phil-fak.uni-duesseldorf.de/~kallmeyer/GrammarFormalisms/4lcfrs-intro.pdf . 2014-08-19 . live. Laura. Kallmeyer. 2011.
  35. Book: Kallmeyer, Laura. Parsing Beyond Context-Free Grammars. 2010. Springer Science & Business Media. 978-3-642-14846-0. 1–5.