G27
Self-checkedTier 1 10 min readtoc.chomsky-hierarchy

Chomsky hierarchy

The four types of grammars (unrestricted, context-sensitive, context-free, regular), the language classes and machines that match them (recursively enumerable and Turing machines, context-sensitive and linear bounded automata, context-free and PDAs, regular and finite automata), the strict inclusions with a witness language for each step, classifying a grammar by the shape of its productions, a context-sensitive grammar for a^n b^n c^n, and the closure and decidability summary.

Why it exists

The classes of languages met so far, regular, deterministic context-free and context-free, fit into a larger chain that ends with everything a computer can recognise. Noam Chomsky classified grammars by the shape of their productions; each shape matches a kind of machine. GATE asks for the chain, the machine of each class, strict inclusion with an example language for each step, and the type of a given grammar.

Prerequisites

You need the closure properties of both regular and context-free languages (regular closure, CFL closure); the top of the chain is the Turing machine (Turing machines).

Precise definitions

A grammar G=(V,Σ,R,S)G = (V, \Sigma, R, S) with productions α→β\alpha \to \beta, α∈(V∪Σ)∗V(V∪Σ)∗\alpha \in (V \cup \Sigma)^* V (V \cup \Sigma)^* (at least one variable on the left) and β∈(V∪Σ)∗\beta \in (V \cup \Sigma)^*. The type of GG:

typenameshape of every productionmachineclass of languages
3regularA→aBA \to aB, A→aA \to a, A→εA \to \varepsilon (right-linear) or A→BaA \to Ba, A→aA \to a, A→εA \to \varepsilon (left-linear)finite automatonregular
2context-freeA→γA \to \gamma with a single variable AA on the leftpushdown automatoncontext-free
1context-sensitiveα→β\alpha \to \beta with ∣α∣≤∣β∣\lvert\alpha\rvert \le \lvert\beta\rvert (nothing shrinks); S→εS \to \varepsilon allowed if SS is on no right sidelinear bounded automatoncontext-sensitive
0unrestrictedany α→β\alpha \to \betaTuring machinerecursively enumerable

(The name "context-sensitive" comes from the equivalent form αAβ→αγβ\alpha A \beta \to \alpha \gamma \beta with γ≠ε\gamma \ne \varepsilon: the variable AA is replaced only in the context α_β\alpha \_ \beta; the form with ∣α∣≤∣β∣\lvert\alpha\rvert \le \lvert\beta\rvert is also called monotone or non-contracting; they generate the same languages, a textbook result.)

A language has type kk if some Type kk grammar generates it. The inclusions are

Type 3  ⊊  Type 2  ⊊  Type 1  ⊊  Type 0.\text{Type 3} \;\subsetneq\; \text{Type 2} \;\subsetneq\; \text{Type 1} \;\subsetneq\; \text{Type 0}.

Between them sit the deterministic CFLs (between regular and context-free) and the recursive (decidable) languages (between context-sensitive and recursively enumerable).

How it works

The grammar types by example

The table gives six grammars; the program decides the type from the shape of the productions:

Which type is this grammar: start

Computed by a script, not typed
grammartype (by the form of the productions)shortest strings generated
S → aS | b
S → Sa | b
S → aA; A → Sb | b
S → aSb | ε
S → aSBC | aBC; CB → BC; aB → ab; bB → bb; bC → bc; cC → cc
S → aSb | AB; AB → ε

Step 0

The type of a grammar is decided by the SHAPE of its productions, not by the language: the last grammar generates the context-free language a^n b^n but is Type 0 because of AB → ε. The third grammar mixes a right-linear and a left-linear production, so it is not Type 3, although its language a^n b^n is context-free.

0 / 6
computed by script: the machine is simulated or the sets are constructed by scripts/toc_lib.py, one step at a time. python3 scripts/gen-toc4-traces.py

