Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 60

Context-Sensitive Grammars & Linear Bounded Automata

Recommended resources

Hello! Welcome to our next lesson on the theory of computation.

In our previous session, we reached the top of the Chomsky Hierarchy by defining Unrestricted Grammars (Type-0) and establishing their equivalence to Turing Machines. We learned that the power of these grammars comes from their lack of constraints, particularly allowing for "shrinking" production rules where the left-hand side can be longer than the right-hand side.

Today, we will take one step down the hierarchy to explore the class of languages that sits between context-free and recursively enumerable. This lesson addresses the learning outcome: Define Context-Sensitive Grammars (CSGs) and Linear Bounded Automata (LBAs). We will define the grammar formalism for this class and then introduce its corresponding machine model, setting the stage for understanding the full, layered structure of formal languages.

1. Context-Sensitive Grammars (Type-1)

While unrestricted grammars can model any computable process, they are often more powerful than needed. By introducing a single, critical constraint, we arrive at Context-Sensitive Grammars.

Let's begin with a video that formally defines this type of grammar and its production rules.

Context Sensitive Language || Context Sensitive Grammar || TOC || FLAT || Theory of Computation

This video by Sudhakar Atchala introduces Context-Sensitive Grammars (CSGs). It will provide the formal 4-tuple definition and, most importantly, explain the key constraint that differentiates CSGs from unrestricted grammars.

Watch from the beginning until 05:00. Pay close attention to the conditions placed on the production rules, especially the relationship between the length of the left-hand side and the right-hand side.

As the video explained, a grammar is Context-Sensitive (CSG) if its production rules are non-contracting (or length-increasing).

Formally, a grammar is a CSG if all its production rules in are of the form:

where and , with the crucial constraint that:

Let's break this down:

  1. The left-hand side must contain at least one non-terminal, just like in an unrestricted grammar.
  2. The right-hand side must not be empty (it must contain at least one symbol).
  3. The length of the left-hand side cannot be greater than the length of the right-hand side. This means that during a derivation, the sentential form can never get shorter.

(Note: There is a special exception for the empty string, . If the language includes , the rule is allowed, but only if the start symbol does not appear on the right-hand side of any other production.)

An Alternative (and the Original) Definition

The name "context-sensitive" comes from an equivalent, more restrictive definition where rules must be of the form:

Here, A is a single non-terminal, and form the "context", and is a non-empty string. This rule states that A can be replaced by only when it appears in the specific context of being preceded by and followed by .

While the non-contracting definition () is more common today, it's useful to know that it is equivalent in generative power to the original context-based definition. For the rest of our course, we will primarily use the non-contracting definition.

Context Sensitive Grammars and Linear Bounded Automata

For a more formal treatment, these lecture slides from IISc Bangalore present both definitions and state their equivalence. This reinforces the concepts from the video with more formal notation.

Please review slides 4 and 7-9. Slide 4 presents the 'context' definition. Slide 7 introduces the 'non-contracting' definition, and slides 8-9 discuss their equivalence. You don't need to follow the proof of NCG to CSG conversion in detail, but understand that this equivalence holds.

Example: The aⁿbⁿcⁿ Language

We know the language is not context-free. Let's see how a non-contracting grammar can generate it. Consider the following CSG:

Notice the rule . Its LHS and RHS have the same length (2), so it's a valid non-contracting rule. However, its LHS is not a single non-terminal, so it's not a context-free rule. This "swapping" rule is what gives the grammar its power. It allows us to sort the generated Bs and Cs.

Let's derive aabbcc:

  1. S aSBC (generates the outer a and c, and recursively calls S)
  2. a(aBC)BC (replaces inner S with the base case aBC)
  3. aaB(CB)C (grouping to show the target for the swap rule)
  4. aaBBC C (applying CB -> BC)
  5. a(aB)BCC (applying aB -> ab)
  6. aabBCC
  7. aa(bB)CC (applying bB -> bb)
  8. aabbCC
  9. aab(bC)C (applying bC -> bc)
  10. aabbcC
  11. aabb(cC) (applying cC -> cc)
  12. aabbcc

The derivation only produces strings that are growing in length or staying the same length, never shrinking.

Test your understanding!

Which of the following production rules are valid in a non-contracting Context-Sensitive Grammar (ignoring the special case)? Assume A,B,C are non-terminals and a,b are terminals.

  1. aA -> aa
  2. ABC -> ab
  3. AB -> BA
  4. b -> B
  5. A -> a
