Sub topic 17
The Chomsky Hierarchy
The Chomsky hierarchy classifies formal languages by the restrictions placed on their grammars. It also connects each grammar class with a machine model.
Prerequisites: you should know alphabets, strings, languages, finite automata, and the basic idea of a grammar. The hierarchy is useful because it tells you what kind of memory a language needs and which algorithms may apply.
The four grammar classes
The classes are numbered from Type 0 to Type 3. A larger type number means a more restricted grammar.
| Type | Grammar class | Machine model | Typical example | Column |
|---|---|---|---|---|
| Type 0 | Unrestricted | Turing machine | Halting-accepted languages | |
| Type 1 | Context-sensitive | Linear bounded automaton | `{a^n b^n c^n \ | n >= 1}` |
| Type 2 | Context-free | Pushdown automaton | `{a^n b^n \ | n >= 1}` |
| Type 3 | Regular | Finite automaton | `a*b*` |
The containment relationship is:
Each inclusion is proper. Therefore, some context-free languages are not regular, some context-sensitive languages are not context-free, and some recursively enumerable languages are not context-sensitive.
Production-rule restrictions
The grammar rules become more restrictive as the type number increases.
Type 0: unrestricted grammars
A Type 0 rule has the general form:
α -> βThe left side must contain at least one variable, but there are few other restrictions. These grammars describe exactly the recursively enumerable languages, which are languages accepted by Turing machines.
A Turing machine may run forever on an input that is not accepted.
Type 1: context-sensitive grammars
A context-sensitive grammar generally uses noncontracting rules:
α A β -> α γ βThe replacement does not make the string shorter. Equivalently, each rule satisfies:
|left side| <= |right side|The exception S -> ε may be allowed when the empty string belongs to the language. A linear bounded automaton recognises context-sensitive languages.
For example, {a^n b^n c^n | n >= 1} is context-sensitive but not context-free.
Type 2: context-free grammars
Every rule has one variable on the left:
A -> γA pushdown automaton recognises exactly the context-free languages. The stack allows it to remember an unbounded number of symbols, which is enough for {a^n b^n}, but not for matching three independent blocks such as {a^n b^n c^n}.
Type 3: regular grammars
A right-linear grammar uses rules such as:
A -> aB
A -> a
A -> εA finite automaton recognises the same languages. Since a finite automaton has only finite memory, it cannot compare two arbitrary counts such as the number of a symbols and b symbols.
GATE problem strategy
When classifying a language, first look for its required memory.
- Fixed patterns and finite repetition usually indicate a regular language.
- One nested or matching dependency often indicates a context-free language.
- Several synchronized counts may require a context-sensitive language.
- A language defined through arbitrary Turing-machine computation may be Type 0.
A language can belong to more than one class. If a language is regular, it is also context-free, context-sensitive, and recursively enumerable. In a question asking for the smallest class, choose the lowest type number that contains it.
Worked classification
Consider:
L = { a^n b^n | n >= 1 }A finite automaton cannot store the value of n, so L is not regular. A pushdown automaton can push one stack symbol for every a and pop one for every b. Thus L is context-free. It is therefore also context-sensitive and recursively enumerable, but its smallest class is Type 2.
Exercise
What is the smallest Chomsky class containing the language {a^n b^n c^n | n >= 1}?
Show answer
It is Type 1, context-sensitive. It is not context-free, although every context-free language is context-sensitive.