Context-Free Grammars and Constituency Parsing
Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19
54 cards
Introduction
- What does the word “syntax” come from, and what does it study?Greek sýntaxis, “setting out together, arrangement”: the way words are arranged together.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, Introduction, p. 425
- What is syntactic parsing, and what are parse trees used for?Assigning a syntactic structure to a sentence. Uses: grammar checking (unparsable sentences may be ungrammatical), an intermediate representation for semantic analysis, and text analysis in data-science applications that model relations between sentence elements.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, Introduction, p. 425
19.1 Constituency
- What is syntactic constituency?The idea that groups of words can behave as single units (constituents), e.g. noun phrases.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.1, p. 426
- What evidence shows that a sequence like “three parties from Brooklyn” is a constituent?The whole sequence appears in the same syntactic environments as other noun phrases (e.g. before a verb: “three parties from Brooklyn arrive”), while its individual words can’t (*from arrive, *the is).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.1, p. 426
19.2 Context-free grammars
- What are other names for context-free grammars, and who formalized them?Phrase-structure grammars; the formalism is equivalent to Backus-Naur form (BNF). The idea of constituent structure goes back to Wundt (1900); it was formalized by Chomsky (1956) and independently by Backus (1959).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 426
- What does a context-free grammar consist of?A set of rules (productions) saying how symbols can be grouped and ordered (e.g. NP → Det Nominal, Nominal → Noun | Nominal Noun) and a lexicon of words and symbols (Det → a, Noun → flight).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 426
- What is the difference between terminal and non-terminal symbols?Terminals correspond to words (“the”, “flight”); the lexicon introduces them. Non-terminals are abstractions over terminals (NP, VP, Nominal). A rule has one non-terminal on the left and an ordered list of terminals and non-terminals on the right.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 427
- What is a derivation, and how is it represented?The sequence of rule expansions that produces a string from a non-terminal (NP → Det Nominal → Det Noun → a flight), usually shown as a parse tree with the root at the top.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 427
- What does it mean that a node dominates or immediately dominates other nodes?A node dominates all nodes below it in the tree; it immediately dominates its direct children (NP immediately dominates Det and Nom).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 427
- What is the language defined by a CFG?The set of strings derivable from the designated start symbol, usually S (“sentence”).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, pp. 427–428
- Give the main rules of the toy grammar \(L_0\) for English.S → NP VP; NP → Pronoun | Proper-Noun | Det Nominal; Nominal → Nominal Noun | Noun; VP → Verb | Verb NP | Verb NP PP | Verb PP; PP → Preposition NP.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, pp. 428–429
- Draw the parse tree of “I prefer a morning flight” under \(L_0\), in bracketed notation too.[S [NP [Pro I]] [VP [V prefer] [NP [Det a] [Nom [Nom [N morning]] [N flight]]]]]
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 429
- What are grammatical and ungrammatical sentences with respect to a formal grammar?Grammatical sentences can be derived by the grammar (they are in its formal language); ungrammatical ones cannot. This hard in/out line only simplifies natural language, where acceptability often depends on context.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 429
- What is generative grammar?In linguistics, using formal languages to model natural languages: the language is the set of sentences “generated” by the grammar (a different sense of “generate” than LLM text generation).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 429
- Give the formal definition of a context-free grammar.A 4-tuple \((N, \Sigma, R, S)\): non-terminals \(N\), terminals \(\Sigma\) (disjoint from \(N\)), rules \(R\) of the form \(A \to \beta\) with \(A \in N\) and \(\beta \in (\Sigma \cup N)^*\), and a start symbol \(S \in N\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 430
- Define “directly derives” and the language generated by a grammar.If \(A \to \beta \in R\), then \(\alpha A \gamma \Rightarrow \alpha\beta\gamma\). Derivation (\(\Rightarrow^*\)) is a chain of direct derivations, and \(L_G = \{w \mid w \in \Sigma^*, S \Rightarrow^* w\}\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.2, p. 430
19.3 Treebanks
- What is a treebank, and how are treebanks usually built?A corpus in which every sentence is annotated with a parse tree. Usually a parser is run over each sentence and linguists hand-correct the output (e.g. the Penn Treebank for English, Arabic, Chinese).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.3, pp. 430–431
- How does a treebank implicitly define a grammar, and what is notable about the Penn Treebank’s?The CFG rules can be read off the parsed sentences. The Penn Treebank grammar is very flat, giving thousands of rules, e.g. separate VP rules for every number and arrangement of PPs and arguments.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.3, pp. 431–432
19.4 Grammar equivalence and normal form
- What is the difference between strongly and weakly equivalent grammars?Strongly equivalent grammars generate the same strings and assign the same phrase structure to each (up to renaming non-terminals); weakly equivalent grammars generate the same strings but assign different structures.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.4, p. 432
- What is Chomsky normal form (CNF)?An ε-free grammar in which every rule is A → B C (two non-terminals) or A → a (one terminal). CNF grammars are binary branching. Any CFG can be converted into a weakly equivalent CNF grammar.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.4, p. 432
- How is a rule A → B C D converted to CNF?Introduce a new non-terminal: A → B X and X → C D.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.4, pp. 432–433
- What is Chomsky-adjunction, and how can it shrink a grammar?Generating a potentially infinite sequence with a rule of the form A → A B. Instead of VP → VBD NP PP, VP → VBD NP PP PP, … one can write VP → VBD NP PP and VP → VP PP.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.4, p. 433
19.5 Ambiguity
- What is structural ambiguity?When a grammar can assign more than one parse to a sentence.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.5, p. 433
- Why is “I shot an elephant in my pajamas” structurally ambiguous?PP-attachment: in my pajamas can attach inside the NP headed by elephant (the elephant wears the pajamas) or to the VP headed by shot (the shooter wears them).
NP attachment shot [NP an elephant in my pajamas] VP attachment [VP shot an elephant] in my pajamas - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.5, p. 434
- What is attachment ambiguity? Give a non-PP example.A constituent can attach to the tree at more than one place. E.g. in “We saw the Eiffel Tower flying to Paris”, the gerundive VP flying to Paris can belong to a clause whose subject is the Eiffel Tower, or modify saw.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.5, p. 434
- What is coordination ambiguity?Ambiguity in what a conjunction conjoins: old men and women = [old [men and women]] or [[old men] and women].
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.5, p. 434
- Why is ambiguity such a serious problem for parsers?Real sentences contain many grammatically valid but semantically unreasonable parses, combining attachment and coordination ambiguities; a parser must handle them efficiently (CKY) and choose the right one (syntactic disambiguation).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.5, p. 435
19.6 CKY parsing
- Why is dynamic programming effective for parsing ambiguous sentences?Because rules are context-free, once a constituent has been found over a span it can be recorded and reused in any larger derivation, rather than reanalyzed. Such table-based methods are also called chart parsing.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 435
- What grammar form does CKY require?Chomsky normal form (rules A → B C or A → w).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 435
- How is a general CFG converted to CNF?(1) Keep conforming rules; (2) replace terminals inside longer rules with dummy non-terminals (INF-VP → to VP becomes INF-VP → TO VP, TO → to); (3) eliminate unit productions (A → B) by rewriting with the non-unit rules they lead to; (4) binarize long rules with new non-terminals (S → Aux NP VP becomes S → X1 VP, X1 → Aux NP).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, pp. 435–436
- What are unit productions, and what does eliminating them do to the grammar?Rules with a single non-terminal on the right (Nominal → Noun). Eliminating them flattens the grammar and promotes terminals to high levels, e.g. verbs become both VPs and Ss (S → book | include | prefer).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, pp. 435–436
- What does cell \([i, j]\) of the CKY table contain, and what are fenceposts?The set of non-terminals for all constituents spanning positions \(i\) to \(j\). Indices point at the gaps between words (0 Book 1 the 2 flight 3 …), called fenceposts; the whole sentence is cell \([0, n]\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 436
- How is a cell \([i, j]\) filled in CKY?Since every constituent has two daughters, for each split point \(k\) with \(i < k < j\), combine an entry B in \([i, k]\) (to the left, along row \(i\)) with an entry C in \([k, j]\) (below, along column \(j\)) whenever the grammar has A → B C, and add A.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, pp. 436–437
- In what order does CKY fill the table, and why?Column by column from left to right, each column from bottom to top, so the cells to the left and below that a cell depends on are already filled. This also mirrors processing words one at a time.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, pp. 437–438
- Fill in the CKY table for “Book the flight through Houston”.
Row = start position \(i\), column = end word; empty cells hold no constituent.Book the flight through Houston 0 S, VP, Verb, Nominal, Noun S, VP, X2 S, VP, X2 (3 parses) 1 Det NP NP 2 Nominal, Noun Nominal 3 Prep PP 4 NP, ProperNoun - Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 438
- Why does cell [0, 5] for “Book the flight through Houston” contain several S entries?There are alternative parses: the PP through Houston modifies the flight, modifies the booking (VP → VP PP), or is the second argument of the original VP → Verb NP PP (now X2 PP).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 438
- Is the basic CKY algorithm a parser?No, a recognizer: it tells whether an S spans [0, n]. To parse, store with each non-terminal backpointers to the cells it came from and allow multiple copies of a non-terminal per cell; then the table contains all parses.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 438
- What practical problem does CNF conversion cause, and how is it handled?Returned trees don’t match the original grammar. Binarization can be undone by deleting dummy non-terminals and promoting their daughters; unit productions are easier to handle by modifying CKY itself.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.6, p. 439
19.7 Span-based neural parsing
- What problem of CKY does span-based neural constituency parsing solve?CKY enumerates all parses but doesn’t say which is correct. A neural classifier scores each labeled span, and a modified CKY combines span scores to find the best-scoring tree (“neural CKY”).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, p. 441
- How does the span-based parser of Kitaev et al. (2019) differ from classic CKY?It doesn’t use a hand-written grammar to constrain which constituents combine; it relies on learned span representations to encode likely combinations.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, p. 441
- How are words represented in a span-based parser built on BERT?Run the tokens through a pretrained model like BERT, map subwords to words (e.g. use the first subword), optionally add transformer layers, giving one output per word.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, pp. 441–442
- How is a span \((i, j)\) represented and scored?Split each word output into a leftward and a rightward half, representing fencepost endpoints; the span vector concatenates the differences of the endpoint halves. An MLP gives a score per label: \(s(i, j, \cdot) = W_2\,\text{ReLU}(\text{LayerNorm}(W_1v(i, j)))\).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, p. 442
- How is a whole tree scored in span-based parsing?A tree is a set of labeled spans \(\{(i_t, j_t, l_t)\}\); its score is the sum of their span scores, and the parser picks the tree with the maximum score.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, p. 442
- Why not simply pick the best label for each span independently?The greedy choice isn’t guaranteed to form a valid tree (though it usually does), so a CKY variant is used.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, p. 443
- Give the CKY recursion used in span-based parsing.\(s_{best}(i, i+1) = \max_l s(i, i+1, l)\); for longer spans, \(s_{best}(i, j) = \max_l s(i, j, l) + \max_k[s_{best}(i, k) + s_{best}(k, j)]\), with no grammar constraints: the network learns them.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.7, p. 443
19.8 Evaluating parsers
- What are the PARSEVAL metrics?Measures of how well a hypothesis parse’s constituents match a hand-labeled reference (gold) parse: a hypothesis constituent is correct if a reference constituent has the same start, end and label. Labeled recall = correct / reference constituents; labeled precision = correct / hypothesis constituents; and F1.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.8, p. 443
- What is the crossing brackets metric?The number of constituents bracketed one way in the reference, e.g. ((A B) C), but crossing it in the hypothesis, e.g. (A (B C)).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.8, p. 444
- What is evalb?The canonical implementation of PARSEVAL, which also canonicalizes grammar-specific details (auxiliaries, pre-infinitival to…) for comparing parsers with different grammars.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.8, p. 444
19.9 Heads and head-finding
- What is the lexical head of a constituent?The grammatically most important word of the phrase (N for an NP, V for a VP). Heads are passed up the tree, so each non-terminal is annotated with its head word; this idea dates to Bloomfield (1914).
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.9, p. 444
- Why are heads important?They are central to dependency grammar and can map between constituency and dependency parses; they matter in probabilistic parsing and formalisms like HPSG.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.9, p. 444
- How are heads found in practical systems?Dynamically after parsing, by walking the tree with hand-written head rules (a head percolation table, Magerman/Collins), e.g. for NP: return a final POS; else search right-to-left for an NN, NNP, NNPS…; else left-to-right for an NP; and so on.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, §19.9, p. 445
Historical notes
- How did American and European grammatical traditions differ?American structuralism (Bloomfield) developed immediate-constituent analysis; traditional European grammar defined relations between words, the root of dependency grammars.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, Historical notes, p. 446
- What was Harris’s substitutability test for constituents?If a simple form (e.g. man) can substitute for a more complex sequence (intense young man) in a construction, the sequence is probably a constituent, an early idea of constituents as equivalence classes.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, Historical notes, pp. 446–447
- Who discovered dynamic programming parsing?John Cocke implemented a parser with the roots of CKY in 1960, later formalized by Kasami (1965) and Younger (1967); Kuno’s well-formed substring table and Kay’s chart were related; Earley (1968) gave the top-down version.
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19, Historical notes, p. 447
Source
- Daniel Jurafsky and James H. Martin. Speech and Language Processing. 3rd edition (draft). https://web.stanford.edu/~jurafsky/slp3/