Show answer
  1. Valid. The LHS has length 2, RHS has length 2. holds (2 2).
  2. Invalid. The LHS has length 3, RHS has length 2. This is a contracting (shrinking) rule, which is not allowed.
  3. Valid. The LHS has length 2, RHS has length 2. holds. This is a common type of rule in CSGs for reordering symbols.
  4. Valid. The LHS has length 1, RHS has length 1. holds.
  5. Valid. The LHS has length 1, RHS has length 1. This is also a valid context-free rule. Remember that context-free grammars (without -productions) are a subset of context-sensitive grammars.

2. Linear Bounded Automata (LBAs)

Just as Finite Automata recognize regular languages and Pushdown Automata recognize context-free languages, there is a machine model for context-sensitive languages: the Linear Bounded Automaton (LBA).

An LBA can be thought of as a non-deterministic Turing Machine with a finite tape. However, the tape isn't of a fixed size. Instead, its size is determined by the input string itself.

Linear Bounded Automata Tape Structure
This diagram shows the core concept of an LBA. The machine's tape is restricted to the cells occupied by the input, bracketed by unique left and right end-markers.

Let's watch a video that describes the LBA model and its formal components.

Linear Bounded Automata || TOC || FLAT || Theory of Computation

This video, also from Sudhakar Atchala, defines the Linear Bounded Automaton. It explains how it relates to other automata, details its formal 7-tuple definition, and describes its operational model.

Please watch from the beginning to 06:39. Focus on what makes an LBA different from a standard Turing Machine, particularly the role of the end markers and the bounded tape.

As the video and diagram illustrate, the key features of an LBA are:

  • It is a non-deterministic Turing Machine. Nondeterminism is essential to its power.
  • Before computation begins, the input string w is placed on the tape, and it is enclosed by two special end markers, often denoted ⊢ (left marker) and ⊣ (right marker).
  • The read/write head can move left and right and modify the tape content between the markers, but it is forbidden from moving past them.
  • This means the amount of memory (tape space) available for computation is directly proportional to—or linearly bounded by—the length of the input string. This links directly to the concept of linear space complexity, O(n).

Formal Definition of an LBA

A Linear Bounded Automaton is a non-deterministic Turing Machine, which can be defined as a 7-tuple , but with two additional constraints on its transition function :

  1. The left end marker ⊢ cannot be overwritten, and the head must move right when it reads ⊢. A typical transition would be δ(q, ⊢) = (p, ⊢, R).
  2. The right end marker ⊣ cannot be overwritten, and the head must move left when it reads ⊣. A typical transition would be δ(q, ⊣) = (p, ⊣, L).

These two rules effectively "fence off" the working area of the tape to be exactly the length of the original input.

The language accepted by an LBA M, denoted L(M), is the set of all strings w for which M enters an accepting state.

Context Sensitive Grammars and Linear Bounded Automata

To see the formal definition in writing, we can refer back to the IISc slides.

Review slides 20-24. These slides provide a formal definition of an LBA as a specialized non-deterministic TM, explain the constraints on its transitions involving end markers, and formally define the language it accepts.

The fundamental theorem connecting our two new concepts is:

Theorem: A language is context-sensitive if and only if it is accepted by a Linear Bounded Automaton.


Conclusion

In this lesson, we have filled a crucial gap in the Chomsky Hierarchy. We moved down from the all-powerful unrestricted grammars to a more constrained, yet still very powerful, class of grammars and machines.

Key Takeaways:

  • A Context-Sensitive Grammar (CSG) is a grammar whose production rules are non-contracting, meaning the length of the left-hand side is less than or equal to the length of the right-hand side ().
  • This non-contracting property means that sentential forms can never shrink during a derivation. This prevents the grammar from generating all recursively enumerable languages but makes it more powerful than a context-free grammar.
  • A Linear Bounded Automaton (LBA) is a non-deterministic Turing Machine whose available tape is restricted to the cells containing the initial input. Its memory is therefore linearly proportional to the input size.
  • CSGs are the grammatical counterpart to LBAs.

Preview of the Next Lesson:
We have defined CSGs and LBAs, and we've stated that they are equivalent. In our next lesson, we will briefly explore the logic behind this equivalence. Then, we will assemble all the pieces we've learned—Regular, Context-Free, Context-Sensitive, and Unrestricted grammars—to formally present the Chomsky Hierarchy and illustrate the precise relationships of containment between these language classes.