Points that the table makes:

  • The type depends on the shape, not the language. S→aSb∣ABS \to aSb \mid AB, AB→εAB \to \varepsilon generates {anbn}\{a^n b^n\}, which is context-free, but the production AB→εAB \to \varepsilon has two symbols on the left and shrinks: the grammar is Type 0.
  • A grammar whose productions are all right-linear or all left-linear is Type 3. A grammar that mixes them is not: S→aAS \to aA, A→Sb∣bA \to Sb \mid b generates anbna^n b^n (n≥1n \ge 1); it is context-free (Type 2) but not regular.
  • Every regular grammar is context-free; every context-free grammar without ε\varepsilon-productions is context-sensitive (a variable replaced by a nonempty string never shrinks).

The strict inclusions and their witnesses

stepwitness languagewhy
Type 3 ⊊\subsetneq Type 2{anbn}\{a^n b^n\}context-free grammar S→aSb∣εS \to aSb \mid \varepsilon; not regular (pumping lemma)
Type 2 ⊊\subsetneq Type 1{anbncn}\{a^n b^n c^n\}context-sensitive grammar below; not context-free (pumping lemma)
Type 1 ⊊\subsetneq Type 0the language of the halting problem, and also some decidable languagesnot decidable at all (next batch) / diagonalisation (quoted)

A grammar for {anbncn:n≥1}\{a^n b^n c^n : n \ge 1\} that is context-sensitive (no production shrinks):

S→aSBC∣aBC,CB→BC,aB→ab,bB→bb,bC→bc,cC→cc.S \to aSBC \mid aBC, \qquad CB \to BC, \qquad aB \to ab, \quad bB \to bb, \qquad bC \to bc, \quad cC \to cc.

Idea: generate an(BC)na^n (BC)^n first, let BB's and CC's sort themselves with CB→BCCB \to BC until all BB's precede all CC's, then convert from the left: aB→abaB \to ab, bB→bbbB \to bb, bC→bcbC \to bc, cC→cccC \to cc. Because no production shrinks the sentential form, membership is decidable by searching all sentential forms up to the length of the string; the program did exactly this and found that the language is anbncna^n b^n c^n with n≥1n \ge 1 (all strings up to length 1212) and that it accepts exactly those strings among all strings over {a,b,c}\{a, b, c\} up to length 99.

Machines and decidability

  • Type 3: finite automaton (DFA = NFA). Membership: linear time.
  • Type 2: pushdown automaton. Membership: cubic time (CYK).
  • Type 1: linear bounded automaton (a Turing machine whose head never leaves the cells of the input); membership is decidable (by the search above) but needs exponential time with the brute-force method.
  • Type 0: Turing machine; membership is only semi-decidable: the machine halts and accepts strings of the language but may run forever on others (decidable and recognisable languages).

Closure summary (quoted textbook results)

operationregularcontext-freecontext-sensitiverecursiver.e.
unionyesyesyesyesyes
intersectionyesnoyesyesyes
complementyesnoyesyesno
concatenationyesyesyesyesyes
staryesyesyesyesyes
reversalyesyesyesyesyes
homomorphismyesyesnonoyes

Complement is the revealing column: the context-sensitive languages are closed under complement (Immerman and Szelepcsényi, a nontrivial theorem), while the recursively enumerable languages are not; a language is recursive exactly when it and its complement are both recursively enumerable (decidable and recognisable languages).

Worked examples

EX 1Type of a grammar

Easy

S→aS∣bS \to aS \mid b: right-linear, Type 3. S→ABS \to AB, A→aA∣aA \to aA \mid a, B→bB∣bB \to bB \mid b: single variable on the left, Type 2 (not regular because S→ABS \to AB has two variables on the right). AB→BAAB \to BA: the left side has two symbols and nothing shrinks, Type 1. aA→baA \to b (left side of length 22, right side of length 11) shrinks: Type 0. A→εA \to \varepsilon alone, with a single variable on the left, is Type 2.

