# Chomsky hierarchy

The Chomsky hierarchy in the fields of formal language theory, computer science, and linguistics, is a containment hierarchy of classes of formal grammars. A formal grammar describes how to form strings from a language's vocabulary (or alphabet) that are valid according to the language's syntax. The linguist Noam Chomsky theorized that four different classes of formal grammars existed that could generate increasingly complex languages. Each class can also completely generate the language of all inferior classes (set inclusive).

## History

The general idea of a hierarchy of grammars was first described by Noam Chomsky in "Three models for the description of language" during the formalization of transformational-generative grammar (TGG). Marcel-Paul Schützenberger also played a role in the development of the theory of formal languages; the paper "The algebraic theory of context free languages" describes the modern hierarchy, including context-free grammars.
Independently, alongside linguists, mathematicians were developing models of computation (via automata). Parsing a sentence in a language is similar to computation, and the grammars described by Chomsky proved to both resemble and be equivalent in computational power to various machine models.

## The hierarchy

The following table summarizes each of Chomsky's four types of grammars, the class of language it generates, the type of automaton that recognizes it, and the form its rules must have. The classes are defined by the constraints on the productions rules.
| Grammar | Languages | Recognizing automaton | Production rules (constraints) | Examples |
| Type-3 | Regular | Finite-state automaton | $A\rightarrow {\text{a}}$ $A\rightarrow {\text{a}}B$ (right regular) or $A\rightarrow {\text{a}}$ $A\rightarrow B{\text{a}}$ (left regular) | $L=\{a^{n}\mid n>0\}$ |
| Type-2 | Context-free | Non-deterministic pushdown automaton | $A\rightarrow \alpha$ | $L=\{a^{n}b^{n}\mid n>0\}$ |
| Type-1 | Context-sensitive | Linear-bounded non-deterministic Turing machine | $\alpha A\beta \rightarrow \alpha \gamma \beta$ | $L=\{a^{n}b^{n}c^{n}\mid n>0\}$ |
| Type-0 | Recursively enumerable | Turing machine | $\gamma \rightarrow \alpha$ ($\gamma$ non-empty) | $L=\{w\mid w$ describes a terminating Turing machine $\$ |
* ↑ Meaning of symbols: ${\text{a}}$ = terminal $A$, $B$ = non-terminal $\alpha$, $\beta$, $\gamma$ = string of terminals and/or non-terminals
Note that the set of grammars corresponding to recursive languages is not a member of this hierarchy; these would be properly between Type-0 and Type-1.
Every regular language is context-free, every context-free language is context-sensitive, every context-sensitive language is recursive and every recursive language is recursively enumerable. These are all proper inclusions, meaning that there exist recursively enumerable languages that are not context-sensitive, context-sensitive languages that are not context-free and context-free languages that are not regular.

### Regular (Type-3) grammars

Type-3 grammars generate the regular languages. Such a grammar restricts its rules to a single nonterminal on the left-hand side and a right-hand side consisting of a single terminal, possibly followed by a single nonterminal, in which case the grammar is right regular. Alternatively, all the rules can have their right-hand sides consist of a single terminal, possibly preceded by a single nonterminal (left regular). These generate the same languages. However, if left-regular rules and right-regular rules are combined, the language need no longer be regular. The rule $S\rightarrow \varepsilon$ is also allowed here if $S$ does not appear on the right side of any rule. These languages are exactly all languages that can be decided by a finite-state automaton. Additionally, this family of formal languages can be obtained by regular expressions. Regular languages are commonly used to define search patterns and the lexical structure of programming languages.
For example, the regular language $L=\{a^{n}\mid n>0\}$ is generated by the Type-3 grammar $G=(\{S\},\{a,b\},P,S)$ with the productions $P$ being the following.
    S → aS
    S → a

### Context-free (Type-2) grammars

Type-2 grammars generate the context-free languages. These are defined by rules of the form $A\rightarrow \alpha$ with $A$ being a nonterminal and $\alpha$ being a string of terminals and/or nonterminals. These languages are exactly all languages that can be recognized by a non-deterministic pushdown automaton. Context-free languages—or rather its subset of deterministic context-free languages—are the theoretical basis for the phrase structure of most programming languages, though their syntax also includes context-sensitive name resolution due to declarations and scope. Often a subset of grammars is used to make parsing easier, such as by an LL parser.
For example, the context-free language $L=\{a^{n}b^{n}\mid n>0\}$ is generated by the Type-2 grammar $G=(\{S\},\{a,b\},P,S)$ with the productions $P$ being the following.
    S → aSb
    S → ab
The language is context-free but not regular (by the pumping lemma for regular languages).

### Context-sensitive (Type-1) grammars

Type-1 grammars generate context-sensitive languages. These grammars have rules of the form $\alpha A\beta \rightarrow \alpha \gamma \beta$ with $A$ a nonterminal and $\alpha$, $\beta$ and $\gamma$ strings of terminals and/or nonterminals. The strings $\alpha$ and $\beta$ may be empty, but $\gamma$ must be nonempty. The rule $S\rightarrow \epsilon$ is allowed if $S$ does not appear on the right side of any rule. The languages described by these grammars are exactly all languages that can be recognized by a linear bounded automaton (a nondeterministic Turing machine whose tape is bounded by a constant times the length of the input.)
For example, the context-sensitive language $L=\{a^{n}b^{n}c^{n}\mid n>0\}$ is generated by the Type-1 grammar $G=(\{S,A,B,C,W,Z\},\{a,b,c\},P,S)$ with the productions $P$ being the following.
    S → aBC
    S → aSBC
    CB → CZ
    CZ → WZ
    WZ → WC
    WC → BC
    aB → ab
    bB → bb
    bC → bc
    cC → cc
The language is context-sensitive but not context-free (by the pumping lemma for context-free languages). A proof that this grammar generates $L=\{a^{n}b^{n}c^{n}\mid n>0\}$ is sketched in the article on Context-sensitive grammars.

### Recursively enumerable (Type-0) grammars

Type-0 grammars include all formal grammars. There are no constraints on the productions rules. They generate exactly all languages that can be recognized by a Turing machine, thus any language that is possible to be generated can be generated by a Type-0 grammar. These languages are also known as the recursively enumerable or Turing-recognizable languages. Note that this is different from the recursive languages, which can be decided by an always-halting Turing machine.