Context-Free Grammars and Constituency Parsing

Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft, ch. 19

54 cards

Introduction

  1. 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.
  2. 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.

19.1 Constituency

  1. What is syntactic constituency?
    The idea that groups of words can behave as single units (constituents), e.g. noun phrases.
  2. 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).

19.2 Context-free grammars

  1. 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).
  2. 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).
  3. 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.
  4. 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.
  5. 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).
  6. What is the language defined by a CFG?
    The set of strings derivable from the designated start symbol, usually S (“sentence”).
  7. 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.
  8. 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]]]]]
    SNPVPProVerbNPDetNomNomNounNounIpreferamorningflight
  9. 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.
  10. 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).
  11. 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\).
  12. 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\}\).

19.3 Treebanks

  1. 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).
  2. 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.

19.4 Grammar equivalence and normal form

  1. 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.
  2. 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.
  3. How is a rule A → B C D converted to CNF?
    Introduce a new non-terminal: A → B X and X → C D.
  4. 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.

19.5 Ambiguity

  1. What is structural ambiguity?
    When a grammar can assign more than one parse to a sentence.
  2. 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 attachmentshot [NP an elephant in my pajamas]
    VP attachment[VP shot an elephant] in my pajamas
  3. 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.
  4. What is coordination ambiguity?
    Ambiguity in what a conjunction conjoins: old men and women = [old [men and women]] or [[old men] and women].
  5. 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).

19.6 CKY parsing

  1. 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.
  2. What grammar form does CKY require?
    Chomsky normal form (rules A → B C or A → w).
  3. 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).
  4. 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).
  5. 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]\).
  6. 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.
  7. 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.
  8. Fill in the CKY table for “Book the flight through Houston”.
    BooktheflightthroughHouston
    0S, VP, Verb, Nominal, NounS, VP, X2S, VP, X2 (3 parses)
    1DetNPNP
    2Nominal, NounNominal
    3PrepPP
    4NP, ProperNoun
    Row = start position \(i\), column = end word; empty cells hold no constituent.
  9. 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).
  10. 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.
  11. 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.

19.7 Span-based neural parsing

  1. 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”).
  2. 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.
  3. 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.
  4. 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)))\).
  5. 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.
  6. 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.
  7. 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.

19.8 Evaluating parsers

  1. 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.
  2. 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)).
  3. What is evalb?
    The canonical implementation of PARSEVAL, which also canonicalizes grammar-specific details (auxiliaries, pre-infinitival to…) for comparing parsers with different grammars.

19.9 Heads and head-finding

  1. 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).
  2. 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.
  3. 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.

Historical notes

  1. 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.
  2. 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.
  3. 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.

Source