EX 2Machine for a language

Easy

{anbncn}\{a^n b^n c^n\}: Type 1; recognised by a linear bounded automaton (and by a Turing machine), not by any PDA. {anbn}\{a^n b^n\}: PDA. {a∗b∗}\{a^* b^*\}: DFA. The halting problem: a Turing machine only.

EX 3Derivation in the context-sensitive grammar

Medium

For aabbccaabbcc: S⇒aSBC⇒aaBCBCS \Rightarrow aSBC \Rightarrow aaBCBC (using S→aBCS \to aBC) ⇒aaBBCC\Rightarrow aaBBCC (using CB→BCCB \to BC) ⇒aabBCC\Rightarrow aabBCC (aB→abaB \to ab) ⇒aabbCC\Rightarrow aabbCC (bB→bbbB \to bb) ⇒aabbcC\Rightarrow aabbcC (bC→bcbC \to bc) ⇒aabbcc\Rightarrow aabbcc (cC→cccC \to cc). Seven steps. No sentential form is longer than the final string, so the search of all forms up to length 66 finds it.

EX 4Mixed linear grammar

Medium

S→aAS \to aA, A→Sb∣bA \to Sb \mid b has Type 2: the first production is right-linear and the second is left-linear, but all productions of one kind are required for Type 3. Its language {anbn:n≥1}\{a^n b^n : n \ge 1\} is not regular, so no regular grammar exists for it at all; a regular grammar for a regular language must use one orientation throughout (and a language like a∗ba^* b has both a right-linear and a left-linear grammar).

EX 5Typical questions

GATE level

(a) "Which of the following is true: Type 3 ⊂\subset Type 2 ⊂\subset Type 1 ⊂\subset Type 0." True, all inclusions strict. (b) "The class of languages accepted by linear bounded automata is:" context-sensitive. (c) "Which language class is not closed under complement:" context-free and recursively enumerable. (d) "A grammar with productions S→aSbS \to aSb, S→εS \to \varepsilon is of type:" 22. (e) "Which type of grammar is needed for {ww}\{ww\} / {anbncn}\{a^n b^n c^n\}:" context-sensitive (Type 1). (f) "Is every context-sensitive language decidable?" Yes; not every decidable language is context-sensitive.

Cheat box

Cheat box

  • Type 3 regular (FA), Type 2 context-free (PDA), Type 1 context-sensitive (LBA), Type 0 unrestricted (TM, r.e.). Each inside the next, all strictly.
  • Type 3: all productions right-linear or all left-linear. Type 2: one variable on the left. Type 1: ∣α∣≤∣β∣\lvert\alpha\rvert \le \lvert\beta\rvert. Type 0: anything.
  • Witnesses: anbna^nb^n (2 not 3), anbncna^nb^nc^n (1 not 2), halting problem (0 not 1).
  • Context-sensitive languages are decidable; recursive languages are between Type 1 and Type 0.
  • Complement: closed for regular, CSL, recursive; not for CFL, r.e.

Traps and GATE patterns

Connections

The top of the hierarchy is explained by Turing machines and by decidability (Turing machines, decidable and recognisable languages); the deterministic subclass sits between regular and context-free (DPDA versus NPDA); the regular and context-free closure tables are on the pages for those classes.

PYQs

