Context-free grammars
A grammar specifies how to build strings. Separate its production structure from the complexity of its language: a complicated CFG can still describe a regular language.
4.1 Productions, derivations and language extraction
Past papers · CS 2014–2026 (23 papers): 8 questions, 11 marks
A CFG is , where is a finite variable set disjoint from terminal alphabet , is a finite production set and is the start variable. Every production has one variable on its left: with . The language is . The star permits zero or more derivation steps.
Read recursive rules as paired or independent contributions. couples the two counts. , permits independent counts. A variable occurrence is expanded independently of other occurrences of the same variable.
| Grammar pattern | Language or invariant |
|---|---|
| , fixed terminal string | |
| , | |
| , | |
| Even-length palindromes over | |
| All palindromes over | |
| , | , |
| Balanced parentheses if opens and closes | |
| All words with equal counts of |
To prove an exact language, prove both directions: productions preserve the claimed restriction, and every word satisfying it has a derivation. A few generated examples prove neither direction in full.
4.2 Grammar invariants and production counts
Past papers · CS 2014–2026 (23 papers): 3 questions, 6 marks
Assign each production a terminal-count contribution and a variable-count change. Sum contributions over a complete derivation. This gives necessary relations without enumerating strings; use a constructive argument if sufficiency is needed.
4.3 Ambiguity
Past papers · CS 2014–2026 (23 papers): 2 questions, 2 marks
A CFG is ambiguous if some terminal word has two different parse trees, equivalently two distinct leftmost derivations or two distinct rightmost derivations. Different orders of expanding unrelated variables in one parse tree do not establish ambiguity.
A language is inherently ambiguous if every CFG for it is ambiguous. One ambiguous grammar does not imply inherent ambiguity: is ambiguous for , which also has the unambiguous grammar . The language is a standard inherently ambiguous CFL; overlap of two grammar branches alone is not a proof of inherent ambiguity.
Expression grammars can encode precedence and associativity by separating expression, term and factor variables. Detailed parsing methods, including LL(), belong to the Compiler Design handbook.
4.4 Simplification and normal forms
Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks
A nullable variable derives . Compute nullable variables to a fixed point. When eliminating rules, add versions of each production with nullable occurrences omitted; retain a new start-to- rule if the language contains . A unit production is ; take the transitive unit closure and transfer non-unit productions.
A generating variable derives a terminal string. First remove nongenerating symbols and rules, then remove unreachable symbols from the start. Reversing those two passes may leave newly unreachable symbols. Recheck usefulness after other transformations.
For CNF conversion, introduce a fresh start, eliminate empty/unit productions and useless symbols, replace terminals in longer right-hand sides by new variables, and split right-hand sides longer than two into binary chains. CNF does not mean the grammar is unambiguous.
4.5 CFG membership and finite tests
Past papers · CS 2014–2026 (23 papers): 2 questions, 3 marks
For membership, convert to CNF and use the CYK dynamic programme. It terminates even if a direct grammar search would loop through recursive productions. Test separately by nullability.
CFG emptiness is decidable: compute generating variables and test the start. Finiteness is decidable after removing useless symbols and converting to CNF: a cycle in the variable-dependency graph yields unbounded growth, since every binary rule's other child produces at least one terminal. Without that cleanup, a cycle alone need not imply an infinite language.
CFG universality, equivalence, inclusion, ambiguity and disjointness of two CFG languages are undecidable. These are problems about arbitrary input grammars; particular instances can still be easy. The finite-state analogue may be decidable even when the CFG problem is not.
Last-minute recall
- Every CFG production has one variable on the left.
- Repeated variable occurrences expand independently.
- Use terminal and variable balances to find invariants.
- Ambiguity requires two trees or two derivations of the same fixed order.
- CNF gives steps only for nonempty words.
- Nullable and unit closures are fixed-point computations.
- Membership, emptiness and finiteness are decidable for CFGs.
- Universality and equivalence are undecidable for arbitrary CFGs.