🌲 Context-free grammar
A context-free grammar is a formal system of rewriting rules in which each rule replaces a single nonterminal symbol with a string of symbols, regardless of surrounding context. It defines a language as the set of terminal strings derivable from a designated start symbol.
What it is
A context-free grammar (CFG) describes how strings are assembled from nested parts. It has two kinds of symbols:
- Nonterminals stand for categories such as "noun phrase".
- Terminals are the actual words or characters.
Every rule rewrites one nonterminal, and the rule can apply wherever that nonterminal occurs. The neighbouring symbols play no role, which is what "context-free" means. In the Chomsky hierarchy, CFGs sit above regular grammars and below context-sensitive grammars. The languages they generate are exactly those recognised by pushdown automata.
Here is a small grammar for English fragments:
- S → NP VP
- NP → Det N | NP PP
- VP → V NP | VP PP
- PP → P NP
- Det → the; N → man | dog | telescope; V → saw; P → with
A derivation starts at S and repeatedly rewrites a nonterminal: S ⇒ NP VP ⇒ Det N VP ⇒ the N VP ⇒ the man VP ⇒ … ⇒ the man saw the dog.
The same derivation can be drawn as a parse tree:
- The root is S.
- Each internal node is a nonterminal whose children are the right-hand side of the rule applied.
- The leaves, read left to right, form the sentence (its yield).
Different rewriting orders, such as leftmost and rightmost, can correspond to one tree. The tree is therefore the real unit of structure.
A grammar is ambiguous if some string has more than one parse tree. With the rules above, "the man saw the dog with the telescope" has two trees:
- In one, the PP attaches to the VP (the seeing used a telescope).
- In the other, it attaches to the NP (the dog had the telescope).
Ambiguity is primarily a property of grammars. A language is called inherently ambiguous when every grammar for it is ambiguous.
The key idea
G = (N, Σ, R, S), rules A → α, A ∈ N, α ∈ (N ∪ Σ)*
P(T) = ∏ p(A → α) over all rule uses in tree T
The symbols in the first line mean:
- N is a finite set of nonterminals.
- Σ is a finite set of terminals, disjoint from N.
- R is a finite set of rules.
- S ∈ N is the start symbol.
In a rule A → α, the left side A is one nonterminal, and α is any finite sequence of terminals and nonterminals. The star denotes all such sequences, including the empty one. The language L(G) is the set of strings over Σ alone that can be derived from S.
The second line covers probabilistic CFGs (PCFGs). Each rule gets a probability p, and the probabilities of all rules sharing a left side sum to one. The probability of a tree T is the product of the probabilities of the rules used to build it. A sentence's probability is the sum over all of its trees.
Context-freeness lets parsing decompose into independent subproblems over spans. Any CFG can be converted to a weakly equivalent grammar in Chomsky normal form (CNF). Weak equivalence means the language is kept but the trees generally are not. In CNF, each rule either rewrites a nonterminal as exactly two nonterminals (A → B C) or as a single terminal (A → a). The one exception is a possible rule letting S produce the empty string, allowed only if S occurs on no right-hand side.
On CNF grammars, the CYK algorithm fills a triangular chart. Cell (i, j) collects every nonterminal that can yield words i through j. It is filled by combining pairs of adjacent smaller cells under binary rules, which takes time cubic in sentence length. Two variants follow from changing what each cell stores:
- Storing "summed probability" instead of "which nonterminals" gives the Inside algorithm.
- Storing "best probability" gives Viterbi parsing.
The context-free assumption also has a probabilistic cost. A PCFG treats each subtree as independent of everything outside it once the subtree's root label is fixed. A node's label is the only channel between a subtree and its context, so the labeling scheme fixes the independence assumptions of a treebank-estimated PCFG. Plain treebank labels carry too little context (Johnson 1998).
History and founding papers
Noam Chomsky introduced phrase-structure grammars in 1956 and 1957 and formalised the hierarchy of grammar types (Types 0–3) in 1959. Around 1959–1960, Backus and Naur adopted an equivalent notation (BNF) to specify ALGOL. This made CFGs the standard tool for programming-language syntax.
Parsing algorithms followed:
- Kasami (1965), Younger (1967) and Cocke, working largely independently (Cocke's version appeared with Schwartz in 1970), gave the cubic chart recognition algorithm now called CYK.
- Earley (1970) gave a general parser that needs no normal form.
- Valiant (1975) tied CFG recognition to Boolean matrix multiplication.
- Lee later showed the link runs in both directions: any substantially faster CFG parser would yield fast Boolean matrix multiplication (Lee 2001).
The probabilistic line has three classic problems:
- string probability via the Inside algorithm;
- the best parse via Viterbi;
- rule estimation via Inside-Outside.
Both the Inside computation and Inside-Outside estimation go back to Baker (1979). Jelinek and Lafferty added a fourth problem: the probability that a sentence begins with a given prefix, which speech recognisers need for next-word prediction (Jelinek & Lafferty 1991). Their Left-to-Right Inside algorithm works on CNF grammars. It obtains left-corner quantities in closed form as Q_L = P_L[I − P_L]⁻¹. For a fixed grammar it runs in O(k³) for a k-word prefix, about twice the Inside algorithm's cost (Jelinek & Lafferty 1991). Stolcke's probabilistic Earley parser computes prefix probabilities incrementally, word by word, for grammars not in normal form (Stolcke 1995).
Theoretical work then settled when PCFGs are well-defined. Careless rule probabilities can leak mass to infinite derivations; a grammar that loses no mass is called tight (or consistent). Take A → AA | a and write p for P(A → AA). Finite trees then receive total mass min(1, 1/p − 1), so the grammar is tight only when p ≤ ½ (Chi & Geman 1998; Chi 1999).
Chi and Geman proved that relative-frequency estimates from parsed corpora are always tight. They also proved that EM iterates are tight when null and unit rules are absent (Chi & Geman 1998). Chi extended this result into a general weighting method. He also showed that a Gibbs model whose only features are rule counts defines the same distributions as a PCFG (Chi 1999).
How it is used today
In compilers and data formats, CFGs remain the specification language for syntax. They are usually restricted to deterministic subclasses such as LL(k), LR(k) and LALR, which can be parsed in linear time. Parsing expression grammars are a related but different formalism, and their relation to CFGs has been studied formally (Mascarenhas et al. 2013).
In natural-language processing, treebanks have long been read as large PCFGs. The baseline Penn Treebank grammar has 14,962 productions. Of these, 1,327 can never appear in a most-probable parse, because combinations of shorter rules always beat them on probability. Annotating each node with its parent's category improved the average of precision and recall by roughly 8% (Johnson 1998).
The BitPar parser copes with the massive ambiguity of such treebank grammars. It handled a grammar of 65,855 rules and 4,444 categories by storing the CYK chart as bit vectors (Schmid 2004). Lexicalised extensions motivated specialised algorithms for bilexical grammars (Eisner & Satta 1999).
Modern constituency parsers still output context-free trees, but neural networks now do the scoring. Chart-based span parsers with self-attentive encoders are decoded with a CYK variant. They reached 95.13 F1 on the WSJ test set with ELMo (Kitaev & Klein 2018), and 95.8 F1 with a BERT ensemble (Kitaev et al. 2019).
Transition-based alternatives give up dynamic programming for speed:
- One shift-reduce parser reached 91.3% F-score on English (Zhu et al. 2013).
- A span-based transition system needs exactly 4n−2 steps for an n-word sentence and scored 91.3 F1 on WSJ section 23 (Cross & Huang 2016).
- An in-order traversal system scored 91.8 F1 greedily on WSJ (Liu & Zhang 2017).
Sequence-labeling encodings reduce trees to one tag per word. The best of them average about 92 F1 over nine treebanks: the English Penn Treebank and eight SPMRL languages (Swedish, Polish, Korean, Hungarian, Hebrew, German, French and Basque) (Roca et al. 2026).
Large language models can be prompted to emit trees. In a zero-shot setting, prompts included error-annotated example parses, and extra agents checked each tree's validity and faithfulness. This approach reached 76.87 F1, reported as the best end-to-end unsupervised result. It is not comparable to the supervised scores above, and LLM output is often malformed (Bai et al. 2023).
Constituent trees also feed downstream tasks such as discourse parsing (Chen et al. 2015). The prefix probabilities of PCFGs support the surprisal theory of human sentence processing. That theory predicts garden-path effects and the extra difficulty of object relatives (Hale 2001). Synchronous CFGs extend the formalism to pairs of languages for machine translation. Unlike monolingual CFGs, their rules cannot always be binarized (Huang et al. 2009).
Open questions
Several limits and open problems remain:
- Expressive adequacy for natural language. Cross-serial dependencies in Swiss German (Shieber 1985) show that some human languages are not context-free. Mildly context-sensitive formalisms such as TAG and CCG were developed to cover such patterns. Discontinuous constituents also strain context-free trees (Coavoux 2020). Richer frameworks such as HPSG add feature structures on top of phrase structure (Müller et al. 2021).
- Unsupervised learning. Inducing a grammar from raw text remains hard. Active approaches include depth-bounded PCFGs inspired by memory limits (Jin et al. 2018) and compound PCFGs with neural parameterisations (Kim et al. 2019).
- Parsing complexity. Valiant's reduction already gives subcubic recognition in theory. Because fast CFG parsing would also yield fast Boolean matrix multiplication, practical subcubic parsing would require practical fast Boolean matrix multiplication (Lee 2001). Prefix-probability methods also struggle when very many nonterminals make the matrix inversion costly (Jelinek & Lafferty 1991).
- Neural networks and the hierarchy. It is unclear whether, and how, neural architectures generalise on context-free languages. This question is being probed with tasks organised by the Chomsky hierarchy (Delétang et al. 2022; Someya et al. 2024).
Key papers
Related concepts
- Dependency parsing — Dependency parsing is the task of recovering a sentence's syntactic structure as directed, labelled links between its words. Each word is…
- Construction grammar — Construction grammar is a family of linguistic theories holding that knowledge of a language consists of constructions. Constructions are…
- Semantic parsing — Semantic parsing is the task of mapping a natural-language utterance to a formal, machine-interpretable representation of its meaning, such…
- Universal Dependencies — Universal Dependencies is a framework and an open collection of treebanks that annotates the grammar of many languages with one shared…
More papers
Topics: 📐 Syntax & grammar formalisms · 🗂️ Treebanks
Shelves: 🗣️ Linguistics & languages
This page in Markdown → https://md.research.elamur.ai/free/en/concepts/context-free-grammar/