11 previous year questions from your book. I re-solved each one myself (5 by running or simulating, 6 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2005 · MCQ · 2 marksQ79 · page 211 of the book
Consider the regular grammar:
S → Xa | Ya
X → Za
Z → Sa | ε
Y → Wa
W → Sa
where S is the starting symbol, the set of terminals is
{a} and the set of non-terminals is {S, W, X, Y, Z}. We
wish to construct a deterministic finite automaton
(DFA) to recognize the same language. What is the
minimum number of states required for the DFA?
  • (a)2
  • (b)3
  • (c)4
  • (d)5 THEORY OF COMPUTATION
year not read yetQ14 · page 227 of the book
Consider the following languages:
L1 = { anwan | w ∈ {a, b}* }
L2 = { wxwr | w, x ∈ {a, b}*, |w|, |x| > 0 }
Note that wr is the reversal of the string w. Which of
the following is/are TRUE?
  • (a)L1 and L2 are regular.
  • (b)L1 and L2 are context-free.
  • (c)L1 is regular and L2 is context-free.
  • (d)L1 and L2 are context-free but not regular.
GATE 2017 · Set 2 · MCQ · 2 marksQ24 · page 228 of the book
Consider the following languages:
L1 = {ap│p is a prime number}
L2 = {anbmc2m | n ≥ 0, m ≥ 0}
L3 = {anbnc2n │ n ≥ 0}
L4 = {anbn │ n ≥ 1}
Which of the following are CORRECT?
I. L1 is context-free but not regular.
II. L2 is not context-free.
III. L3 is not context-free but recursive.
IV. L4 is deterministic context-free.
  • (a)I, II and IV only
  • (b)II and III only
  • (c)I and IV only
  • (d)III and IV only
year not read yetQ25 · page 228 of the book
Language L1 is defined by the grammar:
S1 → aS1b|ε
Language L2 is defined by the grammar:
S2 → abS2|ε
Consider the following statements:
P: L1 is regular
Q: L2 is regular
Which one of the following is TRUE?
  • (a)Both P and Q are true
  • (b)P is true and Q is false
  • (c)P is false and Q is true
  • (d)Both P and Q are false
GATE 2011 · MCQ · 2 marksQ30 · page 229 of the book
Consider the languages L1,L2 and L3 as given below:
L1 = { 0p 1q | p, q ∈ N },
L2 = { 0p 1q | p, q ∈ N and p = q } and
L3 = { 0p 1q 0r | p, q, r ∈ N and p = q = r }.
Which of the following statements is NOT TRUE?
  • (a)Push Down Automate (PDA) can be used to recognize L1 and L2
  • (b)L1 is a regular language
  • (c)All the three languages are context free
  • (d)Turing machines can be used to recognize all the languages
year not read yetQ36 · page 230 of the book
Match the following:

E. Checking that
identifiers are
declared before
their use
P. L={ anbmcndm |n≥1,m≥1}
F. Number of formal
parameters in the
declaration of a
function agrees with
the number of actual
parameters in a use of
that function
Q. X→XbX | XcX | dXf | g
G. Arithmetic
expressions with
matched pairs of
parentheses
R. L={wcw|w∈ (a+b)* }
H. Palindromes
S. X→bXb | cXc | ε
  • (a)E-P, F-R, G-Q, H-S.
  • (b)E-R, F-P, G-S, H-Q
  • (c)E-R, F-P, G-Q, H-S
  • (d)E-P, F-R, G-S, H-Q
GATE 2006 · MCQ · 2 marksQ44 · page 231 of the book
For s ∈ (0+1)* let d(s) denote the decimal value of s (e.g.
d(101)=5 ). Let L={s∈(0+1)* |d(s) mod 5=2 and d(s)
mod 7≠4}
Which one of the following statements is true?
  • (a)is recursively enumerable, but not recursive
  • (b)is recursive, but not context-free
  • (c)is context-free, but not regular
  • (d)is regular
GATE 2005 · MCQQ48 · page 231 of the book
Consider the languages:
L1 = {anbncm | n, m > 0} and L2 = {anbmcm | n, m > 0},
Which one of the following statements is FALSE?
  • (a)L1 ∩ L2 is a context-free language
  • (b)L1 U L2 is a context-free language
  • (c)L1 and L2 are context-free languages
  • (d)L1 ∩ L2 is a context sensitive language THEORY OF COMPUTATION
GATE 2005 · MCQ · 1 markQ50 · page 232 of the book
The language {0n1n2n | 1<=n<=106} is
  • (a)Regular
  • (b)context-free but not regular
  • (c)context-free but its complement is not context-free
  • (d)not context free
GATE 2009 · MCQ · 2 marksQ20 · page 243 of the book
Let L= L1 ∩ L2 , where L1 and L2 are languages as
defined below:
L1={ambmcnbn | m,n >=0}
L2={aibjck | i,j,k >=0}
Then is L
  • (a)Not recursive
  • (b)Regular
  • (c)Context free but not regular
  • (d)Recursively enumerable but not context free
year not read yetQ22 · page 244 of the book
Which of the following is true for the language
{ ap| p is prime} ?
  • (a)It is not accepted by a Turing Machine
  • (b)It is regular but not context-free
  • (c)It is context-free but not regular
  • (d)It is neither regular nor context-free, but accepted by a Turing machine

Self-check

Question 1

Name the machine and a witness language for each strict inclusion in the hierarchy.

Show answer

Finite automaton / PDA / LBA / Turing machine; witnesses anbna^n b^n, anbncna^n b^n c^n, and the halting language.

Question 2

What is the type of S→aSb∣ABS \to aSb \mid AB, AB→εAB \to \varepsilon?

Show answer

Type 0 (AB→εAB \to \varepsilon shrinks and has two symbols on the left), although its language {anbn}\{a^n b^n\} is context-free.

Question 3

Why is membership decidable for a context-sensitive grammar?

Show answer

No production shrinks the string, so a derivation of a string of length nn only uses forms of length at most nn; there are finitely many, and they can be searched.

Question 4

Is the class of context-sensitive languages closed under complement?

Show answer

Yes (Immerman–Szelepcsényi theorem, quoted). The context-free and recursively enumerable classes are not.

Revision card

One-screen revision

  • 3 regular FA; 2 context-free PDA; 1 context-sensitive LBA; 0 r.e. TM. Strict chain.
  • anbna^nb^n (2 not 3), anbncna^nb^nc^n (1 not 2).
  • Type from the shape of productions, not the language.
  • CSL decidable; complement closed for CSL, not for CFL or r.e.

References

  • ·Sipser, Introduction to the Theory of Computation (3rd edition), Chapters 1 to 3 (finite automata, context-free grammars, Turing machines), which together give the hierarchy; linear bounded automata are treated in the exercises and notes
  • ·Hopcroft, Motwani, Ullman, Introduction to Automata Theory, Languages, and Computation (3rd edition), Chapter 9 (undecidability) and the discussion of the Chomsky hierarchy with unrestricted grammars

Verification

scripts/enum-checks/toc_turing.py: the classifier of the shape of productions (Type 3, 2, 1, 0) was checked on ten grammars; the context-sensitive grammar S -> aSBC | aBC, CB -> BC, aB -> ab, bB -> bb, bC -> bc, cC -> cc generates exactly {a^n b^n c^n : n >= 1} on all strings up to length 12 by an exact breadth-first search (no production shrinks the form, so the search is exhaustive) and decides membership for every string over {a, b, c} up to length 9; the mixed linear grammar S -> aA, A -> Sb | b is Type 2 and generates a^n b^n; S -> aSb | AB, AB -> epsilon is Type 0 and generates a^n b^n for n = 0..3; right-linear and left-linear grammars generate a*b and b a*. That a^n b^n is not regular and a^n b^n c^n is not context-free was checked in the earlier pumping-lemma pages. The closure and decidability tables, the equivalence of non-contracting grammars with context-sensitive grammars, of Type 1 with linear bounded automata and of Type 0 with Turing machines, and the existence of a decidable language that is not context-sensitive are textbook theorems, QUOTED and NOT machine-proved (needs verification). Textbook chapter references are from memory; the treatment of linear bounded automata and of the hierarchy differs between books. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (state counts, derivations, parse-tree and move counts, decomposition counts); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never