CFGs and CFLs Context-Free Grammars Programming language - - PowerPoint PPT Presentation
CFGs and CFLs Context-Free Grammars Programming language - - PowerPoint PPT Presentation
CFGs and CFLs Context-Free Grammars Programming language specifica6on Parsing Natural language understanding Markup languages A genera6ve model
Context-‑Free ¡Grammars ¡
- Programming ¡language ¡specifica6on ¡
- Parsing ¡
- Natural ¡language ¡understanding ¡
- Markup ¡languages ¡
- A ¡genera6ve ¡model ¡giving ¡structure ¡
Programming ¡Languages ¡
Natural ¡Language ¡Systems ¡
Natural ¡Language ¡Systems ¡
- h>p://www.bandnamemaker.com ¡
- h>p://www.outofservice.com/country/ ¡
- h>p://www.elsewhere.org/journal/pomo/ ¡
- h>p://pdos.csail.mit.edu/scigen/ ¡
Models ¡of ¡Growth ¡
- L-‑systems ¡
- h>p://www.kevs3d.co.uk/dev/lsystems/ ¡
Miscellaneous ¡
Kolam ¡drawing ¡generated ¡by ¡picture ¡ grammar ¡
CFG ¡definiBon ¡
A ¡CFG ¡is ¡a ¡quadruple ¡G ¡= ¡(V, ¡T, ¡P, ¡S) ¡
- V ¡a ¡set ¡of ¡nonterminal ¡symbols ¡
- T ¡a ¡set ¡of ¡terminal ¡symbols ¡
- P ¡is ¡a ¡set ¡of ¡producBons, ¡each ¡of ¡the ¡form ¡
¡ ¡A ¡ ¡α ¡where ¡α ¡is ¡an ¡element ¡of ¡(V ¡U ¡T)* ¡
¡ ¡Meaning: ¡ ¡ ¡can ¡replace ¡A ¡with ¡α ¡
- S ¡ ¡in ¡V ¡is ¡a ¡start ¡symbol ¡
Example: ¡ ¡G ¡= ¡(V, ¡T, ¡P, ¡S), ¡where... ¡
- V ¡= ¡{S} ¡
- T ¡= ¡{a,b} ¡
- P ¡= ¡{ ¡S ¡ ¡ε ¡ ¡| ¡ ¡a ¡| ¡ ¡b ¡ ¡| ¡ ¡aSa ¡ ¡| ¡ ¡bSb ¡} ¡
¡ ¡(abbrevia6on ¡for ¡Sε, ¡Sa, ¡Sb, ¡...) ¡
- S ¡= ¡start ¡symbol ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡ ¡aSa ¡ ¡abSba ¡ ¡abbSbba ¡ ¡abbbba ¡ ¡ ¡What ¡strings ¡can ¡S ¡generate ¡like ¡this? ¡
Palindromes ¡
- Adam ¡in ¡Eden ¡I’m ¡Adam ¡
- Dog ¡doo? ¡ ¡Good ¡God! ¡
- Dogma: ¡ ¡I ¡am ¡God. ¡
- A ¡man, ¡a ¡plan, ¡a ¡canal, ¡Panama ¡
- Are ¡we ¡not ¡drawn ¡onward, ¡we ¡few, ¡drawn ¡onward ¡
to ¡new ¡era? ¡
- Doc, ¡note: ¡ ¡I ¡dissent. ¡ ¡A ¡fast ¡never ¡prevents ¡a ¡
- fatness. ¡ ¡I ¡diet ¡on ¡cod. ¡
- h>p://www.palindromelist.net ¡
NotaBon ¡& ¡ConvenBons ¡
Let ¡G ¡= ¡(V, ¡T, ¡P, ¡S), ¡then ¡
- a, ¡b, ¡c, ¡d, ¡... ¡in ¡T ¡ ¡ ¡(terminals) ¡
- A, ¡B, ¡C, ¡D, ¡... ¡in ¡V ¡ ¡(nonterminals) ¡
- u,v,w,x,y... ¡in ¡T* ¡
- α,β,γ,δ..[not ¡ε]... ¡in ¡(V ¡ ¡U ¡T)* ¡
- X, ¡Y, ¡Z ¡in ¡ ¡V ¡ ¡U ¡T ¡
“Derives” ¡relaBon ¡
Def: ¡ ¡α1 ¡G ¡α2 ¡ ¡iff ¡ ¡ ¡for ¡some ¡A ¡in ¡V ¡ ¡for ¡some ¡β, ¡γ, ¡δ, ¡in ¡(V ¡U ¡T)* ¡
¡α1 ¡= ¡βAδ ¡ ¡ ¡ ¡, ¡ ¡ ¡α2 ¡= ¡βγδ
¡ ¡ ¡ ¡ ¡ ¡, ¡ ¡and ¡ ¡ ¡A ¡ ¡γ ¡is ¡in ¡P ¡ ¡ “α1 ¡can ¡derive ¡α2 ¡in ¡a ¡single ¡produc6on ¡applica6on” ¡ ¡ ¡
More ¡commonly ¡wri>en ¡with ¡a ¡double-‑arrow ¡ ¡“ ¡ ¡ ¡ ¡ ¡ ¡ ¡” ¡ ¡ ¡ ¡ Also ¡called ¡“yields”: ¡ ¡“α1 ¡derives ¡α2” ¡ ¡or ¡ ¡ ¡“α1 ¡yields ¡α2” ¡ We ¡eliminate ¡the ¡subscript ¡G ¡if ¡grammar ¡is ¡understood ¡from ¡context ¡ ¡ ¡ ¡ ¡ ¡
⇒
“Derives” ¡relaBon ¡(cont.) ¡
Def: ¡ ¡α1 ¡k ¡α2 ¡ ¡induc6vely ¡by: ¡ ¡ ¡α1 ¡0 ¡α2 ¡ ¡if ¡α1 ¡= ¡α2 ¡ ¡
¡ ¡ ¡α1 ¡k ¡α2 ¡ ¡if ¡ ¡α1 ¡ ¡ ¡β ¡k-‑1 ¡α2 ¡
Def: ¡Let ¡* ¡be ¡the ¡reflexive ¡& ¡transi6ve ¡closure ¡of ¡ the ¡rela6on ¡ ¡ ¡ In ¡other ¡words, ¡α1 ¡* ¡α2 ¡ ¡ ¡iff ¡for ¡some ¡k, ¡α1 ¡k ¡α2 ¡ ¡
DefiniBon ¡of ¡L(G) ¡and ¡CFLs ¡
- If ¡G ¡= ¡(V,T,P,S) ¡is ¡a ¡CFG, ¡then ¡ ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡L(G) ¡= ¡{w ¡in ¡T* ¡| ¡S ¡* ¡w} ¡
- In ¡other ¡words, ¡L(G), ¡contains ¡exactly ¡the ¡
strings ¡of ¡terminals ¡that ¡S ¡can ¡derive. ¡ ¡
- L(G) ¡is ¡the ¡language ¡generated ¡by ¡G. ¡ ¡
CFLs ¡ ¡= ¡{L ¡| ¡L ¡= ¡L(G) ¡for ¡some ¡CFG ¡G} ¡
2 ¡– ¡minute ¡challenge ¡
Create ¡a ¡grammar ¡that ¡generates ¡{0n1n|n ¡≥ ¡0} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡ ¡0S1 ¡| ¡ε ¡
5 ¡– ¡minute ¡challenge ¡
Create ¡a ¡grammar ¡that ¡generates ¡{0n1m|n≠m} ¡
More ¡definiBons.... ¡
- A ¡sentenBal ¡form ¡is ¡any ¡string ¡ ¡α ¡such ¡that ¡S ¡* ¡α ¡ ¡
- A ¡derivaBon ¡of ¡w ¡is ¡a ¡sequence ¡of ¡senten6al ¡
forms ¡S ¡= ¡α1 ¡ ¡α2 ¡ ¡... ¡ ¡αn ¡= ¡w ¡
S ¡ ¡aSb ¡ ¡abSab ¡ ¡abSSab ¡ ¡abbaSab ¡ ¡abbaab ¡ A ¡deriva6on ¡of ¡abbaab ¡: ¡ ¡
S ¡ ¡aSb ¡| ¡bSa ¡| ¡SS ¡| ¡ab| ¡ba ¡| ¡ε ¡
DerivaBon ¡Trees ¡
- Rooted ¡trees. ¡ ¡ ¡S ¡at ¡the ¡root. ¡
- Nonterminals ¡at ¡the ¡interior ¡nodes ¡
- Terminals ¡at ¡the ¡leaves ¡
- Shows ¡what ¡was ¡replaced ¡with ¡what: ¡
– Each ¡nonterminal ¡has ¡children ¡which ¡are ¡the ¡ symbols ¡on ¡the ¡right ¡side ¡of ¡some ¡produc6on ¡ – A ¡picture ¡is ¡worth ¡a ¡thousand ¡words... ¡
¡ ¡ ¡ ¡ ¡ ¡
S ¡ ¡aSb ¡| ¡bSa ¡| ¡SS ¡| ¡ab| ¡ba ¡| ¡ε ¡
S ¡ ¡aSb ¡ ¡abSab ¡ ¡abSSab ¡ ¡abbaSab ¡ ¡abbaab ¡ A ¡corresponding ¡deriva6on ¡of ¡abbaab ¡
S ¡ S ¡ b ¡ a ¡ S ¡ a ¡ b ¡ S ¡ S ¡ b ¡ a ¡ ε ¡
A ¡deriva6on ¡tree ¡for ¡abbaab ¡ ¡ ¡ ¡ ¡(also ¡called ¡“parse ¡tree”) ¡
Example: ¡Regular ¡Expressions ¡
S ¡ ¡Ø| ¡ε| ¡a ¡| ¡b ¡| ¡(S+S) ¡| ¡(SS) ¡| ¡(S*) ¡
(a+ab)* ¡ S ¡ a ¡ S ¡ ) ¡ ( ¡ * ¡ S ¡ S ¡ + ¡ ( ¡ ) ¡ S ¡ ( ¡ ) ¡ S ¡ b ¡ a ¡ ((a+(ab))*) ¡
Traverse ¡leaves ¡from ¡ler ¡to ¡right ¡
Example: ¡ProposiBonal ¡Formulae ¡
S ¡ ¡p ¡| ¡q ¡| ¡(~S) ¡| ¡(S ¡^ ¡S) ¡| ¡(S ¡v ¡S) ¡| ¡ε ¡
S ¡ p ¡ ~ ¡ ) ¡ ( ¡ S ¡ S ¡ S ¡ ^ ¡ ( ¡ ) ¡ p ¡ q ¡ S ¡ S ¡ v ¡ ( ¡ ) ¡ (~(p^(q ¡v ¡p))) ¡
Ambiguity ¡
- Consider ¡grammar: ¡ ¡ ¡S ¡ ¡ ¡S-‑S ¡| ¡1 ¡| ¡2 ¡| ¡3 ¡
- Two ¡different ¡parse ¡trees ¡for ¡“3-‑2-‑1”: ¡
S ¡ S ¡ S ¡ S ¡ – ¡ – ¡ S ¡ S ¡ – ¡ S ¡ S ¡ – ¡ S ¡ S ¡ 3 ¡ 2 ¡ 1 ¡ 3 ¡ 2 ¡ 1 ¡ 3–(2–1) ¡ ¡ (3–2)–1 ¡ ¡
Ambiguity ¡
- Original ¡grammar: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡ ¡ ¡S-‑S ¡| ¡1 ¡| ¡2 ¡| ¡3 ¡
- Unambiguous ¡grammar: ¡ ¡ ¡ ¡ ¡S ¡ ¡ ¡S-‑C ¡| ¡1 ¡| ¡2 ¡| ¡3 ¡ ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡C ¡ ¡1 ¡| ¡2 ¡| ¡3 ¡
S ¡ S ¡ – ¡ C ¡ – ¡ S ¡ C ¡ 3 ¡ 2 ¡ 1 ¡ (3–2)–1 ¡ ¡
The ¡grammar ¡forces ¡a ¡parse ¡ corresponding ¡to ¡ ¡ler-‑to-‑right ¡
- evalua6on. ¡
Ambiguity: ¡another ¡example ¡
- Consider ¡grammar: ¡ ¡ ¡S ¡ ¡ ¡S ¡+ ¡S ¡| ¡S ¡x ¡S ¡| ¡a ¡
- Two ¡different ¡parse ¡trees ¡for ¡“a ¡x ¡a ¡+ ¡a”: ¡
S ¡ S ¡ S ¡ S ¡
x ¡
+ ¡ S ¡ S ¡
x ¡
S ¡ S ¡ + ¡ S ¡ S ¡ a ¡ a ¡ a ¡ a ¡ a ¡ a ¡ a ¡x ¡(a ¡+ ¡a) ¡ ¡ (a ¡x ¡a) ¡+ ¡a ¡
Ambiguity ¡
- Original ¡grammar: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡ ¡ ¡S ¡+ ¡S ¡| ¡S ¡x ¡S ¡| ¡a ¡
- Unambiguous ¡grammar: ¡ ¡ ¡ ¡ ¡S ¡ ¡ ¡a ¡+ ¡S ¡| ¡a ¡x ¡S| ¡a ¡
The ¡grammar ¡forces ¡a ¡parse ¡ corresponding ¡to ¡ ¡right-‑to-‑ler ¡evalua6on. ¡ ¡ ¡ ¡ (Not ¡necessarily ¡what ¡we ¡always ¡want. ¡) ¡
S ¡ S ¡
x ¡
a ¡ + ¡ a ¡ S ¡ a ¡ a ¡x ¡a ¡+ ¡a ¡ ¡ ¡ ¡ ¡parses ¡as ¡ ¡ ¡ ¡a ¡x ¡(a ¡+ ¡a) ¡ ¡
To ¡derive: ¡ ¡a ¡x ¡a ¡+ ¡a ¡
Ambiguity, ¡cont. ¡
- a ¡x ¡a ¡+ ¡a ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡want ¡parse ¡to ¡be ¡(a ¡x ¡a) ¡+ ¡a ¡
- a ¡+ ¡a ¡x ¡a ¡
¡want ¡parse ¡to ¡be ¡ ¡a ¡+ ¡(a ¡x ¡a) ¡
- Neither ¡ler-‑to-‑right, ¡nor ¡right-‑to-‑ler. ¡
- Need ¡to ¡incorporate ¡precedence ¡into ¡grammar ¡
S ¡ ¡T ¡| ¡S ¡+ ¡T ¡ ¡ ¡ ¡ ¡/* ¡“+” ¡is ¡highest ¡level ¡grouping ¡*/ ¡ T ¡ ¡F ¡| ¡T ¡x ¡F ¡ ¡ ¡ ¡ ¡/* ¡ ¡ ¡“x” ¡binds ¡most ¡6ghtly ¡*/ ¡ F ¡ ¡a ¡ ¡
- Exercise: ¡ ¡show ¡that ¡a ¡x ¡a ¡+ ¡a, ¡and ¡a ¡+ ¡a ¡x ¡a ¡each ¡
have ¡only ¡one ¡parse, ¡and ¡it ¡is ¡the ¡one ¡that ¡ corresponds ¡to ¡x ¡having ¡precedence ¡over ¡+ ¡
Ambiguity ¡
- Ambiguity ¡cannot ¡always ¡be ¡removed: ¡
– Some ¡CFLs ¡are ¡inherently ¡ambiguous: ¡ ¡Every ¡ grammar ¡for ¡them ¡is ¡ambiguous ¡
- Determining ¡whether ¡a ¡grammar ¡is ¡ambiguous ¡
is ¡a ¡hard ¡problem. ¡ ¡(No ¡algorithm ¡exists!) ¡
- Determining ¡whether ¡L(G) ¡is ¡inherently ¡
ambiguous ¡is ¡also ¡a ¡hard ¡problem. ¡ ¡(No ¡alg!) ¡
InducBve ¡Proofs ¡for ¡CFGs ¡
- Oren ¡by ¡induc6on ¡on ¡length ¡of ¡a ¡deriva6on ¡
- Example, ¡recall ¡grammar ¡G ¡with ¡produc6ons: ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡ ¡ε ¡ ¡| ¡ ¡a ¡| ¡ ¡b ¡ ¡| ¡ ¡aSa ¡ ¡| ¡ ¡bSb ¡ ¡
Claim ¡L(G) ¡= ¡{palindromes} ¡= ¡{w ¡| ¡w ¡= ¡wR} ¡
To ¡prove: ¡ ¡S ¡* ¡ ¡w ¡ ¡if ¡and ¡only ¡if ¡w ¡= ¡wR ¡
(Only ¡if) ¡ ¡ ¡Show ¡that ¡if ¡S ¡* ¡w ¡then ¡w ¡= ¡wR ¡ By ¡induc6on ¡on ¡length ¡of ¡derivaBon ¡
- If ¡S ¡1 ¡w ¡then ¡w=ε=εR ¡or ¡w=a=aR ¡ ¡or ¡w=b=bR. ¡
- Assume ¡for ¡all ¡k ¡< ¡n, ¡that ¡if ¡S ¡k ¡w ¡ ¡then ¡w ¡= ¡wR ¡ ¡
- Let ¡S ¡n ¡w ¡ ¡(with ¡n ¡> ¡1) ¡ ¡and ¡wlog ¡w ¡begins ¡with ¡a ¡
– Then ¡S ¡ ¡aSa ¡n-‑1 ¡ ¡aua ¡= ¡w. ¡ – And, ¡S ¡n-‑1 ¡u ¡ ¡so ¡by ¡I.H., ¡u ¡= ¡uR ¡ – So, ¡wR ¡= ¡(aua)R ¡= ¡(ua)Ra ¡= ¡(auR)a ¡= ¡aua ¡= ¡w. ¡
To ¡prove: ¡ ¡S ¡* ¡ ¡w ¡ ¡if ¡and ¡only ¡if ¡w ¡= ¡wR ¡
(If) ¡ ¡ ¡Show ¡that ¡if ¡w ¡= ¡wR ¡then ¡S ¡* ¡w ¡
By ¡induc6on ¡on ¡length ¡of ¡w ¡
- If ¡|w| ¡< ¡2, ¡then ¡w=ε, ¡w=a, ¡or ¡w ¡= ¡b ¡and ¡S ¡1 ¡w ¡
- Assume ¡for ¡all ¡w ¡with ¡|w|< ¡n, ¡if ¡w ¡= ¡wR ¡ ¡then ¡ ¡S ¡*w ¡
- Let ¡w=wR, ¡ ¡with ¡|w| ¡= ¡n, ¡ ¡and ¡let ¡w ¡= ¡aua ¡with ¡|u| ¡< ¡n. ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡(case ¡that ¡w ¡= ¡bub ¡is ¡analogous) ¡ ¡ ¡ ¡
- Lemma: ¡ ¡u ¡= ¡uR ¡ ¡because ¡aua ¡= ¡(aua)R ¡
- Since ¡|u| ¡< ¡n ¡and ¡u ¡= ¡uR, ¡by ¡IH, ¡S* ¡u, ¡ ¡
- But ¡then ¡ ¡S ¡ ¡aSa ¡* ¡aua ¡= ¡w ¡
S ¡ ¡aSb ¡| ¡bSa ¡| ¡SS ¡| ¡ε ¡ L(G) ¡= ¡{w ¡| ¡#a(w) ¡= ¡#b(w) ¡} ¡
Proof ¡
- must ¡prove ¡both ¡ ¡ ¡ ¡ ¡ ¡ ¡and ¡ ¡
- one ¡direc6on ¡is ¡easier ¡(which)? ¡
- If ¡S ¡* ¡w ¡ ¡then ¡#a(w) ¡= ¡#b(w) ¡ ¡
– easy ¡induc6on ¡for ¡you ¡to ¡do. ¡ ¡(Induc6on ¡on ¡what?) ¡ ¡
⊆ ⊇
Another ¡example ¡
S ¡ ¡aSb ¡| ¡bSa ¡| ¡SS ¡| ¡ε ¡
- If ¡|w| ¡= ¡0, ¡done. ¡
- Assume ¡for ¡all ¡w ¡of ¡length ¡< ¡n ¡that.... ¡
- Let ¡|w| ¡= ¡n, ¡with ¡#a(w) ¡= ¡#b(w) ¡ ¡
– Case ¡1a: ¡ ¡ ¡w ¡= ¡aub. ¡ ¡Then ¡ ¡#a(u) ¡= ¡#b(u) ¡ ¡(why?) ¡and ¡... ¡ – Case ¡1b: ¡ ¡ ¡w ¡= ¡bua. ¡ ¡Same. ¡ – Case ¡2a: ¡ ¡ ¡w ¡= ¡aua. ¡ ¡ ¡
- Then ¡similar ¡to ¡HW ¡1, ¡w ¡can ¡be ¡par66oned ¡w ¡= ¡xy, ¡with ¡
#a(x) ¡= ¡#b(x) ¡ ¡and ¡#a(y)= ¡#b(y) ¡ ¡
- Then ¡S ¡ ¡SS ¡* ¡xy ¡ ¡(by ¡induc6ve ¡hypothesis)... ¡
If ¡#a(w) ¡= ¡#b(w) ¡then ¡S ¡* ¡w ¡ ¡ ¡
Tricks ¡for ¡Grammar ¡ConstrucBon ¡
- Repeated ¡characters: ¡
¡ ¡ ¡ ¡ ¡ ¡ ¡A ¡ ¡aA ¡| ¡ε ¡
- In ¡equal ¡numbers ¡
¡ ¡ ¡ ¡A ¡ ¡aAb ¡| ¡ε ¡
- Break ¡into ¡cases ¡/ ¡unions ¡ ¡ ¡ ¡
– L ¡= ¡L1 ¡ ¡U ¡ ¡L2 ¡ – S ¡ ¡S1 ¡| ¡S2 ¡ ¡ ¡but ¡make ¡sure ¡G1 ¡and ¡G2 ¡are ¡disjoint. ¡
Cases... ¡
L ¡= ¡{0n1m|n≠m} ¡ ¡= ¡{0n1m|n ¡< ¡m} ¡ ¡U ¡ ¡{0n1m|n ¡> ¡m} ¡ ¡ ¡ ¡ ¡ ¡ S1 ¡ ¡0S11 ¡| ¡A1 ¡ A ¡ ¡ ¡A1 ¡| ¡ε ¡ S2 ¡ ¡0S21 ¡| ¡0B ¡ B ¡ ¡ ¡0B ¡| ¡ε ¡
Normal ¡Forms ¡
- Convenient ¡if ¡we ¡restrict ¡form ¡of ¡produc6on ¡
rules ¡in ¡various ¡ways ¡
– Can ¡limit ¡number ¡of ¡produc6ons ¡used ¡in ¡ deriva6on ¡ – Can ¡balance ¡deriva6on ¡tree ¡ – Some6mes ¡makes ¡parsing ¡easier ¡ – Make ¡some ¡proofs ¡easier ¡
Normal ¡Forms ¡
- Chomsky ¡Normal ¡Form ¡
– Only ¡produc6ons ¡of ¡form ¡A ¡ ¡BC ¡or ¡A ¡ ¡a ¡ ¡are ¡allowed ¡
- If ¡ε ¡is ¡in ¡L, ¡then ¡S ¡ ¡ε ¡is ¡also ¡allowed ¡
– All ¡CFLs ¡have ¡a ¡grammar ¡in ¡CNF. ¡ – Proof ¡is ¡not ¡difficult: ¡ ¡convert ¡an ¡arbitrary ¡CFG ¡into ¡
- ne ¡in ¡CNF ¡– ¡involves ¡four ¡different ¡steps ¡
- Greibach ¡Normal ¡Form ¡