# Parikh's theorem

Parikh's theorem in theoretical computer science says that if one looks only at the number of occurrences of each terminal symbol in a context-free language, without regard to their order, then the language is indistinguishable from a regular language. It is useful for deciding that strings with a given number of terminals are not accepted by a context-free grammar. It was first proved by Rohit Parikh in 1961 and republished in 1966.

## Definitions and formal statement

Let $\Sigma =\{a_{1},a_{2},\ldots ,a_{k}\}$ be an alphabet. The Parikh vector of a word is defined as the function p:Σ$^∗$→N$^k${\textstyle p:\Sigma ^{*}\to \mathbb {N} ^{k}}, given by $p(w)=(|w|_{a_{1}},|w|_{a_{2}},\ldots ,|w|_{a_{k}})$ where $|w|_{a_{i}}$ denotes the number of occurrences of the symbol $a_{i}$ in the word $w$.
A subset of $\mathbb {N} ^{k}$ is said to be linear if it is of the form $u_{0}+\mathbb {N} u_{1}+\dots +\mathbb {N} u_{m}=\{u_{0}+t_{1}u_{1}+\dots +t_{m}u_{m}\mid t_{1},\ldots ,t_{m}\in \mathbb {N} \}$ for some vectors u$_0$, …, u$_m${\textstyle u_{0}, \ldots , u_{m}}. A subset of $\mathbb {N} ^{k}$ is said to be semi-linear if it is a union of finitely many linear subsets.
Theorem — Let $L$ be a context-free language or a regular language, let $P(L)$ be the set of Parikh vectors of words in $L$, that is, P(L)={p(w)∣w∈L}{\textstyle P(L)=\{p(w)\mid w\in L\}}. Then $P(L)$ is a semi-linear set.
If $S$ is any semi-linear set, then there exists a regular language (which a fortiori is context-free) whose Parikh vectors is $S$.
In short, the image under $p$ of context-free languages and of regular languages is the same, and it is equal to the set of semilinear sets.
Two languages are said to be commutatively equivalent if they have the same set of Parikh vectors. Thus, every context-free language is commutatively equivalent to some regular language.

## Proof

The second part is easy to prove.
Given semi-linear set $S$, to construct a regular language whose set of Parikh vectors is $S$.
$S$ is a union of 0 or more linear sets. Since the empty language is regular, and union of regular languages is regular, it suffices to prove that any linear set is the set of Parikh vectors of a regular language.
Let $S=\{u_{0}+t_{1}u_{1}+\dots +t_{m}u_{m}\mid t_{1},\ldots ,t_{m}\in \mathbb {N} \}$, then it is the set of Parikh vectors of $\{z_{0}\}\cdot (\cup _{i=1}^{m}\{z_{i}\})^{*}$, where each $z_{i}$ has Parikh vector $u_{i}$.
The first part is less easy. The following proof is credited to Goldstine.
First we need a small strengthening of the pumping lemma for context-free languages:
Lemma — If $L$ is generated by a Chomsky normal form grammar, then $\exists N\geq 1$, such that
For any $k\geq 1$, and for any $w\in L$ with $|w|\geq N^{k}$, there exists a way to split $w$ into segments $ux_{1}\cdots x_{k}zy_{k}\cdots y_{1}v$, and a nonterminal symbol $A$, such that
$|x_{i}y_{i}|\geq 1$ for all $i$, and $|x_{1}\cdots x_{k}zy_{k}\cdots y_{1}|\leq N^{k}$
$S\Rightarrow ^{*}uAv\quad A\Rightarrow ^{*}z\quad \forall i,A\Rightarrow ^{*}x_{i}Ay_{i}$
The proof is essentially the same as the standard pumping lemma: use the pigeonhole principle to find $k$ copies of some nonterminal symbol $A$ in the longest path in the shortest derivation tree.
Now we prove the first part of Parikh's theorem, making use of the above lemma.
First, construct a Chomsky normal form grammar for $L$.
For each finite nonempty subset of nonterminals $U$, define $L_{U}$ to be the set of sentences in $L$ such that there exists a derivation that uses every nonterminal in $U$, no more and no less. It is clear that $L=\cup _{U}L_{U}$, so it suffices to prove that each $p(L_{U})$ is a semilinear set.
Now fix some $U$, and let $k=|U|$. We construct two finite sets $F,G$, such that $p(L_{U})=p(F\cdot G^{*})$, which is obviously semilinear.
    For notational clarity, write $\Rightarrow _{U}^{*}$ to mean "there exists a derivation using no more (but possibly less) than nonterminals in $U$. With that, we define $F,G$ as follows:
