Context-sensitive Grammar
   HOME

TheInfoList



OR:

A context-sensitive grammar (CSG) is a
formal grammar In formal language theory, a grammar (when the context is not given, often called a formal grammar for clarity) describes how to form strings from a language's alphabet that are valid according to the language's syntax. A grammar does not describe ...
in which the left-hand sides and right-hand sides of any production rules may be surrounded by a context of
terminal Terminal may refer to: Computing Hardware * Terminal (electronics), a device for joining electrical circuits together * Terminal (telecommunication), a device communicating over a line * Computer terminal, a set of primary input and output dev ...
and
nonterminal symbol In computer science, terminal and nonterminal symbols are the lexical elements used in specifying the production rules constituting a formal grammar. ''Terminal symbols'' are the elementary symbols of the language defined by a formal grammar. ...
s. Context-sensitive grammars are more general than
context-free grammar In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be empt ...
s, 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 grammar In automata theory, the class of unrestricted grammars (also called semi-Thue, type-0 or phrase structure grammars) is the most general class of grammars in the Chomsky hierarchy. No restrictions are made on the productions of an unrestricted gramma ...
s. Thus, CSG are positioned between context-free and unrestricted grammars in the
Chomsky hierarchy In formal language theory, computer science and linguistics, the Chomsky hierarchy (also referred to as the Chomsky–Schützenberger hierarchy) is a containment hierarchy of classes of formal grammars. This hierarchy of grammars was described by ...
. A
formal language In logic, mathematics, computer science, and linguistics, a formal language consists of words whose letters are taken from an alphabet and are well-formed according to a specific set of rules. The alphabet of a formal language consists of symb ...
that can be described by a context-sensitive grammar, or, equivalently, by a
noncontracting grammar In formal language theory, a formal grammar, grammar is noncontracting (or monotonic) if all of its production rules are of the form α → β where α and β are string (formal languages)#Formal theory, strings of nonterminal and termina ...
or a
linear bounded automaton In computer science, a linear bounded automaton (plural linear bounded automata, abbreviated LBA) is a restricted form of Turing machine. Operation A linear bounded automaton is a nondeterministic Turing machine that satisfies the following thre ...
, is called a
context-sensitive language In formal language theory, a context-sensitive language is a language that can be defined by a context-sensitive grammar (and equivalently by a noncontracting grammar). Context-sensitive is one of the four types of grammars in the Chomsky hierarc ...
. Some textbooks actually define CSGs as non-contracting, although this is not how
Noam Chomsky Avram Noam Chomsky (born December 7, 1928) is an American public intellectual: a linguist, philosopher, cognitive scientist, historian, social critic, and political activist. Sometimes called "the father of modern linguistics", Chomsky is ...
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 In neuropsychology, linguistics, and philosophy of language, a natural language or ordinary language is any language that has evolved naturally in humans through use and repetition without conscious planning or premeditation. Natural languages ...
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 Walter John Savitch (February 21, 1943 – February 1, 2021) was best known for defining the complexity class NL (complexity), NL (nondeterministic logarithmic space), and for Savitch's theorem, which defines a relationship between the NSPACE and ...
has criticized the terminology "context-sensitive" as misleading and proposed "non-erasing" as better explaining the distinction between a CSG and an
unrestricted grammar In automata theory, the class of unrestricted grammars (also called semi-Thue, type-0 or phrase structure grammars) is the most general class of grammars in the Chomsky hierarchy. No restrictions are made on the productions of an unrestricted gramma ...
. 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 language In computational linguistics, the term mildly context-sensitive grammar formalisms refers to several grammar formalisms that have been developed in an effort to provide adequate descriptions of the syntactic structure of natural language. Every m ...
s. The syntaxes of some
visual programming language In computing, a visual programming language (visual programming system, VPL, or, VPS) is any programming language that lets users create programs by manipulating program elements ''graphically'' rather than by specifying them ''textually''. A VP ...
s can be described by context-sensitive
graph grammar In computer science, graph transformation, or graph rewriting, concerns the technique of creating a new graph out of an original graph algorithmically. It has numerous applications, ranging from software engineering (software construction and also ...
s.


Formal definition

A
formal grammar In formal language theory, a grammar (when the context is not given, often called a formal grammar for clarity) describes how to form strings from a language's alphabet that are valid according to the language's syntax. A grammar does not describe ...
''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 ''A'' ∈ ''N'',i.e., ''A'' a single
nonterminal In computer science, terminal and nonterminal symbols are the lexical elements used in specifying the production rules constituting a formal grammar. ''Terminal symbols'' are the elementary symbols of the language defined by a formal grammar. ...
α,β ∈ (''N''∪Σ)* i.e., α and β strings of nonterminals and terminals and γ ∈ (''N''∪Σ)+.i.e., γ is a nonempty string of nonterminals and terminals A string ''u'' ∈ (''N''∪Σ)* directly yields, or directly derives to, a string ''v'' ∈ (''N''∪Σ)*, denoted as ''u'' ⇒ ''v'', 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'' = ''u''1 ⇒ ... ⇒ ''u''''n'' = ''v'' for some ''n''≥0 and some strings ''u''2, ..., ''u''''n''-1 (''N''∪Σ)*. That is, the relation (⇒*) is the
reflexive transitive closure In mathematics, a subset of a given set is closed under an operation of the larger set if performing that operation on members of the subset always produces a member of that subset. For example, the natural numbers are closed under addition, but ...
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'') = . 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 grammar In automata theory, the class of unrestricted grammars (also called semi-Thue, type-0 or phrase structure grammars) is the most general class of grammars in the Chomsky hierarchy. No restrictions are made on the productions of an unrestricted gramma ...
s is that γ can be empty in the unrestricted case. 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, see Noncontracting grammar#Transforming into context-sensitive grammar. In addition, a rule of the form : ''S'' → λ where λ represents the
empty string In formal language theory, the empty string, or empty word, is the unique string of length zero. Formal theory Formally, a string is a finite, ordered sequence of characters such as letters, digits or spaces. The empty string is the special cas ...
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 In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules are of the form :A\ \to\ \alpha with A a ''single'' nonterminal symbol, and \alpha a string of terminals and/or nonterminals (\alpha can be empt ...
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. 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. also at https://www.encyclopediaofmath.org/index.php/Grammar,_context-sensitive The equivalence was established by
Penttonen normal form In formal language theory, a context-sensitive grammar is in Kuroda normal form if all production rules are of the form: :''AB'' → ''CD'' or :''A'' → ''BC'' or :''A'' → ''B'' or :''A'' → ''a'' where A, B, C and D are nonterminal symbols an ...
. citing


Examples


''a''''n''''b''''n''''c''''n''

The following context-sensitive grammar, with start symbol ''S'', generates the canonical non-
context-free language In formal language theory, a context-free language (CFL) is a language generated by a context-free grammar (CFG). Context-free languages have many applications in programming languages, in particular, most arithmetic expressions are generated by ...
: Rules 1 and 2 allow for blowing-up ''S'' to ''a''''n''''BC''(''BC'')''n''-1; rules 3 to 6 allow for successively exchanging each ''CB'' to ''BC'' ( four rules are needed for that since a rule ''CB'' → ''BC'' 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 ' is: : ''S'' : →2 : →2 : →1 : →3 : →4 : →5 : →6 : →3 : →4 : →5 : →6 : →3 : →4 : →5 : →6 : →7 : →8 : →8 : →9 : →10 : →10 :


''a''''n''''b''''n''''c''''n''''d''''n'', etc.

More complicated grammars CSGcan 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)^abcd and then include the non contracting productions p_ : Da\rightarrow aD, p_ : Db\rightarrow bD, p_ : Dc\rightarrow cD, p_ : Dd\rightarrow dd, p_ : Ca\rightarrow aC, p_ : Cb\rightarrow bC, p_ : Cc\rightarrow cc, p_ : Ba\rightarrow aB, p_ : Bb\rightarrow bb, p_ : Aa\rightarrow aa.


