Context-free languages

Definition (context-free grammar, language) open_in_new

A grammar $G = (Σ, N, P, S)$ is context-free if every production rule $α\to β\in P$ has $α\in N$. That is, every production rule is either of type 2 or $A\to ε$ for $A\in N$.

A language $L \subseteq Σ^\ast$ is context-free if it is produced by some context-free grammar.

Definition (Chomsky normal form) open_in_new

A grammar $G = (Σ, N, P, S)$ is said to be in Chomsky normal form if all of its rewrite rules are either of the form $A \to AB$ (for $A,B,C \in N$) or of the form $A \to a$ (for $A\in N$ and $a\in Σ$).

$G$ is context-free.

Lemma (derivation length in Chomsky normal form) open_in_new

If $G = (Σ, N, P, S)$ is a grammar in Chomsky normal form and $w\in \mathcal{L}(G)$ with $|w| = n$, then any $G$-derivation of $w$ has length $2n-1$.

Proof

Lemma (removing eps and unit productions from context-free grammar) open_in_new

Let $G = (Σ, N, P, S)$ be a context-free grammar.

There exists a context-free grammar $\mathring G = (Σ, N, \mathring P, S)$ where $\mathring P$ has no $ε$ production (i.e. rules of the form $A\to ε$) and unit production (i.e. rules of the form $A\to B$) and $\mathcal{L}(\mathring G) = \mathcal{L}(G) \setminus \set{ε}$.

Proof

Lemma (equivalent to variable-targeted grammar) open_in_new

For every context-free grammar without $ε$-production (i.e. rules of the form $A\to ε$), there exists an equivalent context-free variable-targeted grammar, i.e. where all rewrite rules are either $A\to a$ or $A \to B_1 B_2 \ldots B_k$ (where $A,B_1, \ldots, B_k\in N$ and $a\in Σ$).

Proof

Theorem (equivalent to Chomsky normal form) open_in_new

For any context-free grammar $G$, there exists a grammar $\bar G$ in Chomsky normal form such that $\mathcal{L}(\bar G) = \mathcal{L}(G) \setminus \set{ε}$.

Proof

Definition (finitely branching tree) open_in_new

A subset $T\subseteq \mathbb{N}$$^\ast$ is said to be a finitely branching tree if

  • $T$ is closed under initial segments, i.e. if $t\in T$ and $s\subseteq t$, then $s\in T$; and

  • for each $t\in T$, there exists some $n\in \N$ such that $tk\in T$ iff $k<n$.


  • We say $t$ has $n$ successors or is $n$-branching.

  • An element $t\in T$ is a leaf or terminal node if it has no successors.

  • $ε$ is contained in every non-empty tree, and is called the root.

  • The $k$-th level of $T$ is all elements of length $k$.

  • The height of $T$ is the maximal $k\in \N$ such that $T$ has an element on the $k$-th level. This exists if $T$ is a finite tree.

  • For $t\in T$ on the $k$-th level, the corresponding branch through $T$ is the sequence $\sb{t|_m}{m\leq k}$, which is a sequence of nodes of $T$ of length $k+1$.

Define the left-to-right order $<$ as $$ \begin{align*} s<t\iff s\neq t \text{ and if $k$ is least such that $s(k)\neq t(k)$, then $s(k) < t(k)$}. \end{align*} $$

  • This defines a partial order on $T$.

  • $<$ is a total order on the set of nodes on the same level.

  • $<$ is a total order on the set of leaves of $T$.

Definition (parse tree) open_in_new

Let $G = (Σ, V, P, S)$ be a context-free grammar, and $A\in V$ be a variable.

A pair $\mathbf{T} = (T,l)$ is a $G$-parse tree starting from $A$ if $T$ is a finite finitely branching tree and $l: T \to Ω$ satisfying

  • $l(ε) = A$;

  • if $l(t) \in Σ$, then $t$ is a leaf in $T$; and

  • if $l(t) \in V$ and $t$ is $n+1$-branching ($n\in \N$, i.e. $t$ is not a leaf), then there is a rule $l(t) \to x_0 x_1 \ldots x_n \in P$ such that $l(tk) = x_k$ for all $k=0,1,\ldots,n$.

Write $σ_\mathbf{T} = l(t_0) \ldots l(t_m)$ for the string parsed by $\mathbf{T}$, where $l_0 < t_1 < \ldots < t_m$ are the leaves of $\mathbf{T}$ totally-ordered by the left-to-right order.

For $t\in T$ not a leaf, define the subtree at $t$ by $\mathbf{T}_t = (T_t, l_t)$ where $T_t = \sb{s\in \N^\ast}{ts\in T}$ and $l_t(s) = l(ts)$.

  • $\mathbf{T}_t$ is a $G$-parse tree starting from $l(t).$

  • Under the left-to-right ordering, the leaves of $\mathbf{T}_t$ are a consecutive sequence of the leaves of $\mathbf{T}$. Therefore $σ_\mathbf{T} = u σ_{\mathbf{T}_t} v$, where $u\in \mathbb{W}$ corresponds to all leaves of $\mathbf{T}$ to the left of leaves of $\mathbf{T}_t$, and $w\in \mathbb{W}$ corresponds to all leaves of $\mathbf{T}$ to the right of leaves of $\mathbf{T}_t$.

If $\mathbf{T}$ and $\mathbf{T}'$ are $G$-parse trees, $t\in T$ with $l(t) = A$ and $\mathbf{T}'$ starts from $A$, then we can graft $\mathbf{T}'$ into $\mathbf{T}$: define $\mathrm{graft}(\mathbf{T}, t, \mathbf{T}') = (T^\star, l^\star)$ where $T^\star = \sb{s\in T}{t\not\subseteq s} \cup \sb{ts}{s\in T'}$ and $$ \begin{align*} l^\star(s) = \begin{cases} l(s), &\text{if } t\not\subseteq s, \\ l'(u), &\text{if } s = tu \text{ for some $u\in T'$}. \end{cases} \end{align*} $$

  • Grafting corresponds to removing the subword $σ_{\mathbf{T}_t}$ from $σ_{\mathbf{T}}$ and replacing it with $σ_{\mathbf{T}'}$.

Proposition (equivalence of context-free grammar and parse tree) open_in_new

Let $G = (Σ, V, P, S)$ be a context-free grammar.

$w\in \mathcal{L}(G)$ iff there exists a $G$-parse tree $\mathbf{T}$ starting from $S$ such that $w = σ_\v T$.

Proof

Lemma (length of word parsed by parse tree) open_in_new

If $G$ is a grammar in Chomsky normal form, $\mathbf{T}$ is a $G$-parse tree of height $h+1$, and $σ_\mathbf{T} = w\in Σ^\ast$, then $|w|\leq 2^h$.

Proof

Theorem (pumping lemma for context-free languages) open_in_new

For any context-free language $L\subseteq Σ^\ast$, there exists $n\in \N$ such that for all $u\in L$, if $|u|\geq n$, then there are subwords $v,w,x,y,z\in Σ^\ast$ such that

  1. $u = vwxyz$;

  2. $w$ and $y$ are not both $ε$;

  3. $|wxy|\leq n$;

  4. $vw^k xy^k z\in L$ for all $k=0,1,2,\ldots$.

Proof