cfgs and cfls context free grammars
play

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


  1. CFGs ¡and ¡CFLs ¡

  2. Context-­‑Free ¡Grammars ¡ • Programming ¡language ¡specifica6on ¡ • Parsing ¡ • Natural ¡language ¡understanding ¡ • Markup ¡languages ¡ • A ¡genera6ve ¡model ¡giving ¡structure ¡

  3. Programming ¡Languages ¡

  4. Natural ¡Language ¡Systems ¡

  5. 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/ ¡

  6. Models ¡of ¡Growth ¡ • L-­‑systems ¡ • h>p://www.kevs3d.co.uk/dev/lsystems/ ¡

  7. Miscellaneous ¡

  8. Kolam ¡drawing ¡generated ¡by ¡picture ¡ grammar ¡

  9. 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 ¡

  10. Example: ¡ ¡G ¡= ¡(V, ¡T, ¡P, ¡S), ¡where... ¡ • V ¡= ¡{S} ¡ • T ¡= ¡{a,b} ¡ • P ¡= ¡{ ¡S ¡  ¡ ε ¡ ¡| ¡ ¡a ¡| ¡ ¡b ¡ ¡| ¡ ¡aSa ¡ ¡| ¡ ¡bSb ¡} ¡ ¡ ¡(abbrevia6on ¡for ¡S  ε, ¡S  a, ¡S  b, ¡...) ¡ • S ¡= ¡start ¡symbol ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡S ¡  ¡ aSa ¡  ¡ abSba ¡  ¡ abbSbba ¡  ¡ abbbba ¡ ¡ ¡What ¡strings ¡can ¡S ¡generate ¡like ¡this? ¡

  11. 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 ¡

  12. 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 ¡

  13. “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 ¡ ¡ ¡ ¡ ¡ ¡

  14. “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 ¡ ¡

  15. 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 } ¡

  16. 2 ¡– ¡minute ¡challenge ¡ Create ¡a ¡grammar ¡that ¡generates ¡{0 n 1 n |n ¡≥ ¡0} ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ S ¡  ¡ 0 S 1 ¡ | ¡ε ¡ 5 ¡– ¡minute ¡challenge ¡ Create ¡a ¡grammar ¡that ¡generates ¡{0 n 1 m |n≠m} ¡

  17. 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 ¡ | ¡bSa ¡ | ¡SS ¡ | ¡ab | ¡ba ¡ | ¡ε ¡ A ¡ deriva6on ¡of ¡ abbaab ¡: ¡ ¡ S ¡  ¡ aSb ¡  ¡ abSab ¡  ¡ abSSab ¡  ¡ abbaSab ¡  ¡ abbaab ¡

  18. 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... ¡ ¡ ¡ ¡ ¡ ¡ ¡

  19. S ¡ A ¡deriva6on ¡tree ¡for ¡ abbaab ¡ ¡ ¡ ¡ ¡ (also ¡called ¡“parse ¡tree”) ¡ a ¡ S ¡ b ¡ b ¡ S ¡ a ¡ S ¡  ¡aSb ¡ | ¡bSa ¡ | ¡SS ¡ | ¡ab | ¡ba ¡ | ¡ε ¡ S ¡ S ¡ A ¡ corresponding ¡deriva6on ¡of ¡ abbaab ¡ b ¡ a ¡ ε ¡ S ¡  ¡ aSb ¡  ¡ abSab ¡  ¡ abSSab ¡  ¡ abbaSab ¡  ¡ abbaab ¡

  20. Example: ¡Regular ¡Expressions ¡ S ¡  ¡ Ø| ¡ε| ¡ a ¡ | ¡ b ¡ | ¡( S + S ) ¡| ¡( SS ) ¡| ¡( S *) ¡ S ¡ ( ¡ S ¡ * ¡ ) ¡ Traverse ¡leaves ¡from ¡ler ¡to ¡right ¡ (( a +( ab ))*) ¡ ( ¡ + ¡ ) ¡ S ¡ S ¡ ( a + ab )* ¡ S ¡ ( ¡ S ¡ ) ¡ a ¡ a ¡ b ¡

  21. Example: ¡ProposiBonal ¡Formulae ¡ S ¡  ¡ p ¡ | ¡ q ¡ | ¡(~ S ) ¡| ¡( S ¡ ^ ¡ S ) ¡| ¡( S ¡ v ¡ S ) ¡| ¡ε ¡ S ¡ ( ¡ ~ ¡ S ¡ ) ¡ ( ¡ ^ ¡ ) ¡ S ¡ S ¡ (~( p ^( q ¡ v ¡ p ))) ¡ ( ¡ v ¡ ) ¡ S ¡ S ¡ p ¡ p ¡ q ¡

  22. 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 ¡ 1 ¡ 3 ¡ 2 ¡ 1 ¡ 2 ¡ 3–(2–1) ¡ ¡ (3–2)–1 ¡ ¡

  23. Ambiguity ¡ • Original ¡grammar: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ S ¡  ¡ ¡ S-­‑S ¡ | ¡ 1 ¡| ¡ 2 ¡| ¡ 3 ¡ • Unambiguous ¡grammar: ¡ ¡ ¡ ¡ ¡ S ¡  ¡ ¡ S-­‑ C ¡| ¡1 ¡| ¡2 ¡| ¡3 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ C ¡  ¡ 1 ¡| ¡ 2 ¡| ¡ 3 ¡ S ¡ – ¡ S ¡ C ¡ The ¡grammar ¡forces ¡a ¡parse ¡ corresponding ¡to ¡ ¡ler-­‑to-­‑right ¡ S ¡ – ¡ C ¡ 1 ¡ evalua6on. ¡ 3 ¡ 2 ¡ (3–2)–1 ¡ ¡

  24. Ambiguity: ¡another ¡example ¡ • Consider ¡grammar: ¡ ¡ ¡ S ¡  ¡ ¡ S ¡ + ¡ S ¡ | ¡S ¡ x ¡ S ¡| ¡a ¡ • Two ¡different ¡parse ¡trees ¡for ¡“ a ¡ x ¡ a ¡ + ¡ a”: ¡ S ¡ S ¡ x ¡ S ¡ S ¡ S ¡ S ¡ + ¡ S ¡ S ¡ + ¡ S ¡ S ¡ a ¡ x ¡ a ¡ a ¡ a ¡ a ¡ a ¡ a ¡ x ¡ ( a ¡+ ¡ a ) ¡ ¡ ( a ¡ x ¡ a ) ¡+ ¡ a ¡

  25. Ambiguity ¡ • Original ¡grammar: ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ S ¡  ¡ ¡ S ¡ + ¡ S ¡ | ¡S ¡ x ¡ S ¡ | ¡ a ¡ • Unambiguous ¡grammar: ¡ ¡ ¡ ¡ ¡ S ¡  ¡ ¡ a ¡+ ¡ S ¡| ¡ a ¡ x ¡ S | ¡ a ¡ S ¡ To ¡derive: ¡ ¡ a ¡ x ¡ a ¡+ ¡ a ¡ x ¡ a ¡ S ¡ The ¡grammar ¡forces ¡a ¡parse ¡ corresponding ¡to ¡ ¡right-­‑to-­‑ler ¡evalua6on. ¡ ¡ ¡ ¡ a ¡ S ¡ + ¡ (Not ¡necessarily ¡what ¡we ¡always ¡want. ¡) ¡ a ¡ a ¡ x ¡ a ¡ + ¡ a ¡ ¡ ¡ ¡ ¡parses ¡as ¡ ¡ ¡ ¡a ¡ x ¡ ( a ¡+ ¡ a ) ¡ ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend