On this page
A grammar is how a compiler knows that 2 + 3 × 4 means 14, and how it checks that every bracket in your code is closed. Here is the idea built from one two-line rule.
What is a context-free grammar?
According to the Wikipedia article on context-free grammars, "the formalism of context-free grammars was developed in the mid-1950s by Noam Chomsky". The paper usually cited for it is his 1956 "Three models for the description of language".
A grammar has variables (such as S), terminals (the letters of the finished string), rules and a start variable. It is "context-free" because each rule has a single variable on its left side. That means you can rewrite the variable wherever it appears.
Take S → aSb | ε, where ε is the empty string. To get aaabbb:
S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb
Each use of S → aSb adds one a on the left and one b on the right, and S → ε stops. So the grammar generates exactly the strings aⁿbⁿ.
How do derivations and parse trees work?
A derivation is the list of steps above; a parse tree draws them. The start variable is the root, each rule turns a node into its children, and the leaves read from left to right spell the string.
Balanced parentheses are what the same article calls "the canonical example of a context-free grammar": S → SS | (S) | (). It derives (()()) like this:
S ⇒ (S) ⇒ (SS) ⇒ (()S) ⇒ (()())
Counting the balanced strings with n pairs of brackets gives the Catalan numbers. Our post on polygon triangulations and the Catalan numbers explains where they come from.
Why is 2 + 3 × 4 ambiguous?
Try E → E + E | E × E | (E) | n, where n is any number. The string 2 + 3 × 4 has two parse trees:
The root uses E → E + E, and the right-hand E becomes 3 × 4. That gives 2 + 12 = 14.
The root uses E → E × E, and the left-hand E becomes 2 + 3. That gives 5 × 4 = 20.
A grammar is ambiguous if some string has two different parse trees. According to the Wikipedia article on ambiguous grammars, "the decision problem of whether an arbitrary grammar is ambiguous is undecidable". That is a cousin of the result in our post on why the halting problem is undecidable.
For arithmetic, you can fix the ambiguity by giving each precedence level its own variable:
E → E + T | T
T → T × F | F
F → (E) | n
Now × can only be built inside a T, below any +. So 2 + 3 × 4 has one tree, and it gives 14.
What is Chomsky normal form?
The Wikipedia article on Chomsky normal form gives the definition: every rule has the form A → BC or A → a, and only the start variable may have S → ε. Neither B nor C may be the start variable. Chomsky first described it in his 1959 paper "On certain formal properties of grammars"90362-6). Every context-free grammar can be converted into an equivalent grammar in this form, a step the CYK parsing algorithm relies on.
Here is S → aSb | ε converted by hand:
S0 → AX | AB | ε
S → AX | AB
X → SB
A → a
B → b
Check it: AB gives ab, and AX ⇒ aSB wraps one more a…b pair around whatever S makes. Every rule either splits one variable into two or produces one letter. So a nonempty string of length n takes n − 1 splits plus n letters, which is 2n − 1 steps.
How do context-free grammars connect to pushdown automata?
A pushdown automaton is a finite-state machine with a stack. Lecture notes from Western Washington University's CSCI 301 state the theorem: a language is context-free if and only if some nondeterministic pushdown automaton accepts it.
For aⁿbⁿ, push a token for each a and pop one for each b. Reject an a after a b, or a pop from an empty stack. Accept if the stack ends empty. Parentheses work the same way: push on (, pop on ). Our post on stack sorting and pattern avoidance puts a stack to another use.
A four-step check for any grammar you write
Write three short strings the grammar must produce and three it must reject.
Derive each string it must produce and draw the parse tree.
For short strings with two operators, look for a second tree. If you find one, the grammar is ambiguous.
Fix the ambiguity by layering: one variable per precedence level, with the tighter operator lower down.
If a step stalls, the Fermi Concept Library has a lesson for every concept in mathematics and computing. To practise, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer.



