There is (at least) one way to prove unambiguity of a grammar G=(N,T,δ,S)$G=(N,T,\delta ,S)$ for language L$L$. It consists of two steps:

- Prove L⊆L(G)$L\subseteq \mathcal{L}(G)$.
- Prove [zn]SG(z)=|Ln|$[{z}^{n}]{S}_{G}(z)=|{L}_{n}|$.

The first step is pretty clear: show that the grammar generates (at least) the words you want, that is correctness.

The second step shows that G$G$ has as many syntax trees for words of length n$n$ as L$L$ has words of length n$n$ -- with 1. this implies unambiguity. It uses the structure function of G$G$ which goes back to Chomsky and Schützenberger [1], namely

SG(z)=∑n=0∞tnzn$\phantom{\rule{2em}{0ex}}{\displaystyle {S}_{G}(z)=\sum _{n=0}^{\mathrm{\infty}}{t}_{n}{z}^{n}}$

with tn=[zn]SG(z)${t}_{n}=[{z}^{n}]{S}_{G}(z)$ the number of syntax trees G$G$ has for words of length n$n$. Of course you need to have |Ln|$|{L}_{n}|$ for this to work.

The nice thing is that SG${S}_{G}$ is (usually) easy to obtain for context-free languages, although finding a closed form for tn${t}_{n}$ can be difficult. Transform G$G$ into an equation system of functions with one variable per nonterminal:

⎡⎣A(z)=∑(A,a0…ak)∈δ ∏i=0k τ(ai) :A∈N⎤⎦ with τ(a)={a(z)z,a∈N,a∈T.$\phantom{\rule{2em}{0ex}}{\displaystyle [A(z)=\sum _{(A,{a}_{0}\dots {a}_{k})\in \delta}\text{}\prod _{i=0}^{k}\text{}\tau ({a}_{i})\text{}:A\in N]\text{with}\tau (a)=\{\begin{array}{ll}a(z)& ,a\in N\\ z& ,a\in T\end{array}.}$

This may look daunting but is really only a syntactical transformation as will become clear in the example. The idea is that generated terminal symbols are counted in the exponent of z$z$ and because the system has the same form as G$G$, zn${z}^{n}$ occurs as often in the sum as n$n$ terminals can be generated by G$G$. Check Kuich [2] for details.

Solving this equation system (computer algebra!) yields S(z)=SG(z)$S(z)={S}_{G}(z)$; now you "only" have to pull the coefficient (in closed, general form). The TCS Cheat Sheet and computer algebra can often do so.

### Example

Consider the simple grammar G$G$ with rules

S→aSa∣bSb∣ε$\phantom{\rule{2em}{0ex}}{\displaystyle S\to aSa\mid bSb\mid \epsilon}$.

It is clear that L(G)={wwR∣w∈{a,b}∗}$\mathcal{L}(G)=\{w{w}^{R}\mid w\in \{a,b{\}}^{\ast}\}$ (step 1, proof by induction). There are 2n2${2}^{\frac{n}{2}}$ palindromes of length n$n$ if n$n$ is even, 0$0$ otherwise.

Setting up the equation system yields

S(z)=2z2S(z)+1$\phantom{\rule{2em}{0ex}}{\displaystyle S(z)=2{z}^{2}S(z)+1}$

whose solution is

SG(z)=11−2z2$\phantom{\rule{2em}{0ex}}{\displaystyle {S}_{G}(z)=\frac{1}{1-2{z}^{2}}}$.

The coefficients of SG${S}_{G}$ coincide with the numbers of palindromes, so G$G$ is unambiguous.

- The Algebraic Theory of Context-Free Languages by Chomsky, Schützenberger (1963)
- On the entropy of context-free languages by Kuich (1970)