Sub topic 17
The Chomsky hierarchy
This chapter assumes you know regular languages, context-free grammars (CFGs), pushdown automata (PDAs), and the pumping lemma for context-free languages (CFLs). It ties them together into one ladder of language classes. GATE usually asks you to place a language on this ladder or to compare two rungs.
Four grammar types
Noam Chomsky classified grammars by the shape of their productions. Each type puts more restrictions on the previous one. Here α and β are strings of variables and terminals, A and B are variables, and a is a terminal.
| Type | Language class | Production shape | Machine | Column | Column | Column | Column |
|---|---|---|---|---|---|---|---|
| 3 | Regular | A → aB, A → a, A → ε (right-linear), or the left-linear mirror | DFA, NFA | ||||
| 2 | Context-free | A → α | NPDA | ||||
| 1 | Context-sensitive | α → β with \ | α\ | ≤ \ | β\ | Linear bounded automaton (LBA) | |
| 0 | Recursively enumerable (RE) | α → β with α containing a variable | Turing machine |
Two details matter:
- A context-sensitive grammar may also have S → ε, provided S never appears on a right-hand side.
- The rules for a type-3 grammar must all be right-linear or all be left-linear. Mixing the two can produce a non-regular language, such as S → aB, B → Sb, S → ε, which generates a^n b^n.
Containment and witness languages
Every regular language is context-free, every CFL is context-sensitive, and so on. Each inclusion is proper. The deterministic CFLs (DCFLs) sit between regular and context-free, and the decidable (recursive) languages sit between context-sensitive and RE.
A witness is a language that lies in one class but not the class below it. These are worth memorising:
| Language | Smallest class | Why it is not lower |
|---|---|---|
| a^n b^n | DCFL | Fails the regular pumping lemma. |
| w w^R (even-length palindromes) | CFL | A PDA must guess the middle, so no DPDA accepts it. |
| a^i b^j c^k with i = j or j = k | CFL | A DPDA cannot decide in advance which equality to check. |
| a^n b^n c^n | CSL | Fails the CFL pumping lemma. |
| Halting problem language | RE | Not decidable. |
Some languages are not even RE. There are only countably many Turing machines but uncountably many languages, so most languages have no machine at all. The complement of the halting language is one example.
Classifying a grammar by its productions
The grammar type depends only on the production shapes, so you can test it mechanically. The function below returns the highest type number that fits, treating uppercase letters as variables and lowercase letters as terminals. It accepts a rule like A → abB, which is regular as a language even though it is not strictly in the one-terminal form.
def grammar_type(rules, start="S"):
"""rules: list of (lhs, rhs) strings. Uppercase = variable, lowercase = terminal."""
def is_var(c):
return c.isupper()
def right_linear(r):
body = r[:-1] if r and is_var(r[-1]) else r
return not any(is_var(c) for c in body)
def left_linear(r):
body = r[1:] if r and is_var(r[0]) else r
return not any(is_var(c) for c in body)
single_var = all(len(l) == 1 and is_var(l) for l, _ in rules)
if single_var and (all(right_linear(r) for _, r in rules)
or all(left_linear(r) for _, r in rules)):
return 3
if single_var:
return 2
start_on_rhs = any(start in r for _, r in rules)
noncontracting = all(
len(l) <= len(r) or (l == start and r == "" and not start_on_rhs)
for l, r in rules
)
return 1 if noncontracting else 0
print(grammar_type([("S", "aS"), ("S", "b")])) # 3
print(grammar_type([("S", "aSb"), ("S", "ab")])) # 2
print(grammar_type([("S", "aSBC"), ("S", "aBC"), ("CB", "BC"),
("aB", "ab"), ("bB", "bb"), ("bC", "bc"),
("cC", "cc")])) # 1
print(grammar_type([("S", "aA"), ("aA", "")])) # 0The third grammar generates a^n b^n c^n. Its rule CB → BC has a two-symbol left side, which rules out type 2, but no rule shrinks the string, so it is type 1.
Nondeterminism across the hierarchy
Adding nondeterminism changes the power of a machine differently at each level:
- DFA and NFA recognise the same class.
- DPDA is strictly weaker than NPDA.
- A deterministic Turing machine and a nondeterministic one recognise the same class.
- Whether deterministic LBAs equal nondeterministic LBAs is the open LBA problem. Do not claim either answer on an exam.
Closure properties
The table shows whether each class is closed under the operation. Y means closed and N means not closed.
| Class | Union | Concatenation | Star | Intersection | Complement |
|---|---|---|---|---|---|
| Regular | Y | Y | Y | Y | Y |
| DCFL | N | N | N | N | Y |
| CFL | Y | Y | Y | N | N |
| CSL | Y | Y | Y | Y | Y |
| Recursive | Y | Y | Y | Y | Y |
| RE | Y | Y | Y | Y | N |
Both CFLs and DCFLs are closed under intersection with a regular language. The CSL complement result is the Immerman–Szelepcsényi theorem. RE is not closed under complement, because a language and its complement both being RE makes the language recursive.
Decision problems
The table shows whether each question is decidable for a given class, where D means decidable and U means undecidable.
| Class | Membership | Emptiness | Equivalence |
|---|---|---|---|
| Regular | D | D | D |
| DCFL | D | D | D |
| CFL | D | D | U |
| CSL | D | U | U |
| RE | U | U | U |
Finiteness is decidable for CFLs, and universality (does the grammar generate every string?) is undecidable for CFLs. Ambiguity of a CFG is also undecidable. Membership for CFLs is solved by CYK, and for CSLs by exhaustive search within the linear space bound.
Placing a language: a procedure
- Try to build a DFA or a regular expression. If that works, the language is regular.
- Try to build a DPDA, or a grammar with a clear stack discipline. If it works, the language is a DCFL or CFL. Ask whether the machine must guess a midpoint, which signals a non-deterministic CFL.
- If neither works, try the CFL pumping lemma. A successful proof removes the language from type 2.
- For type 1, look for a bounded-space algorithm. If none exists but the language is decidable, it is recursive. If only a semi-decision procedure exists, it is RE.
Exercise
Is L = { a^n b^n c^n : n ≥ 1 } ∪ { a^i b^j : i, j ≥ 1 } context-free? Which class is it in?
Show answer
It is not context-free. Intersect L with the regular language a+b+c+. Because every string in a^i b^j has no c, the intersection is exactly { a^n b^n c^n : n ≥ 1 }. CFLs are closed under intersection with regular languages, so L being context-free would force a^n b^n c^n to be context-free. It is not, so L is not a CFL. L is context-sensitive, because a linear-space machine can check the counts.
Exercise
A grammar has productions S → aSb | A and A → aA | ε. State its type by the production shapes, and name the smallest class its language belongs to.
Show answer
Every left-hand side is a single variable, and S → aSb has a variable in the middle of the right-hand side, so the grammar is type 2. The language is { a^i a^n b^n : i ≥ 0, n ≥ 0 } wait, check the derivation: S → aSb repeated n times gives a^n S b^n, then S → A gives a^n a^k b^n. This equals { a^m b^n : m ≥ n }, which needs a stack to compare counts and is not regular. A DPDA can accept it, so the smallest class is the DCFLs.