''a''''m''''b''''n''''c''''m''''d''''n''

A non contracting grammar (for which there is an equivalent CSG) for the language L_ = \ is defined by p_1 : R\rightarrow aRC , aC and p_3 : T\rightarrow BTd , Bd, p_5 : CB\rightarrow BC, p_0 : S \rightarrow RT, p_6 : aB\rightarrow ab, p_7 : bB\rightarrow bb, p_8 : Cd\rightarrow cd, p_9 : Cc\rightarrow cc. With these definitions, a derivation for a^3b^2c^3d^2 is: S \Rightarrow_ RT \Rightarrow_ a^3C^3T \Rightarrow_ a^3C^3B^2d^2 \Rightarrow_ a^3B^2C^3d^2 \Rightarrow_ a^3b^2C^3d^2 \Rightarrow_ a^3b^2c^3d^2 .


''a''2i

A noncontracting grammar for the language is constructed in Example 9.5 (p. 224) of (Hopcroft, Ullman, 1979): # S\rightarrow CaB/math> # \begin \ a\rightarrow aa a\\ \ aaB]\rightarrow aa aB\\ \ Ca\rightarrow a a\\ \ CaaB]\rightarrow a aB\\ \ CaBrightarrow aaCB] \\ \ aBrightarrow a CB\end # CBrightarrow DB/math> # CBrightarrow E/math> # \begin \ a arightarrow a \\ \ DBrightarrow aB\\ \ aDa]\rightarrow Da \\ \ a aBrightarrow aaB] \\ \ aDaB]\rightarrow DaaB] \end # Darightarrow Ca/math> # \begin \ a arightarrow a \\ \ Erightarrow a\\ \ aEa]\rightarrow Ea \end # Earightarrow 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 In formal language theory, a context-sensitive grammar is in Kuroda normal form if all production rules are of the form: :''AB'' → ''CD'' or :''A'' → ''BC'' or :''A'' → ''B'' or :''A'' → ''a'' where A, B, C and D are nonterminal symbols and ...
. "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 In formal language theory, a formal grammar, grammar is noncontracting (or monotonic) if all of its production rules are of the form α → β where α and β are string (formal languages)#Formal theory, strings of nonterminal and termina ...
. 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 In computer science, a linear bounded automaton (plural linear bounded automata, abbreviated LBA) is a restricted form of Turing machine. Operation A linear bounded automaton is a nondeterministic Turing machine that satisfies the following thre ...
(LBA). In some textbooks this result is attributed solely to Landweber and
Kuroda Kuroda (written: lit. "black ricefield") is a Japanese surname. Notable people with the surname include: *, Japanese painter * Akinobu Kuroda 黒田 明伸, Japanese historian * Chris Kuroda, lighting designer and operator for the band Phish and J ...
. Others call it the Myhill–Landweber–Kuroda theorem. (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.) it is still an open question whether every context-sensitive language can be accepted by a ''deterministic'' LBA.


Closure properties

Context-sensitive languages are closed under
complement A complement is something that completes something else. Complement may refer specifically to: The arts * Complement (music), an interval that, when added to another, spans an octave ** Aggregate complementation, the separation of pitch-class ...
. This 1988 result is known as the
Immerman–Szelepcsényi theorem In computational complexity theory, the Immerman–Szelepcsényi theorem states that nondeterministic space complexity classes are closed under complementation. It was proven independently by Neil Immerman and Róbert Szelepcsényi in 1987, for wh ...
. Moreover, they are closed under
union Union commonly refers to: * Trade union, an organization of workers * Union (set theory), in mathematics, a fundamental operation on sets Union may also refer to: Arts and entertainment Music * Union (band), an American rock group ** ''Un ...
,
intersection In mathematics, the intersection of two or more objects is another object consisting of everything that is contained in all of the objects simultaneously. For example, in Euclidean geometry, when two lines in a plane are not parallel, their i ...
,
concatenation In formal language, formal language theory and computer programming, string concatenation is the operation of joining character string (computer science), character strings wikt:end-to-end, end-to-end. For example, the concatenation of "sno ...
,
substitution Substitution may refer to: Arts and media *Chord substitution, in music, swapping one chord for a related one within a chord progression * Substitution (poetry), a variation in poetic scansion * "Substitution" (song), a 2009 song by Silversun Pi ...
,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 inverse homomorphism, and
Kleene plus In mathematical logic and computer science, the Kleene star (or Kleene operator or Kleene closure) is a unary operation, either on sets of strings or on sets of symbols or characters. In mathematics, it is more commonly known as the free monoid c ...
. Every
recursively enumerable language In mathematics, logic and computer science, a formal language is called recursively enumerable (also recognizable, partially decidable, semidecidable, Turing-acceptable or Turing-recognizable) if it is a recursively enumerable subset in the set o ...
''L'' can be written as ''h''(''L'') for some context-sensitive language ''L'' and some string homomorphism ''h''.


Computational problems

The
decision problem In computability theory and computational complexity theory, a decision problem is a computational problem that can be posed as a yes–no question of the input values. An example of a decision problem is deciding by means of an algorithm whethe ...
that asks whether a certain string ''s'' belongs to the language of a given context-sensitive grammar ''G'', is
PSPACE-complete In computational complexity theory, a decision problem is PSPACE-complete if it can be solved using an amount of memory that is polynomial in the input length (polynomial space) and if every other problem that can be solved in polynomial space can b ...
. 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). The
emptiness problem In theoretical computer science and formal language theory, a formal language is empty if its set of valid sentences is the empty set. The emptiness problem is the question of determining whether a language is empty given some representation of it, ...
for context-sensitive grammars (given a context-sensitive grammar ''G'', is ''L''(''G'')=∅ ?) is undecidable.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.


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 In computability theory, a set ''S'' of natural numbers is called computably enumerable (c.e.), recursively enumerable (r.e.), semidecidable, partially decidable, listable, provable or Turing-recognizable if: *There is an algorithm such that the ...
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''. It has been shown that nearly all
natural language In neuropsychology, linguistics, and philosophy of language, a natural language or ordinary language is any language that has evolved naturally in humans through use and repetition without conscious planning or premeditation. Natural languages ...
s may in general be characterized by context-sensitive grammars, but the whole class of CSG's seems to be much bigger than natural languages. 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 The P versus NP problem is a major unsolved problem in theoretical computer science. In informal terms, it asks whether every problem whose solution can be quickly verified can also be quickly solved. The informal term ''quickly'', used abov ...
. It was proven that some natural languages are not context-free, based on identifying so-called
cross-serial dependencies In linguistics, cross-serial dependencies (also called crossing dependencies by some authors.) occur when the lines representing the dependency relations between two series of words cross over each other.. They are of particular interest to ling ...
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 system Generalized context-free grammar (GCFG) is a grammar formalism that expands on context-free grammars by adding potentially non-context-free composition functions to rewrite rules. Head grammar (and its weak equivalents) is an instance of such a GC ...
s (LCFRS) can account for the phenomenon of cross-serial dependencies; one can write a LCFRS grammar for for example. Ongoing research on
computational linguistics Computational linguistics is an Interdisciplinarity, interdisciplinary field concerned with the computational modelling of natural language, as well as the study of appropriate computational approaches to linguistic questions. In general, comput ...
has focused on formulating other classes of languages that are " mildly context-sensitive" whose decision problems are feasible, such as
tree-adjoining grammar Tree-adjoining grammar (TAG) is a grammar formalism defined by Aravind Joshi. Tree-adjoining grammars are somewhat similar to context-free grammars, but the elementary unit of rewriting is the tree rather than the symbol. Whereas context-free gram ...
s,
combinatory categorial grammar Combinatory categorial grammar (CCG) is an efficiently parsable, yet linguistically expressive grammar formalism. It has a transparent interface between surface syntax and underlying semantic representation, including predicate–argument structure ...
s,
coupled context-free language ''Coupled'' is an American dating game show that aired on Fox Broadcasting Company, Fox from May 17 to August 2, 2016. It was hosted by television personality, Terrence J and created by Mark Burnett, of ''Survivor (U.S. TV series), Survivor'', ''T ...
s, and
linear context-free rewriting system Generalized context-free grammar (GCFG) is a grammar formalism that expands on context-free grammars by adding potentially non-context-free composition functions to rewrite rules. Head grammar (and its weak equivalents) is an instance of such a GC ...
s. The languages generated by these formalisms properly lie between the context-free and context-sensitive languages. More recently, the class
PTIME In computational complexity theory, P, also known as PTIME or DTIME(''n''O(1)), is a fundamental complexity class. It contains all decision problems that can be solved by a deterministic Turing machine using a polynomial amount of computation time ...
has been identified with
range concatenation grammar Range concatenation grammar (RCG) is a grammar formalism developed by Pierre Boullier in 1998 as an attempt to characterize a number of phenomena of natural language, such as Chinese numbers and German word order scrambling, which are outside the b ...
s, which are now considered to be the most expressive of the mild-context sensitive language classes.


See also

*
Chomsky hierarchy In formal language theory, computer science and linguistics, the Chomsky hierarchy (also referred to as the Chomsky–Schützenberger hierarchy) is a containment hierarchy of classes of formal grammars. This hierarchy of grammars was described by ...
* Growing context-sensitive grammar * Definite clause grammar#Non-context-free grammars * List of parser generators for context-sensitive grammars


Notes


References


Further reading

*


External links


Earley Parsing for Context-Sensitive Grammars
{{DEFAULTSORT:Context-Sensitive Grammar Formal languages Grammar frameworks