The Timpson Lyceum seal

The Timpson Lyceum · Computation Theory

Lecture 8 (Computation Theory) Pumping Lemma, Right Linear Grammars, and Context-Free Grammars

Overview

This lecture closes out the theory of regular languages and opens the study of grammars. It begins by reviewing the pumping lemma — the standard tool for proving that a language is not regular — restating the pumping property and the contradiction argument behind it. It then introduces right linear grammars, a rule-based way of generating strings that turns out to be logically equivalent to DFAs, NFAs, and regular expressions. Along the way it fixes the vocabulary of grammars (variables, terminals, rules, yields, derives, language of a grammar) and surveys which yes/no questions about regular languages are decidable. Finally it moves up one level to context-free grammars (CFGs), introducing parse trees, ambiguity, and why precedence-preserving grammars matter for compilers.

Key Concepts

Regular language. A language AA is regular if some DFA recognizes it. DFAs, NFAs, and regular expressions are all logically equivalent, so any of them can describe exactly the regular languages.

The pumping lemma (pumping property). Every regular language has the pumping property, so contrapositively, lacking it proves non-regularity. Formally: for every regular language AA there is a number pp (the pumping length, typically the number of states in a recognizing DFA) such that every string s∈As \in A with ∣s∣≥p|s| \ge p can be split as s=xyzs = xyz with

The intuition: a DFA with pp states reading a string of length ≥p\ge p must revisit a state, so the string contains a loop. The looped portion yy can be repeated ("pumped up") or removed ("pumped down") any number of times and the result still lands in an accept state.

Negation (how it is used). To show AA is not regular, show the property fails: for any pp an adversary names, you can exhibit a string of length >p> p such that for every legal split (∣xy∣≤p|xy| \le p, ∣y∣≥1|y| \ge 1) there is some ii with xyiz∉Axy^{i}z \notin A.

Right linear grammar (a regular grammar). A 4-tuple G=(V,Σ,R,S)G = (V, \Sigma, R, S) where VV is a finite set of variables (capital letters), Σ\Sigma is a finite set of terminals (everything else), V∩Σ=∅V \cap \Sigma = \varnothing, RR is a finite set of rules, and S∈VS \in V is the start variable. Every rule has one of three forms:

with A,C∈VA, C \in V and b∈Σb \in \Sigma. The variable always sits at the right end of the string, so generation proceeds left to right. A left linear grammar simply puts the variable on the left; both are regular grammars and are logically equivalent. Right linear grammars are logically equivalent to DFAs/NFAs: the rules mirror a DFA's transitions (RR plays the role of δ\delta) and A→εA \to \varepsilon marks accept states.

Grammar vocabulary (all grammars). For strings u,v,wu, v, w of terminals and variables:

Decidable questions about regular languages. All of the following can always be answered yes/no for DFAs/NFAs/regex/linear grammars (in contrast to Turing machines, where they become undecidable):

Context-free grammar (CFG). Same 4-tuple G=(V,Σ,R,S)G = (V, \Sigma, R, S), but rules are relaxed to A→αA \to \alpha where the left side is a single variable and the right side α\alpha is any string of variables and terminals. "Context-free" means a variable may be replaced regardless of its surrounding symbols. (A context-sensitive grammar allows more than one symbol on the left, e.g. AB→…AB \to \ldots, so replacement depends on context.) Right linear grammars are a subset of CFGs, and CFGs are strictly more powerful than DFAs — e.g. A→0A1∣εA \to 0A1 \mid \varepsilon generates {0n1n:n≥0}\{0^{n}1^{n} : n \ge 0\}, a non-regular language.

Parse trees. A derivation can be drawn as a tree whose root is SS, whose internal nodes are variables, and whose leaves (read left to right) spell the generated string. A well-chosen arithmetic grammar makes the tree encode operator precedence: forcing multiplication to bind before addition.

Ambiguity. A string is derived ambiguously if it has two or more distinct parse trees. A grammar is ambiguous if it generates any string ambiguously. Compilers avoid this by using restricted, unambiguous CFGs (the LR(k) grammars). Deciding whether an arbitrary CFG is ambiguous is undecidable in general (though decidable for particular cases and for LR(k) grammars) — like how there is no general solution formula for degree-5 polynomials even though specific ones are solvable.

Worked Example

Prove A={0n1n:n≥0}A = \{0^{n}1^{n} : n \ge 0\} is not regular (pumping lemma, by contradiction).

Rule first, then the work: assume AA is regular with pumping length pp, choose a witness string longer than pp, then show every legal split can be pumped out of AA.

s=0p1p,∣s∣=2p≥p(witness)s=xyz,∣xy∣≤p, ∣y∣≥1  ⇒  y lies entirely in the 0’s, so y=0k, 1≤k≤pxy2z=0 p+k 1 p(pump up: i=2)since p+k>p, the count of 0’s≠count of 1’s  ⇒  xy2z∉A\begin{aligned} s &= 0^{p}1^{p}, \qquad |s| = 2p \ge p \quad(\text{witness}) \\ s = xyz,\quad &|xy| \le p,\ |y| \ge 1 \;\Rightarrow\; y \text{ lies entirely in the } 0\text{'s, so } y = 0^{k},\ 1 \le k \le p \\ xy^{2}z &= 0^{\,p+k}\,1^{\,p} \quad(\text{pump up: } i = 2) \\ &\text{since } p + k > p,\ \text{the count of } 0\text{'s} \neq \text{count of } 1\text{'s} \;\Rightarrow\; xy^{2}z \notin A \end{aligned}

Every admissible split yields a string outside AA, contradicting the pumping property. Therefore no such pp exists and AA is not regular. ■\blacksquare

Summary