A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Context-Free Grammars

College Depth 84 in the knowledge graph ☐ I know this ☆ Set as goal
171topics build on this
470prerequisites beneath it
See this on the map →
Pushdown AutomataRecursive Definitions on Finite Sets+2 more→→Neural Language Models and Transformers
formal-languages grammars context-free-languages parsing

Core Idea

A context-free grammar (CFG) generates strings through production rules that replace a single nonterminal with a string of terminals and nonterminals, regardless of surrounding context. The language of all derivable strings is a context-free language (CFL), and the class of CFLs is exactly the class recognized by nondeterministic pushdown automata. Every CFG can be converted to Chomsky normal form, where each production is either A -> BC or A -> a, enabling the CYK parsing algorithm. The pumping lemma for context-free languages proves that certain languages (e.g., {an bn cn}) are not context-free.

How It's Best Learned

Write grammars for familiar languages — arithmetic expressions, matched parentheses, palindromes — and draw derivation (parse) trees. Then convert a grammar to Chomsky normal form step by step, and apply the CFL pumping lemma to prove {an bn cn} is not context-free. This builds intuition for where the stack-based model breaks down.

Common Misconceptions

Explainer

You already understand pushdown automata — machines that recognize strings by reading input and consulting a stack. A context-free grammar (CFG) is the generative counterpart: instead of recognizing, it produces. A grammar has a finite set of nonterminals (placeholder symbols, written in uppercase), a set of terminals (the actual alphabet of the language), a set of production rules of the form A → α, and a designated start symbol. A derivation begins with the start symbol and repeatedly replaces one nonterminal with the right-hand side of some production rule, until only terminals remain. The "context-free" in the name means the replacement is unconditional — you may replace A regardless of what surrounds it.

The connection to your prerequisite is precise: the class of languages generated by CFGs is exactly the class recognized by nondeterministic pushdown automata. This equivalence is proved by construction in both directions — you can convert any CFG into an equivalent PDA, and any PDA into an equivalent CFG. As a concrete example, consider the language of balanced parentheses. The grammar S → SS | (S) | ε generates it cleanly, and the corresponding PDA simply pushes on '(' and pops on ')'. For arithmetic expressions, a grammar can encode operator precedence and associativity through nesting of nonterminals, which is exactly how real parsers work.

Chomsky Normal Form (CNF) is a restricted grammar form where every production is either A → BC (two nonterminals) or A → a (one terminal). Every CFG can be converted to CNF through a sequence of transformations: eliminate ε-productions, eliminate unit productions (A → B), and break long right-hand sides into binary ones by introducing new nonterminals. CNF is not a restriction on the languages — it is purely a structural normalization that enables the CYK algorithm, a dynamic programming parser that decides in O(n³) time whether a string of length n is in the language. You build a table over all substrings and fill it bottom-up using the binary structure of CNF.

The pumping lemma for context-free languages is the tool for proving a language is not context-free. It states that for any CFL, long enough strings can be written as uvwxy such that pumping v and x together (uvi wxi y) stays in the language. The proof uses the fact that long derivations must repeat a nonterminal (a pigeonhole argument on parse trees), and pumping corresponds to repeating a subtree. The classic application: {an bn cn} cannot be pumped because pumping any two substrings cannot simultaneously fix all three counts. This language requires a machine that can count three things independently — something a single stack cannot do. This is where the CFL class ends and the context-sensitive languages begin.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Literal Equations → Slope-Intercept Form → Point-Slope Form → Writing Linear Equations → Parallel and Perpendicular Line Slopes → Graphing Linear Equations → Piecewise Functions → Step Functions → Composition of Functions → Inverse Functions → Radical Functions and Graphs → Rational Exponents → Exponential Functions and Graphs → Logarithms Introduction → Big-O Notation and Asymptotic Analysis → Breadth-First Search (BFS) → Shortest Paths in Unweighted Graphs → Dijkstra's Shortest Path Algorithm → Algorithm Analysis and Big-O Notation → Turing Machines → Deterministic Finite Automata → Nondeterministic Finite Automata → Pushdown Automata → Context-Free Grammars

Longest path: 85 steps · 470 total prerequisite topics

Prerequisites (4)

Leads To (1)