Context-Free Languages
3.1 Context-Free Grammars
A context-free grammar (CFG) is a 4-tuple where:
- is a finite set of variables (non-terminals).
- is a finite set of terminals.
- is a finite set of production rules.
- is the start variable.
A production means variable can be replaced by string .
Example. CFG for :
Example. CFG for balanced parentheses:
Example. CFG for (palindromes):
Example. CFG for :
The variable generates the part, and generates the part. Since The total number Of bS and cS equals the total number of aS.
Example. CFG for strings with equal numbers of 0S and 1S:
The first two rules add a matched pair (in either order); the third concatenates two balanced Strings; the fourth handles the empty string.
Example. CFG for the language of all strings over that are not palindromes:
Here generates non-palindromes and generates arbitrary strings of length .
Worked Example: CFG for $\{a^i b^j : 2i = j \mathrm{ or} 2j = i\}$
generates and generates . Their union is the desired Language.
Proof of correctness for . By induction: . So . Similarly . Hence Which is exactly the target language.
3.2 Parse Trees and Ambiguity
A parse tree (derivation tree) for a string according to grammar is a tree where:
- The root is labelled .
- Internal nodes are labelled with variables.
- Leaves are labelled with terminals (or ).
- Children of a node labelled are the symbols of the right-hand side of a production in left-to-right order.
A CFG is ambiguous if some string in its language has two or more distinct parse trees (equivalently, Two or more leftmost derivations or two or more rightmost derivations).
Example (ambiguous). . The string has two parse trees: and .
Removing ambiguity. Some ambiguous grammars can be made unambiguous by rewriting the productions To enforce a particular evaluation order. For the arithmetic expression grammar:
This grammar is unambiguous and enforces the standard precedence ( before ) and left Associativity.
Worked Example: Proving a grammar is unambiguous
Consider the grammar: . We prove it is unambiguous.
Proof. Any string in has the form for some . We show by induction On that has exactly one parse tree.
Base (): has only the parse tree using .
Inductive step: must use (the rule produces only Strings of length 2). The inner must derive Which by IH has a unique parse tree. Hence has exactly one parse tree.
Example. is inherently ambiguous. (Proof omitted; Any grammar must have two competing mechanisms for the two conditions, and these Interfere.)
3.3 Chomsky Normal Form
A CFG is in Chomsky Normal Form (CNF) if every production is of the form:
- (two variables), or
- (single terminal), or
- (only the start variable, and does not appear on the right of any rule).
Theorem 3.1. Every CFG has an equivalent CFG in Chomsky Normal Form.
Proof (conversion algorithm).
- Add a new start variable with rule (ensures does not appear on any right side if it generates ).
- Remove all -productions (except possibly ). For each occurrence of in a right-hand side, add a variant without .
- Remove all unit productions . For each chain Add for each .
- Convert remaining rules: replace terminals in right-hand sides of length with new variables and rules . Replace right-hand sides of length by introducing intermediate variables.
Worked Example: Converting a CFG to CNF
Convert the following grammar to CNF:
Step 1: Add new start variable. .
Step 2: Remove -productions. Both and are nullable. For each production Containing or Add variants with nullable symbols removed:
Step 3: Remove unit productions. is a unit production. Replace with all -productions:
Step 4: Convert long right-hand sides and terminals.
Introduce and :
Handle (length 3): introduce Then and .
Final CNF grammar:
3.4 Pushdown Automata
A pushdown automaton (PDA) is a 6-tuple where:
- is a finite set of states.
- is the input alphabet.
- is the stack alphabet.
- is the transition function.
- is the start state.
- is the set of accepting states.
A PDA can read input, push symbols onto the stack, pop symbols from the stack, and make -moves (without consuming input or changing the stack).
Example. PDA for :
- Push a marker \text{\}$ onto the stack.
- For each
aPushXonto the stack. - For each
bPopXfrom the stack. - Accept if the stack is empty (just the marker) and all input is consumed.
Formally: states (start), (pushing), (popping), (accept).
Worked Example: PDA for palindromes $\{ww^R : w \in \{0,1\}^*\}$
Design a PDA for .
Idea: Push the first half of the input onto the stack. Nondeterministically guess when the Middle is reached, then pop and compare with the remaining input.
States: (pushing), (popping/comparing), (accept).
Transitions:
Push phase ():
— push
0.— push
1.— guess the midpoint.
Pop phase ():
— match
0.— match
1.— accept if stack empty and input consumed.
Accept: .
Correctness. If The PDA pushes Guesses the midpoint, then pops and matches . If No sequence of guesses leads to acceptance.
3.5 Equivalence of CFG and PDA
Theorem 3.2. A language is context-free if and only if some PDA recognises it.
Proof (sketch).
CFG to PDA: The PDA simulates a leftmost derivation. It maintains the current sentential form On the stack. At each step, it pops a variable from the stack and pushes the right-hand side of a Production for that variable.
PDA to CFG: Given PDA Construct a grammar whose variables encode pairs of states Meaning “the PDA can go from state to state Popping everything pushed onto the stack.” The productions simulate the PDA”s transitions.
Theorem 3.2a (CFG to PDA construction). Let be a CFG. Then there exists a PDA with .
Proof. Construct M = (\{q_0, q_1, q_2\}, \Sigma, V \cup \Sigma \cup \{\}, \delta, q_0, {q_2})$:
- (q_0, \varepsilon, \varepsilon) \to (q_1, S\)$ — push the start variable and bottom marker.
- For each : — replace a variable with its production.
- For each : — match a terminal on input with stack.
- (q_1, \varepsilon, \) \to (q_2, $)$ — accept when only the marker remains.
The PDA maintains the current sentential form (minus terminals already matched) on the stack. When only \text{\}\blacksquare$
3.6 Pumping Lemma for Context-Free Languages
Theorem 3.3. If is context-free, there exists such that for every with , can be decomposed as satisfying:
- .
- .
- for all .
Proof. Let be a CFG in CNF with variables. Any parse tree of height generates a string Of length at most . Set . For The parse tree has height So some path repeats a variable. The substring generated between the two occurrences can be pumped.
Example. is not context-free.
Proof. Assume pumping length . Let . Since The substring cannot span all three letter types. Case analysis:
- If is within the
aS orbS orcS: pumping changes only one count, breaking the equality. - If spans
aS andbS: pumping changes theaandbcounts but notcBreaking equality. - If spans
bS andcS: analogous.
In all cases, .
Example. is not context-free.
Proof. Assume pumping length . Let . Since :
- If lies entirely within one block: pumping up () increases only one count, but the gap between adjacent counts is only 1 or 2, so doubling the pumped count violates the strict inequalities.
- If spans two blocks: pumping changes two adjacent counts by the same additive amount. The gap between those counts is 1, so increasing both by the same positive amount makes them equal, violating .
In all cases, .
Worked Example: $\{a^n b^n a^n : n \geq 0\}$ is not context-free
Proof. Assume pumping length . Let . Since The substring cannot span all three blocks. Case analysis:
- within the first block: pumping down () reduces the first count only. for .
- within the block: pumping changes the count only, breaking equality.
- within the last block: analogous.
- spans the first and : pumping changes first and counts but not the last count.
- spans and last : analogous.
In all cases, pumping produces a string not in .
3.7 The CYK Parsing Algorithm
The Cocke—Younger—Kasami (CYK) algorithm determines membership in a context-free language When the grammar is in Chomsky Normal Form.
Theorem 3.4. Given a CFG in CNF and a string of length The CYK algorithm decides Whether in time.
Algorithm. Construct a table for Where is the set Of variables that can derive the substring .
- Base case (): T[i, i] = \{A : A \to w_i \mathrm{ is a rule in G\}.
- Recursive case (): For each split with :
- Answer: iff .
Proof of correctness. In CNF, every derivation of a string of length involves exactly binary productions. The table considers every possible “root” of the derivation tree For each substring, and every possible split of that substring into two parts. By induction on the Substring length, contains exactly those variables that derive .
Time complexity. The table has entries. Each entry considers at most split points And checks all rules, giving total time.
Worked Example: CYK on a small grammar
Grammar (CNF): , , , .
String: .
Length 1:
- : So (since ).
- : So (since and ).
Length 2:
- : split at . Check all pairs :
- : ? No. ? No. ? No. ? No.
- : ? Yes — add .
- : already checked. So .
Since The string is in . The parse tree is Where and .