$F=\{w\in L_{U}:|w|<N^{k}\}$ $G=\{xy:1\leq |xy|\leq N^{k}{\text{ and there exists }}A\in U{\text{ such that }}A\Rightarrow _{U}^{*}xAy\}$
To prove $p(L_{U})\subset p(F\cdot G^{*})$, we induct on the length of $w\in L_{U}$.
    If $|w|<N^{k}$, then $w\in F$, so $p(w)\in p(F\cdot G^{*})$. Otherwise, by the strengthened pumping lemma, there exists a derivation of $w$ using precisely the elements of $U$, and is of the form
$S{\underset {d_{0}}{\stackrel {*}{\Rightarrow }}}uAv{\underset {d_{1}}{\stackrel {*}{\Rightarrow }}}ux_{1}Ay_{1}v{\underset {d_{2}}{\stackrel {*}{\Rightarrow }}}\cdots {\underset {d_{k}}{\stackrel {*}{\Rightarrow }}}ux_{1}\cdots x_{k}Ay_{k}\cdots y_{1}v{\underset {d_{k+1}}{\stackrel {*}{\Rightarrow }}}ux_{1}\cdots x_{k}zy_{k}\cdots y_{1}v$
    where $A\in U$, $1\leq |x_{i}y_{i}|$, and $|x_{1}\cdots x_{k}zy_{k}\cdots y_{1}|\leq N^{k}$.
    Since there are only $k-1$ elements in $U\setminus \{A\}$, but there are $k$ sub-derivations $d_{1},...,d_{k}$ in the middle, we may delete one sub-derivation $d_{i}$, and obtain a shorter $w'$ that is still in $L_{U}$, with
$p(w)=p(uzv)+p(x_{1}y_{1})+\cdots +p(x_{k}y_{k})=p(w')+p(x_{i}y_{i})$
    By induction, $p(w')\in p(F\cdot G^{*})$, and by construction, $x_{i}y_{i}\in G$, so $p(w)\in p(F\cdot G^{*})$.
To prove $p(L_{U})\supset p(F\cdot G^{*})$, consider an element $w\in F\cdot G^{*}$. We need to show that $p(w)\in p(L_{U})$. We induct on the minimal number of factors from $G$ that is needed to identify $w$ as an element of $F\cdot G^{*}$.
    If no factor from $G$ is needed, then $w\in F\subset L_{U}$.
    Otherwise, $w=w'xy$ for some $w'\in F\cdot G^{*}$ and $xy\in G$, where $w'$ requires less factors from $G$ than $w$.
    By induction, $p(w')=p(w'')$ for some $w''\in L_{U}$. By construction of $G$, there exists some $A\in U$ such that $A\Rightarrow _{U}^{*}xAy$.
    By construction of $L_{U}$, the symbol $A$ appears in a derivation of $w''$ using exactly all of $U$. Then we can interpolate $A\Rightarrow _{U}^{*}xAy$ into that derivation to obtain some $w'''\in L_{U}$ such that
$p(w''')=p(w'')+p(xy)=p(w')+p(xy)=p(w)$

## Strengthening for bounded languages

A language $L$ is bounded if $L\subset w_{1}^{*}\ldots w_{k}^{*}$ for some fixed words $w_{1},\ldots ,w_{k}$. Ginsburg and Spanier gave a necessary and sufficient condition, similar to Parikh's theorem, for bounded languages.
Call a linear set stratified, if in its definition for each $i\geq 1$ the vector $u_{i}$ has the property that it has at most two non-zero coordinates, and for each $i,j\geq 1$ if each of the vectors $u_{i},u_{j}$ has two non-zero coordinates, $i_{1}<i_{2}$ and $j_{1}<j_{2}$, respectively, then their order is not $i_{1}<j_{1}<i_{2}<j_{2}$. A semi-linear set is stratified if it is a union of finitely many stratified linear subsets.
Ginsburg-Spanier — A bounded language $L$ is context-free if and only if $\{(n_{1},\ldots ,n_{k})\mid w_{1}^{n_{1}}\ldots w_{k}^{n_{k}}\in L\}$ is a stratified semi-linear set.

## Significance

The theorem has multiple interpretations. It shows that a context-free language over a singleton alphabet must be a regular language and that some context-free languages can only have ambiguous grammars. Such languages are called inherently ambiguous languages. From a formal grammar perspective, this means that some ambiguous context-free grammars cannot be converted to equivalent unambiguous context-free grammars.