Sub topic 14
Chomsky hierarchy and the machine for each level
This chapter assumes you know finite automata, regular expressions, context-free grammars and pushdown automata. It also assumes the Turing machine model and the recursive versus recursively enumerable (RE) distinction from earlier chapters. The aim is to line up four grammar families with four machine models and know which problems each level can and cannot handle.
The four levels
Noam Chomsky classified grammars by the shape of their production rules. Each restriction on the rules weakens the grammar. Each weaker grammar matches a weaker machine.
| Type | Grammar | Rule shape | Machine | Language class | Typical example | Column | Column | Column | Column |
|---|---|---|---|---|---|---|---|---|---|
| 3 | Regular | `A → aB` or `A → a` (right-linear), or the mirror form | DFA or NFA | Regular | `a*b` | ||||
| 2 | Context-free | `A → α`, one nonterminal on the left | Nondeterministic PDA | CFL | `aⁿbⁿ` | ||||
| 1 | Context-sensitive | `α → β` with ` | α | ≤ | β | ` | Linear bounded automaton (LBA) | CSL | `aⁿbⁿcⁿ` |
| 0 | Unrestricted | `α → β`, any strings, `α` non-empty | Turing machine | RE | `{⟨M,w⟩ : M halts on w}` |
An LBA is a nondeterministic Turing machine whose head may not leave the cells that hold the input. The tape length is therefore bounded by a constant multiple of the input length.
Type 1 has a technical exception. S → ε is allowed only when S never appears on a right-hand side.
Read each arrow as "is a proper subset of".
Strict containment and witnesses
Every regular language is context-free, every CFL is context-sensitive, and so on up the chain. Each inclusion is strict, and a witness language shows it:
aⁿbⁿis context-free but not regular. The pumping lemma for regular languages rules out a finite-state machine.aⁿbⁿcⁿis context-sensitive but not context-free. The pumping lemma for CFLs rules out a PDA, because one stack cannot compare three counts.- Some decidable languages are not context-sensitive. This follows from diagonalization over an enumeration of LBAs.
- The halting language is RE but not recursive.
The Turing machine appears in the table as the top level. In the full picture, two classes sit between the context-sensitive languages and Type 0: the recursive languages and the RE languages. Type 0 grammars generate exactly the RE languages. No grammar type corresponds to the recursive class.
Why every CSL is decidable
An LBA on an input of length n has a bounded number of configurations. With |Q| states, |Γ| tape symbols and n cells, there are at most |Q| · n · |Γ|ⁿ configurations.
To decide membership, simulate the LBA breadth-first over configurations. Reject if you see a repeat or exhaust all reachable configurations without accepting. The search is finite, so it always halts.
This is why CSL membership is decidable, while membership for an arbitrary Type 0 grammar is only semi-decidable.
Deterministic versus nondeterministic at each level
| Level | Deterministic equals nondeterministic? |
|---|---|
| Finite automata | Yes. The subset construction converts an NFA to a DFA. |
| Pushdown automata | No. DPDAs accept a proper subset, the deterministic CFLs. |
| Linear bounded automata | Open. This is the LBA problem. |
| Turing machines | Yes. A deterministic machine simulates the nondeterministic one by breadth-first search over configurations. |
aⁿbⁿ, wcwᴿ and balanced parentheses are deterministic CFLs. wwᴿ (even palindromes without a marker) is context-free but not deterministic. A DPDA cannot guess the midpoint.
Closure properties across the hierarchy
GATE often asks which class is closed under which operation.
| Operation | Regular | DCFL | CFL | CSL | Recursive | RE |
|---|---|---|---|---|---|---|
| Union | Yes | No | Yes | Yes | Yes | Yes |
| Intersection | Yes | No | No | Yes | Yes | Yes |
| Complement | Yes | Yes | No | Yes | Yes | No |
| Concatenation | Yes | No | Yes | Yes | Yes | Yes |
| Kleene star | Yes | No | Yes | Yes | Yes | Yes |
| Intersection with a regular language | Yes | Yes | Yes | Yes | Yes | Yes |
Complement closure for CSLs follows from the Immerman–Szelepcsényi theorem. The RE column's missing complement follows from the earlier fact that a language and its complement both being RE makes the language recursive. If RE were closed under complement, every RE language would be recursive.
A small grammar classifier
The classifier below checks the rule shapes for a grammar written as pairs (lhs, rhs). It treats uppercase letters as nonterminals and lowercase letters as terminals. It returns the most restrictive type the grammar satisfies.
import re
def classify(rules):
"""Return the most restrictive Chomsky type (3, 2, 1 or 0) of a grammar."""
def right(r):
return re.fullmatch(r"[a-z]*[A-Z]?", r) is not None
def left(r):
return re.fullmatch(r"[A-Z]?[a-z]*", r) is not None
single = all(len(l) == 1 and l.isupper() for l, _ in rules)
if single and (all(right(r) for _, r in rules) or all(left(r) for _, r in rules)):
return 3
if single:
return 2
s_on_rhs = any("S" in r for _, r in rules)
noncontracting = all(
len(l) <= len(r) or (l == "S" and r == "" and not s_on_rhs)
for l, r in rules
)
return 1 if noncontracting else 0
grammars = {
"a*b": [("S", "aS"), ("S", "b")],
"a^n b^n": [("S", "aSb"), ("S", "")],
"a^n b^n c^n": [("S", "aSBC"), ("S", "aBC"), ("CB", "BC"),
("aB", "ab"), ("bB", "bb"), ("bC", "bc"), ("cC", "cc")],
"shrinking rule": [("S", "AB"), ("AB", "a")],
}
for name, rules in grammars.items():
print(name, classify(rules))
# a*b 3, a^n b^n 2, a^n b^n c^n 1, shrinking rule 0The check looks only at the shape of the rules, not at the language they generate. A grammar can be labeled Type 0 even though its language is regular. The language's class is the lowest level for which any grammar or machine exists.
Common errors
- Calling a grammar's type the language's type. A badly written grammar for a regular language can be Type 2 or Type 1. Always ask for the simplest machine that accepts the language.
- Placing the recursive languages in the wrong spot. Type 0 means RE, not recursive. Decidable languages that are not context-sensitive exist, but no grammar type isolates them.
- Assuming a PDA with two stacks is a PDA. It is as powerful as a Turing machine. The single stack is what limits PDAs.
- Treating
aⁿbⁿcⁿas context-free. It needs three synchronized counts. Onlyaⁿbⁿand similar two-count languages are context-free. - Mixing deterministic and nondeterministic PDAs. Nondeterminism adds power for PDAs, but not for DFAs or Turing machines.
- Confusing
aⁿbᵐcⁿwithaⁿbⁿcⁿ. The first is context-free because thebblock is independent. The second is not.
Decision questions in GATE style
To place a language, work from the bottom of the hierarchy upward:
- Can a finite number of states do it, with no unbounded counter? If so, it is regular.
- Does it need one unbounded counter, or a stack with matching, such as
aⁿbⁿorwwᴿ? If so, it is context-free. - Does it need to compare two or more independent counts, such as
aⁿbⁿcⁿ,wworaⁿbⁿ²? If so, it is context-sensitive. Check that the computation fits in linear space. - Does it require unbounded space or an unbounded search, and is the answer always eventually produced? If so, it is recursive.
- Can you only confirm membership but never refute it? Then the language is RE and not recursive.
Exercise
Which is the smallest class in the Chomsky hierarchy that contains L = { ww : w ∈ {a,b}* }, and which machine type is it, at minimum, for L?
Show answer
L is context-sensitive and not context-free. The pumping lemma for CFLs rules out a PDA, because the string a^p b^p a^p b^p cannot be pumped while staying in L. An LBA decides it by matching the first and second halves cell by cell, using only the input cells. So the smallest class is Type 1, with an LBA as the machine.
Exercise
True or false: if L is a context-sensitive language, then membership in L is semi-decidable but not necessarily decidable.
Show answer
False. Membership in a context-sensitive language is decidable. An LBA on an input of length n has at most |Q| · n · |Γ|^n configurations, so simulation either accepts, rejects or repeats a configuration in finite time.