The Timpson Lyceum · Computation Theory
Lecture 9 (Computation Theory) Context-Free Grammars and Pushdown Automata
Overview
This lecture moves us up to the second level of computation: the context-free level. After reviewing the definition of a context-free grammar (CFG) and building grammars by hand, we learn Chomsky normal form (CNF) — a restricted but logically equivalent form that every CFG can be converted into — and walk through the conversion procedure. We then prove several closure properties of context-free languages (closed under union and concatenation, but not under intersection or complement). Finally we introduce the machine model that matches CFGs: the pushdown automaton (PDA), an NFA augmented with a stack, and give its formal six-tuple definition.
Key Concepts
Context-free grammar (CFG). A four-tuple where:
- is a finite set of variables (capital letters by convention).
- is a finite set of terminals (lowercase letters / symbols); .
- is a finite set of production rules, each of the form where and is any string of variables and terminals.
- is the start variable.
The defining feature: the left side of every rule is a single variable — it stands alone, with no surrounding context.
CFG vs. regular grammar. Regular grammars restrict the right side to a terminal followed by at most one variable, so only one variable ever appears as a string is generated. CFGs lift that restriction: the right side may mix any number of terminals and variables. This is what lets CFGs generate non-regular languages such as .
Generating a string. Always start from and repeatedly apply rules, replacing a variable with the right side of one of its rules. When no variables remain, the string is generated. Use the shorthand for multiple rules on the same variable. Include if the empty string belongs to the language.
Chomsky normal form (CNF). A CFG is in CNF if every rule has one of these forms:
- (a variable to exactly two variables), where are not the start variable;
- (a variable to a single terminal);
- (only the start variable may go to the empty string).
CNF grammars are a subset of CFGs (every CNF grammar is a CFG, not vice versa), but they generate exactly the same class of languages. They are valued because their rigid shape makes proofs about them easy. Any CFG can be converted to an equivalent CNF grammar — analogous to converting an NFA to a DFA.
Conversion to CNF — five steps, in order:
- New start variable. Add so the start variable never appears on a right side.
- Remove -rules. Working bottom-up, delete each (except for the start variable) and patch every rule that used so the grammar still generates the same strings. If the language contains , a single survives.
- Remove unit rules. For any (variable to single variable), replace it with (everything could produce).
- Replace non-isolated terminals. Any terminal appearing alongside other symbols is swapped for a new variable , and the rule is added.
- Break up long right sides. Any rule whose right side has more than two variables is split by introducing new variables for pairs until only two remain per rule.
Closure properties of context-free languages:
- Union — closed. Combine the two grammars' rules and add .
- Concatenation — closed. Combine the rules and add .
- Intersection — NOT closed. Shown by counterexample.
- Complement — NOT closed. Follows from intersection via De Morgan: . Since CFLs are closed under union, if they were also closed under complement they would be closed under intersection — but they are not.
Definition of a context-free language (CFL). A language is context-free iff some CFG generates it. (Parallel to a regular language being defined as one a DFA recognizes.)
A canonical non-context-free language. is not context-free (taken on faith here; proved later with a pumping lemma). It is the witness used to show intersection is not closed: , an intersection of two CFLs that is not a CFL. A stack can match/count one pair of adjacent things, not two independent counts.
Pushdown automaton (PDA). An NFA equipped with a stack — a last-in/first-out store where you can only access the top symbol (the cafeteria plate-dispenser analogy). A special bottom marker \text{\}$ is pushed first so the machine can detect an empty stack. On each transition the machine may read an input symbol (or nothing), pop a stack symbol (or nothing), and push a stack symbol (or nothing). PDAs are non-deterministic, like NFAs.
Formal definition. A PDA is a six-tuple :
- — finite set of states; — input alphabet; — stack alphabet (need not equal ).
- — given a state, the symbol read, and the symbol popped, returns a set of (next state, symbol pushed) pairs. The power set captures non-determinism.
- — start state; — set of accept states.
The notation means . The only difference from the NFA definition is the added stack alphabet and the stack-aware transition function. (A deterministic PDA drops the power set, giving one outcome per input — but we work almost exclusively with the non-deterministic version.) The transition-design power of PDAs is exactly the power of a single stack: an NFA (no stack) recognizes regular languages, a PDA (one stack) recognizes context-free languages, and adding a second stack yields the power of a Turing machine.
Worked Example
Build a CFG and a PDA for , then trace a derivation.
A grammar for :
Generating the string — start from and apply three times, then :
Matching PDA (the stack counts the zeros):
- From the start state, on read / pop nothing, push \text{\}$ (mark the bottom of the stack).
- Loop: on reading , pop nothing, push — one stack symbol per zero.
- On , move to the popping state (guess the boundary between s and s).
- Loop: on reading , pop , push nothing — one zero removed per one.
- On , pop \text{\}$ → accept.
Trace on (a string it should reject): push \text{\}0000100\varepsilon\text{$}0\text{$}$ precisely when the input runs out.
Summary
- A context-free grammar is a four-tuple whose rules have a single variable on the left and any string of variables/terminals on the right; CFGs generate the context-free languages.
- To design a grammar, assign each variable a meaning (e.g. "still need one more 1") and write rules that preserve that meaning — as with the same-number-of-0s-and-1s language.
- Chomsky normal form restricts rules to , , or ; every CFG converts to an equivalent CNF grammar via the five-step procedure (new start, remove -rules, remove unit rules, isolate terminals, break long right sides).
- Context-free languages are closed under union and concatenation (add or ) but not under intersection or complement — and the complement result follows from the intersection result by De Morgan's law.
- A pushdown automaton is an NFA plus a stack, formally a six-tuple ; the stack lets it match or count a single quantity, giving it exactly the recognizing power of CFGs.
- Computation hierarchy: no stack → regular languages; one stack → context-free languages; two stacks → Turing-machine power.