SLIDE 1
Formal ¡Grammars ¡
Dorota ¡Glowacka ¡ dorota.glowacka@ed.ac.uk ¡
SLIDE 2 Syntax ¡
- Syntax ¡(Greek ¡syntaxis) ¡– ¡refers ¡to ¡the ¡way ¡
words ¡are ¡arranged ¡together. ¡
¡-‑ ¡ordering ¡of ¡strings ¡of ¡words ¡ ¡-‑ ¡how ¡to ¡compute ¡probabiliEes ¡ ¡-‑ ¡part ¡of ¡speech ¡categories ¡
- Today’s ¡lecture: ¡context-‑free ¡grammars ¡– ¡
formal ¡models ¡of ¡grammar ¡and ¡syntax ¡ ¡
SLIDE 3 Context-‑free ¡Grammars ¡
Why ¡context-‑free ¡grammars ¡(CFG)? ¡
- Backbone ¡of ¡many ¡formal ¡models ¡of ¡syntax ¡of ¡natural ¡
as ¡well ¡as ¡programming ¡languages ¡
- Integral ¡part ¡of ¡various ¡applicaEons: ¡
¡-‑ ¡Grammar ¡checking ¡ ¡-‑ ¡Dialogue ¡understanding ¡ ¡-‑ ¡Machine ¡translaEon ¡
- CFGs ¡ are ¡ powerful ¡ enough ¡ to ¡ express ¡ sophisEcated ¡
relaEons ¡ among ¡ words ¡ in ¡ a ¡ sentence, ¡ yet ¡ computaEonally ¡tractable ¡for ¡parsing ¡algorithms. ¡ ¡ ¡
SLIDE 4 ConsEtuency ¡
- Cons2tuency ¡ – ¡ groups ¡ of ¡ words ¡ behaving ¡ as ¡ ¡
single ¡units, ¡or ¡consEtuents. ¡
- Discovering ¡the ¡inventory ¡of ¡consEtuents ¡present ¡
in ¡the ¡language ¡is ¡a ¡significant ¡part ¡of ¡grammar ¡
- development. ¡
- Noun ¡phrase ¡– ¡a ¡sequence ¡of ¡words ¡surrounding ¡
at ¡least ¡one ¡noun: ¡ ¡Harry ¡the ¡Horse ¡ ¡The ¡Broadway ¡coppers ¡ ¡They ¡ ¡ ¡ ¡ ¡ ¡ ¡Three ¡par2es ¡from ¡Brooklyn ¡ ¡a ¡high-‑class ¡spot ¡such ¡as ¡Mindy’s ¡
SLIDE 5
ConsEtuency ¡
How ¡ do ¡ we ¡ know ¡ that ¡ these ¡ word ¡ groups ¡ are ¡ consEtuents? ¡ They ¡can ¡all ¡appear ¡in ¡similar ¡syntacEc ¡environments, ¡e.g. ¡ before ¡a ¡verb: ¡ ¡ ¡three ¡par2es ¡from ¡Brooklyn ¡arrive ¡… ¡ ¡ ¡a ¡high-‑class ¡spot ¡such ¡as ¡Mindy’s ¡a&racts ¡… ¡ ¡ ¡the ¡Broadway ¡coppers ¡love ¡… ¡ ¡ ¡they ¡sit ¡… ¡ However, ¡not ¡individual ¡words: ¡ ¡ ¡*from ¡arrive ¡… ¡ ¡ ¡*as ¡a&racts ¡… ¡ ¡ ¡*the ¡is ¡… ¡ ¡ ¡ ¡ ¡*high-‑class ¡sat ¡… ¡
SLIDE 6 ConsEtuency ¡
Preposed ¡and ¡postposed ¡construcEons, ¡e.g. ¡
¡
¡On ¡September ¡seventeenth, ¡I’d ¡like ¡to ¡fly ¡from ¡Atlanta ¡to ¡Denver. ¡ ¡I’d ¡like ¡to ¡fly ¡on ¡September ¡seventeenth ¡from ¡Atlanta ¡to ¡Denver. ¡ ¡I’d ¡like ¡to ¡fly ¡from ¡Atlanta ¡to ¡Denver ¡on ¡September ¡seventeenth. ¡ ¡
However, ¡not ¡individual ¡words ¡from ¡the ¡phrase: ¡
¡
¡*On ¡September, ¡I’d ¡like ¡to ¡fly ¡seventeenth ¡from ¡Atlanta ¡to ¡Denver. ¡ ¡*On ¡I’d ¡like ¡to ¡fly ¡September ¡seventeenth ¡from ¡Atlanta ¡to ¡Denver. ¡ ¡*I’d ¡like ¡to ¡fly ¡on ¡September ¡from ¡Atlanta ¡to ¡Denver ¡seventeenth. ¡
SLIDE 7 Context-‑free ¡Grammars ¡ ¡ (Chomsky ¡1956) ¡
- Also ¡called ¡Phrase-‑Structure ¡Grammars ¡
- Equivalent ¡to ¡Backus-‑Naur ¡Form ¡(BNF) ¡
A ¡CFG ¡consists ¡of ¡set ¡of ¡rules ¡expressing ¡how ¡symbols ¡of ¡the ¡ language ¡can ¡be ¡grouped ¡together, ¡and ¡a ¡lexicon ¡of ¡words ¡and ¡
¡ ¡ ¡ ¡NP ¡à ¡Det ¡Nominal ¡ ¡ ¡ ¡ ¡ ¡NP ¡à ¡ProperNoun ¡ ¡ ¡ ¡ ¡ ¡Nominal ¡à ¡Noun ¡| ¡Nominal ¡Noun ¡ Context-‑free ¡rules ¡can ¡be ¡hierarchically ¡embedded: ¡ ¡ ¡ ¡ ¡Det ¡à ¡a ¡ ¡ ¡ ¡ ¡Det ¡à ¡the ¡ ¡ ¡ ¡ ¡Noun ¡à ¡flight ¡
SLIDE 8 Context-‑free ¡Grammars ¡
- Terminal ¡symbols ¡– ¡symbols ¡that ¡correspond ¡to ¡words ¡in ¡
the ¡language: ¡the, ¡nightclub ¡
- Non-‑terminals ¡– ¡symbols ¡that ¡express ¡abstracEons ¡
CFG ¡can ¡be ¡thought ¡of ¡as ¡sentence ¡generator ¡so ¡we ¡can ¡read ¡ à ¡as ¡rewrite ¡the ¡symbol ¡on ¡the ¡leO ¡with ¡the ¡string ¡of ¡symbols ¡
¡Star2ng ¡from ¡the ¡symbol: ¡ ¡ ¡ ¡ ¡NP ¡ ¡We ¡can ¡use ¡our ¡first ¡rule ¡to ¡rewrite ¡NP: ¡ ¡Det ¡Nominal ¡ ¡And ¡then ¡rewrite ¡Nominal ¡as: ¡ ¡ ¡ ¡Det ¡Noun ¡ ¡And ¡finally ¡rewrite ¡these ¡parts ¡of ¡speech: ¡a ¡flight ¡ ¡ The ¡sequence ¡of ¡rule ¡expansion ¡is ¡called ¡a ¡deriva2on. ¡
SLIDE 9
Parse ¡Tree ¡and ¡Bracketed ¡NotaEon ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡
¡ [S[NP[PRO ¡I]][VP ¡[V ¡prefer][NP[Det ¡a][Nom[N ¡morning] [Nom[N ¡flight]]]]]] ¡
SLIDE 10 More ¡Rules ¡
¡ ¡S ¡à ¡NP ¡VP ¡ ¡I ¡prefer ¡a ¡morning ¡flight ¡ A ¡verb ¡phrase ¡consists ¡of ¡a ¡verb ¡followed ¡by ¡other ¡things: ¡ ¡VP ¡à ¡Verb ¡NP ¡ ¡ ¡prefer ¡a ¡morning ¡flight ¡ ¡VP ¡à ¡Verb ¡NP ¡PP ¡ ¡leave ¡Boston ¡in ¡the ¡morning ¡ ¡VP ¡à ¡Verb ¡PP ¡ ¡ ¡leaving ¡on ¡Thursday ¡ A ¡preposi2onal ¡phrase ¡PP ¡has ¡a ¡preposiEon ¡followed ¡by ¡a ¡ noun ¡phrase ¡NP: ¡ ¡PP ¡à ¡Preposi2on ¡NP ¡ ¡from ¡Los ¡Angeles ¡ ¡ to ¡London ¡ ¡ ¡about ¡the ¡ground ¡transporta2on ¡in ¡Chicago ¡
- n ¡these ¡flights ¡on ¡the ¡ninth ¡of ¡July ¡
in ¡the ¡evening ¡ ¡with ¡a ¡stopover ¡in ¡Nashville ¡
SLIDE 11
Sample ¡Lexicon ¡
Noun ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡à ¡flights| ¡breeze| ¡trip| ¡morning ¡ Verb ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡à ¡is| ¡prefer| ¡like| ¡need| ¡want| ¡fly ¡ Adjec2ve ¡ ¡ ¡ ¡ ¡à ¡cheapest| ¡first| ¡latest| ¡other| ¡direct ¡ Pronoun ¡ ¡ ¡ ¡ ¡ ¡ ¡à ¡me| ¡I ¡| ¡you| ¡it ¡ ProperNoun ¡à ¡Alaska| ¡Chicago| ¡Los ¡Angeles ¡ Determiner ¡ ¡à ¡the| ¡a| ¡an| ¡this| ¡that| ¡these ¡ Preposi2on ¡ ¡à ¡from| ¡to| ¡on| ¡near ¡ Conjunc2on ¡à ¡and| ¡or| ¡but ¡
SLIDE 12
Grammar ¡Rules ¡
S ¡à ¡NP ¡VP ¡ ¡ ¡ ¡ ¡I ¡+ ¡want ¡a ¡morning ¡flight ¡ NP ¡à ¡Pronoun ¡ ¡ ¡ ¡I ¡ ¡| ¡ProperNoun ¡ ¡ ¡ ¡London ¡ ¡| ¡Det ¡Nominal ¡ ¡ ¡a ¡+ ¡flight ¡ Nominal ¡à ¡Nominal ¡Noun ¡morning ¡+ ¡flight ¡ ¡ ¡ ¡ ¡ ¡ ¡| ¡Noun ¡ ¡ ¡ ¡flights ¡ PP ¡à ¡PreposiEon ¡NP ¡ ¡ ¡from ¡+ ¡Los ¡Angeles ¡ VP ¡à ¡Verb ¡ ¡ ¡ ¡ ¡do ¡ ¡| ¡Verb ¡NP ¡ ¡ ¡ ¡ ¡want ¡+ ¡a ¡flight ¡ ¡| ¡Verb ¡NP ¡PP ¡ ¡ ¡ ¡leave ¡+ ¡Boston ¡+ ¡in ¡the ¡morning ¡ ¡| ¡Verb ¡PP ¡ ¡ ¡ ¡ ¡leaving ¡+ ¡on ¡Thursday ¡
SLIDE 13
Context-‑Free ¡Grammar ¡
Gramma2cal ¡sentence ¡– ¡a ¡sentence ¡derived ¡by ¡a ¡ grammar ¡and ¡is ¡in ¡the ¡formal ¡language ¡defined ¡by ¡ that ¡grammar. ¡ Ungramma2cal ¡sentence ¡– ¡a ¡sentence ¡that ¡cannot ¡ be ¡derived ¡by ¡a ¡given ¡formal ¡grammar ¡and ¡is ¡not ¡in ¡ the ¡language ¡defined ¡by ¡that ¡grammar. ¡ Genera2ve ¡grammar ¡– ¡formal ¡language ¡used ¡to ¡ model ¡natural ¡language ¡since ¡the ¡language ¡is ¡ defined ¡by ¡the ¡set ¡of ¡possible ¡sentences ¡generated ¡ by ¡the ¡grammar. ¡ ¡
SLIDE 14
CFG ¡– ¡Formal ¡DefiniEon ¡
A ¡CFG ¡G ¡is ¡defined ¡by ¡four ¡parameters ¡N, ¡Σ, ¡R, ¡S: ¡ ¡N ¡is ¡a ¡set ¡of ¡non-‑terminal ¡symbols ¡(or ¡variables) ¡ ¡Σ ¡is ¡a ¡set ¡of ¡terminal ¡symbols ¡(disjoint ¡from ¡N) ¡ ¡R ¡is ¡a ¡set ¡of ¡rules, ¡each ¡of ¡the ¡form ¡A ¡à ¡β, ¡where ¡ ¡ ¡ ¡A ¡is ¡a ¡non-‑terminal ¡ ¡ ¡β ¡is ¡a ¡string ¡of ¡symbols ¡from ¡the ¡infinite ¡set ¡of ¡strings ¡(Σ ¡U ¡N)* ¡ ¡S ¡is ¡a ¡designated ¡start ¡symbol ¡and ¡a ¡member ¡of ¡N ¡ ¡ Naming ¡conven2on ¡ ¡Capital ¡lebers ¡like ¡A, ¡B ¡and ¡S ¡ ¡Non-‑terminals ¡ ¡S ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡the ¡start ¡symbol ¡ ¡Lower-‑case ¡Greek ¡lebers ¡α, ¡β, ¡γ ¡ ¡ ¡Strings ¡drawn ¡from ¡(Σ ¡U ¡N)* ¡ ¡Lower-‑case ¡Roman ¡lebers ¡u, ¡v, ¡w ¡String ¡terminals ¡ ¡
SLIDE 15
DerivaEon ¡
A ¡language ¡is ¡defined ¡through ¡the ¡concept ¡of ¡ deriva2on: ¡ ¡ ¡if ¡A ¡à ¡β ¡is ¡a ¡rule ¡of ¡R, ¡and ¡α ¡and ¡γ ¡are ¡ ¡any ¡ strings ¡in ¡the ¡set ¡ ¡(Σ ¡U ¡N)*, ¡ ¡ then ¡we ¡say ¡that ¡αAγ ¡directly ¡derives ¡αβγ, ¡or ¡ αAγ ¡=> ¡αβγ ¡
SLIDE 16
DerivaEon ¡
DerivaEon ¡is ¡then ¡a ¡generalizaEon ¡of ¡direct ¡derivaEon: ¡ ¡Let ¡α1, ¡α2, ¡… ¡αm ¡be ¡strings ¡in ¡(Σ ¡U ¡N)*, ¡m ¡≥ ¡1, ¡such ¡that ¡ ¡α1 ¡=> ¡α2, ¡α2 ¡=> ¡α3, ¡…, ¡αm-‑1 ¡=> ¡αm ¡ We ¡say ¡that ¡α1 ¡derives ¡αm, ¡or ¡α1 ¡=> ¡αm ¡ ¡ We ¡formally ¡define ¡the ¡language ¡LG ¡generated ¡by ¡ grammar ¡G ¡as ¡the ¡set ¡of ¡strings ¡composed ¡of ¡terminal ¡ symbols ¡that ¡can ¡be ¡derived ¡from ¡the ¡designated ¡start ¡ symbol ¡S ¡ LG ¡= ¡{w|w ¡is ¡in ¡Σ* ¡and ¡S ¡=> ¡w} ¡
SLIDE 17
Grammar ¡Equivalence ¡
If ¡two ¡grammars ¡generate ¡the ¡same ¡set ¡of ¡strings, ¡ are ¡they ¡equivalent? ¡ Two ¡ grammars ¡ are ¡ strongly ¡ equivalent ¡ if ¡ they ¡ generate ¡the ¡same ¡set ¡of ¡strings ¡and ¡if ¡they ¡assign ¡ the ¡same ¡phrase ¡structure ¡to ¡each ¡sentence. ¡ ¡ Two ¡ grammars ¡ are ¡ weakly ¡ equivalent ¡ if ¡ they ¡ generate ¡the ¡same ¡set ¡of ¡strings ¡but ¡do ¡not ¡assign ¡ the ¡same ¡phrase ¡structure ¡to ¡each ¡sentence. ¡ ¡
SLIDE 18
Chomsky ¡Normal ¡Form ¡(CNF) ¡
Each ¡producEon ¡is ¡of ¡the ¡form: ¡ ¡A ¡à ¡B ¡C ¡ ¡or ¡ ¡ ¡A ¡à ¡a ¡ The ¡right-‑hand ¡side ¡of ¡each ¡rule ¡has ¡two ¡non-‑terminal ¡ symbols ¡or ¡one ¡terminal ¡symbol. ¡ ¡ Any ¡CFG ¡can ¡be ¡converted ¡into ¡a ¡weakly ¡equivalent ¡CNF: ¡ A ¡à ¡B ¡C ¡D ¡ Can ¡be ¡converted ¡into ¡the ¡following ¡two ¡CNF ¡rules: ¡ A ¡à ¡B ¡X ¡ X ¡à ¡C ¡D ¡
SLIDE 19
Binary ¡Branching ¡
CNF ¡are ¡binary ¡branching ¡– ¡they ¡have ¡binary ¡trees: ¡ ¡ ¡A ¡à ¡B ¡X ¡ ¡ ¡X ¡à ¡C ¡D ¡ Binary ¡branching ¡can ¡produce ¡smaller ¡grammars: ¡ ¡ ¡VP ¡à ¡VB ¡NP ¡PP* ¡ ¡ ¡VP ¡à ¡VB ¡NP ¡PP ¡ ¡ ¡VP ¡à ¡VB ¡NP ¡PP ¡PP ¡ ¡ ¡VP ¡à ¡VB ¡NP ¡PP ¡PP ¡PP ¡ Could ¡also ¡be ¡generated ¡by ¡two-‑grammar ¡rule: ¡ ¡ ¡VP ¡à ¡VB ¡NP ¡PP ¡ ¡ ¡VP ¡à ¡VP ¡PP ¡
SLIDE 20
Ambiguity ¡
One ¡morning ¡I ¡shot ¡an ¡elephant ¡in ¡my ¡pajamas, ¡ How ¡he ¡got ¡into ¡my ¡pajamas ¡I ¡don’t ¡know. ¡ Groucho ¡Marx, ¡Animal ¡Crackers, ¡1930 ¡ ¡ What ¡makes ¡these ¡sentences ¡ambiguous? ¡ ¡ Structural ¡ambiguity ¡– ¡the ¡sentence ¡can ¡be ¡ parsed ¡in ¡more ¡than ¡one ¡way. ¡
SLIDE 21
Ambiguity ¡
SLIDE 22
Ambiguity ¡
We ¡saw ¡the ¡Eiffel ¡Tower ¡flying ¡to ¡Paris. ¡ We ¡saw ¡[the ¡Eiffel ¡Tower ¡flying ¡to ¡Paris]. ¡ We ¡[saw ¡[the ¡Eiffel ¡Tower] ¡flying ¡to ¡Paris]. ¡ ¡ ¡ Old ¡men ¡and ¡women ¡ [old ¡[men ¡and ¡women]] ¡or ¡ [old ¡men] ¡and ¡[women] ¡
SLIDE 23
Ambiguity ¡
President ¡ Kennedy ¡ today ¡ pushed ¡ aside ¡ other ¡ White ¡House ¡business ¡to ¡devote ¡all ¡his ¡2me ¡and ¡ aoen2on ¡to ¡working ¡on ¡the ¡Berlin ¡crisis ¡address ¡ he ¡will ¡deliver ¡tomorrow ¡night ¡to ¡the ¡American ¡ people ¡over ¡na2onwide ¡television ¡and ¡radio. ¡ ¡ What ¡ambiguiEes ¡can ¡you ¡find ¡in ¡this ¡sentence? ¡