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 moreNeural 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 ZeroThe Number ZeroCounting to FiveCounting to 10Counting to 20Counting a Set of Objects Up to 20Cardinality: The Last Number CountedMatching Numerals to QuantitiesSubitizing Small QuantitiesAddition Within 10Number Bonds to 10Addition Within 20Doubles and Near DoublesDoubles Facts Within 10Near Doubles Facts Within 20Mental Math Strategies for AdditionMental Math: Adding and Subtracting TensAddition Within 100Repeated Addition as MultiplicationMultiplication as Equal GroupsMultiplication: ArraysBasic Multiplication Facts (0s, 1s, 2s, 5s, 10s)Multiplication Facts Within 100Division as Equal SharingDivision as Grouping (Measurement Division)Division: Grouping (Repeated Subtraction) ModelDivision: Fair Sharing ModelDivision as Equal SharingDivision as GroupingBasic Division FactsDivision Facts Within 100Multiplication and Division Fact FamiliesRelationship Between Multiplication and DivisionDivision Facts as Inverse of MultiplicationRemainders and Quotients in DivisionDivision Word ProblemsMulti-Step Word ProblemsSolving Multi-Step Word ProblemsMultiplication Word ProblemsDivision Word ProblemsIntroduction to Long DivisionFactors and MultiplesPrime and Composite NumbersEquivalent FractionsRelating Fractions and DecimalsDecimal Place ValueIntegers and the Number LineComparing and Ordering IntegersAbsolute ValueAdding IntegersSubtracting IntegersMultiplying IntegersDividing IntegersUnit RatesProportionsPercent ConceptConverting Between Fractions, Decimals, and PercentsOperations with Rational NumbersTwo-Step EquationsSolving Multi-Step EquationsEquations with Variables on Both SidesLiteral EquationsSlope-Intercept FormPoint-Slope FormWriting Linear EquationsParallel and Perpendicular Line SlopesGraphing Linear EquationsPiecewise FunctionsStep FunctionsComposition of FunctionsInverse FunctionsRadical Functions and GraphsRational ExponentsExponential Functions and GraphsLogarithms IntroductionBig-O Notation and Asymptotic AnalysisBreadth-First Search (BFS)Shortest Paths in Unweighted GraphsDijkstra's Shortest Path AlgorithmAlgorithm Analysis and Big-O NotationTuring MachinesDeterministic Finite AutomataNondeterministic Finite AutomataPushdown AutomataContext-Free Grammars

Longest path: 85 steps · 470 total prerequisite topics

Prerequisites (4)

Leads To (1)