ArticleslgStudy

science

Context-sensitive grammar

Context-sensitive grammar is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Context-sensitive grammar rather than just read about it. In short: 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.

Key takeaways

  • Context-sensitive grammar belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Context-sensitive grammar to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Context-sensitive grammar from memory before moving on to harder problems.

Reference excerpt

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. 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 the form

αAβ → αγβ with 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.

Examples

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

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 CB → BC 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 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 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: Start with a kernel of regular productions generating the sentential forms (ABCD)nabcd and then include the non contracting productions pDa:Da→aD, pDb:Db→bD, pDc:Dc→cD, pDd:Dd→dd, pCa:Ca→aC, pCb:Cb→bC, pCc:Cc→cc, pBa:Ba→aB, pBb:Bb→bb, pAa:Aa→aa.

ambncmdn A non contracting grammar (for which there is an equivalent CSG) for the language LCross={ambncmdn∣m≥1,n≥1} is defined by

p0:S→RT, p1:R→aRC|aC, p3:T→BTd|Bd, p5:CB→BC, p6:aB→ab, p7:bB→bb, p8:Cd→cd, and p9:Cc→cc. With these definitions, a derivation for a3b2c3d2 is: S⇒p0RT⇒p12p2a3C3T⇒p3p4a3C3B2d2⇒p56a3B2C3d2⇒p6p7a3b2C3d2⇒p8p92a3b2c3d2.

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

S→[ACaB] { [Ca]a→aa[Ca] [Ca][aB]→aa[CaB] [ACa]a→[Aa]a[Ca] [ACa][aB]→[Aa]a[CaB] [ACaB]→[Aa][aCB] [CaB]→a[aCB] [aCB]→[aDB] [aCB]→[aE] { a[Da]→[Da]a [aDB]→[DaB] [Aa][Da]→[ADa]a a[DaB]→[Da][aB] [Aa][DaB]→[ADa][aB] [ADa]→[ACa] { a[Ea]→[Ea]a [aE]→[Ea] [Aa][Ea]→[AEa]a [AEa]→a

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Context-sensitive grammar

Start with the simplest possible case. Write down what Context-sensitive grammar claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Context-sensitive grammar before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Context-sensitive grammar ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Context-sensitive grammar

In research
Context-sensitive grammar appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Context-sensitive grammar in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Context-sensitive grammar is common in secondary-school and first-year university syllabi. It links to neighbouring topics Formal languages, Grammar frameworks, so understanding it makes those chapters shorter.
In everyday life
Look for Context-sensitive grammar outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Context-sensitive grammar” →

How to study Context-sensitive grammar in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Context-sensitive grammar means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Context-sensitive grammar out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Context-sensitive grammar in simple terms?

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 lan…

Why does Context-sensitive grammar matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Context-sensitive grammar?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Context-sensitive grammar.

Tags

  • Formal languages
  • Grammar frameworks

Keep exploring