Syntactic parsing and probabilistic CFGs
Syntactic parsing finds the grammatical tree for a sentence; probabilistic CFGs (PCFGs) add rule probabilities so we can choose the most likely parse and train grammars from treebanks.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Two or three sentences: where an engineer uses this.
Syntactic parsing is used by engineers when building NLP systems that need sentence structure: e.g., extracting relations, building semantic representations, machine translation or question answering. Probabilistic context-free grammars (PCFGs) let systems prefer more likely parses, handle ambiguity automatically, and can be trained from annotated corpora (treebanks).
Key ideas
Explain from first principles. Define terms clearly.
Grammar (formal): a set of production rules that rewrite nonterminals into terminals and/or other nonterminals. A grammar generates a language (set of sentences).
Context-free grammar (CFG): a grammar where each production has a single nonterminal on the left-hand side, written A -> β, where A is a nonterminal and β is a string of terminals and nonterminals. (Assumption: rewriting at any nonterminal position is allowed independently of context.)
Terminal: a symbol that appears in the final sentence (word token). Define before use.
Nonterminal: a syntactic category symbol (S, NP, VP, N, V, etc.).
Parse tree (constituency tree): a tree showing how the start symbol expands by grammar rules to produce the sentence. A valid parse tree corresponds to a leftmost (or any) derivation under the CFG.
Ambiguity: a sentence is ambiguous if two or more distinct parse trees are allowed by the grammar.
Probabilistic CFG (PCFG): a CFG together with, for each nonterminal A, a probability distribution over its productions. If A -> β_1, A -> β_2, ..., then sum_i P(A -> β_i) = 1. The probability of a complete parse tree is the product of the probabilities of every rule used in that tree (assumption: independent rule choices given the nonterminal). A PCFG thus defines a probability distribution over parses (and hence over derivations and strings by marginalising).
Exact statement: Given a PCFG G with rule probabilities P(r) for each rule r, the probability of a parse tree T that uses rules r_1, r_2, ..., r_k is P(T) = Π_{i=1..k} P(r_i). (Assumption: the PCFG imposes independence of expansion decisions at different nonterminals.)
Maximum a posteriori (MAP) parse / Viterbi parse: for a sentence w, select the parse tree T that maximises P(T | w). For a PCFG under the generative view, P(T | w) is proportional to P(T) (since T generates exactly w), so the MAP parse is the tree with maximum product of rule probabilities.
Conversion to Chomsky Normal Form (CNF): CKY parsing requires CNF: each production is either A -> B C (two nonterminals) or A -> a (single terminal). If grammar is not in CNF, convert by standard steps: remove null-productions, remove unit productions, break long right-hand sides into binary rules using new nonterminals. Keep/update probabilities: when converting a PCFG you must redistribute or re-estimate probabilities carefully (simple conversions can keep probabilities by introducing deterministic rules; for training, retrain on treebank or renormalise). Describe to draw: for CNF conversion, draw original rule A -> B C D and show you introduce X1 so that A -> B X1, X1 -> C D; label new nodes and indicate how probabilities are assigned/renormalised.
CKY algorithm (Cocke–Younger–Kasami): a dynamic-programming algorithm that fills an upper-triangular table for substrings of the sentence. For PCFGs we use the probabilistic (Viterbi) CKY: the cell [i,j] stores the highest probability for each nonterminal generating the substring from word i (inclusive) to j (exclusive). (Assumption: grammar in CNF and sentence tokenised.) Draw: an n-by-n table above the diagonal; index tokens w_0..w_{n-1} on the diagonal; for each span (i,j) list nonterminals and best probabilities and backpointers.
Training PCFGs:
- Supervised (treebank MLE): estimate P(A -> β) = count(A -> β) / count(A) from a corpus of parse trees. This is the maximum-likelihood estimator under i.i.d. tree assumptions.
- Unsupervised (EM / inside–outside): estimate rule probabilities with the inside–outside (Baum–Welch) algorithm by iteratively computing expected counts under the current grammar and re-normalising. (Assumption: sentences are generated independently from the PCFG.)
Numerical issues: probabilities multiply and quickly underflow. In implementation use log-probabilities and replace products by sums of logs; maximisation over sums is stable.
Formulas
Each formula on its own line followed by symbol meanings and units.
P(T) = ∏_{r ∈ T} P(r)
- P(T): probability of parse tree T (dimensionless). Applies when T is a complete derivation under the PCFG. r ranges over production rules used in T.
∑_{β : A->β} P(A -> β) = 1 for each nonterminal A
- P(A -> β): probability of the production A -> β. (dimensionless). Normalisation condition for PCFGs.
P(A -> β) = count(A -> β) / count(A)
- MLE estimate from a treebank. count(A -> β) is the number of times that production is used in the training trees; count(A) is the total number of times nonterminal A appears as a left-hand side. (dimensionless)
π[i,j,A] = max_{A -> B C} max_{i<k<j} ( π[i,k,B] * π[k,j,C] * P(A -> B C) )
- Viterbi (CKY) recurrence: π[i,j,A] is the best probability that nonterminal A generates the span words i..j-1. Indices: i inclusive, j exclusive; k splits the span. Applies when grammar is in CNF.
π[i,i+1,A] = P(A -> w_i)
- Base case: probability of A producing the single terminal w_i directly.
α(A,i,j) = ∑_{A->B C} ∑_{i<k<j} α(B,i,k) * α(C,k,j) * P(A->B C)
- Inside probability for nonterminal A generating span i..j-1 (used in inside–outside/EM).
α(A,i,i+1) = P(A -> w_i) - Base case for inside probabilities (terminals).
P(w_0..w_{n-1}) = α(S,0,n) - Probability of the whole sentence under the grammar.
β(S,0,n) = 1 - Outside base case: start symbol covering the full span.
β(B,i,k) = ∑_{A->B C} ∑_{k<j<=n} β(A,i,j) * P(A -> B C) * α(C,k,j)β(C,k,j) = ∑_{A->B C} ∑_{0<=i<k} β(A,i,j) * P(A -> B C) * α(B,i,k) - Outside probability recurrences (CNF), matching i inclusive, j exclusive indexing.
E[count(A -> B C)] = ∑_{i<k<j} [ β(A,i,j) * P(A -> B C) * α(B,i,k) * α(C,k,j) ] / α(S,0,n)E[count(A -> w_i)] = [ β(A,i,i+1) * P(A -> w_i) ] / α(S,0,n)P(A -> β) ← E[count(A -> β)] / E[count(A)] - Inside–outside EM: expected counts and M-step re-estimation; sums are over all valid spans/splits (and over sentences in a corpus).
Time complexity (CKY) = O(n^3 · |G|)
- n: sentence length (number of tokens). |G|: number of relevant binary productions. Applies to the cubic dynamic-programming CKY.
Note: use log-space in implementations: log π is obtained by sums instead of products.
Worked examples
Two exam-standard model answers (theory / numerical). Show all steps and final answers bold.
Example 1 — computing parse probability (simple PCFG) Question (model): Given this PCFG: S -> NP VP [1.0] NP -> 'she' [0.5] NP -> 'fish' [0.5] VP -> V NP [1.0] V -> 'eats' [1.0] Compute the probability of the parse tree for the sentence "she eats fish" and show steps.
Model answer (step-by-step): The parse tree uses rules: S->NP VP, NP->'she', VP->V NP, V->'eats', NP->'fish'. Multiply their probabilities using the PCFG product rule. P(T) = 1.0 * 0.5 * 1.0 * 1.0 * 0.5 = 0.25. Therefore the probability of the parse tree is 0.25.
Example 2 — ambiguity resolved by PCFG (numeric CKY style computation) Question (model): Consider the grammar (probabilities given for each left-hand side sum to 1): S -> NP VP [1.0] NP -> 'I' [0.3] NP -> Det N [0.5] NP -> NP PP [0.2] VP -> V NP [0.6] VP -> VP PP [0.4] PP -> P NP [1.0] Det -> 'the' [0.7] Det -> 'a' [0.3] N -> 'man' [0.7] N -> 'telescope' [0.3] V -> 'saw' [1.0] P -> 'with' [1.0]
Compute probabilities for the two parses of "I saw the man with a telescope": PP attached to NP vs PP attached to VP; which parse is more probable?
Model answer (showing products): Parse A (PP attaches to NP): rules used and multiplications give final probability 0.0003969. (All multiplications shown in steps — see derivation in body.) Parse B (PP attaches to VP): repeating with appropriate rules gives final probability 0.0007938. Comparison: Parse B (VP-attachment) has probability 0.0007938 > 0.0003969, so PCFG chooses VP-attachment as more likely.
(These numbers come from multiplying the listed rule probabilities for each node in the trees; show the multiplication chain and result in an exam answer.)
Common mistakes
- Multiplying many small probabilities directly (underflow) instead of using log-probabilities.
- Confusing probability of a parse tree P(T) with probability of the string P(w) (the latter sums probabilities over all parses generating w).
- Using MLE counts from an unrepresentative treebank without smoothing (sparse/unseen productions problems).
- Treating PCFG independence too strictly: PCFG ignores lexical and sibling context unless grammar augmented (leading to poor disambiguation for natural languages). Note this assumption explicitly in answers.
For your exam
Kinds of questions and what full-marks answers include:
- Definition questions: define CFG, PCFG, parse tree, CKY, CNF. Full-marks answer: concise, formal definition and any normalization condition (for PCFG state the sum-to-one property per LHS).
- Short notes: e.g., "Probabilistic CKY" — include CNF requirement, Viterbi recurrence, complexity, and mention log-space for implementation.
- Derivations: derive CKY Viterbi recurrence from dynamic programming; show base case (terminals) and induction (binary rules). Full marks: show indices i,j,k clearly and justify max vs sum distinction (Viterbi vs inside).
- Numericals: compute parse probabilities; show each rule used, multiply probabilities, numeric steps, and final bolded answer.
- Diagrams: asked to draw CKY chart or a parse tree — draw an upper-triangular table of spans labeled with nonterminals and probabilities/backpointers, or draw tree nodes labelled by nonterminals with terminals at leaves and annotate each edge with rule probabilities.
- Programs: you may be asked for pseudocode for probabilistic CKY. Include initialization for length-1 spans from terminals and nested loops for span length and split index k; show how to store backpointers and probability values (use log-probabilities in code sketch).
Quick check
Three short questions; answers in one final line.
- Define a PCFG in one sentence.
- What is the CKY time complexity (big-O) in terms of sentence length n and grammar size |G|?
- Give the MLE formula for estimating rule probability P(A -> β) from a treebank.
Answers: 1) A PCFG is a CFG equipped with a probability distribution over productions for each left-hand nonterminal such that probabilities of productions from the same nonterminal sum to 1. 2) O(n^3 · |G|). 3) P(A -> β) = count(A -> β) / count(A).
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?