1 / 102

Jeff Edmonds York University

2001 Context Free Grammars.  Op . Context Free Grammars Chomsky Hierarchy Ambiguity Java Like Example Human Languages Union, Concat , Spew, & Plop NFA to Context Free Grammar Closure and Proof Big Enough Parsing/Compiling Chomsky Normal Form Context Sensitive Grammars

Download Presentation

Jeff Edmonds York University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2001 Context Free Grammars • Op • Context Free Grammars • Chomsky Hierarchy • Ambiguity • Java Like Example • Human Languages • Union, Concat, Spew, & Plop • NFA to Context Free Grammar • Closure and Proof Big Enough • Parsing/Compiling • Chomsky Normal Form • Context Sensitive Grammars • TM (accept) to CSG (generate) • If • if (Boolean) then Op • Equ=Equ • Term • Term+Equ • Term • Factor • FactorTerm Jeff Edmonds York University • Factor • Factor • 3 • 7 • y • x • COSC 4111 Lecture4

  2. Context Free Grammars Context Free Grammar: To be more legal exp term Generates Strings: Generates Language/Computational Problem: Grammar generates string iff string is in the language iff Computational Problem says Yes.

  3. Context Free Grammars Context Free Grammar: To be more legal exp term Generates Strings: The Parse Tree: • Adds “meaning” to the string. • Can be used to evaluate/process the string. • Mircosoft Word underlines poor grammar with green. • Used in a compiler to produce machine code.

  4. Context Free Grammars • Context Free Grammar: • Terminals: a,b,c,… characters in the final string being generated. • NonTerminals: A,B,C,… characters that generate more substrings. • Rules: A  aAb  cBccBcc At any time, you can replace A with aAb • Which rule to use is chosen nondeterministically. • Start Symbol: S • Stop when no more nonterminals in your string. • Examples: • English, JAVA, …

  5. Context Free Grammars • Which strings the does the following grammar generate? • SADB S or or • Aa • BbB bB a B D A bbB bbbB This grammar only generates the one string a. bbbbB bbbbbB • Start with the start symbol, S. • You can think of choosing which rule to apply Non-Deterministically. • If all characters in the string become terminal symbols, then this string is generated • If the process ends because no rule can be applied then this process fails to generate a string. • or if it continues forever.

  6. Chomsky Hierarchy 1956

  7. Chomsky Hierarchy • Computable/Decidable(Turing Machines) • Regular Languages • (DFA, NFA, Regular Expressions) • Context Free Grammars(Push Down Automata) • Context Sensitive Grammars • (allow shortening rule) • Context Free Grammar: • CaBc • Context does not matter. • Context Sensitive Grammar: • dCcaBcc • Context does matter. • We will allow shortening rules • dCcaB • This allows the grammar to simulate a TM. 0n1n2n 0n1n 0*1*

  8. Chomsky Hierarchy • Computable/Decidable(Turing Machines) • Regular Languages • (DFA, NFA, Regular Expressions) • Context Free Grammars(Push Down Automata) • Context Sensitive Grammars • (allow shortening rule) On Yes instances, TM must halt and accept. On No instances may run forever. • Acceptable/Enumerable (Turing Machines) Halting Problem D B A a The TM must halt on every input and give the correct answer. 0n1n2n S or or bB 0n1n bbB bbbB bbbbB On Yes instances, grammar must halt and accept. On No instances may run forever or halt with nonterminials. bbbbbB 0*1*

  9. Ambiguity • Give a simple grammar for expressions with +  • eg a = 01+1 • Rule S  0 | 1 | S+S | SS • Parse Tree: • S • S • S • S •  • S • S • + • 0 • S • S • + • S • 1 • S •  • 1 • 1 • 0 • 1 • A grammar is ambiguous if some strings are derivedwith two different parse trees. • This can lead to different interpretations of the string.

  10. Ambiguity • Give a simple grammar for expressions with +  • eg a = 01+1 exp term • exp • exp • term • term • + • fact • term • 0 •  • fact • fact • 1 • 0

  11. Ambiguity • Give a simple grammar for expressions with +  • eg a = 01+1 exp • exp term • term • term • + Not really a legal Context Free Grammar • fact • 0 •  • fact • fact • 1 • 0

  12. Ambiguity • Give a simple grammar for expressions with +  • eg a = 01+1

  13. Java like Example • Op • If • if (Boolean) then Op • Equ=Equ • Term • Term+Equ • Term • Factor • FactorTerm • Factor • Factor • 3 • 7 • y • x

  14. Java like Example • Op • If • if (Boolean) then Op • While(Boolean) Op • y=3x7 • i<10 • i=0 • y=(y+10)+5 • While • i=i+1 • { Ops } • { Ops } • Op • OpOps • { • } • Sorry the programmer forgot the { }. • ??? • ???

  15. Java like Example • Op • If • if (Boolean) then Op • While(Boolean) Op • y=3x7 • i<10 • i=0 • While • i=i+1 • { Ops } • Op • Op • OpOps • Ops • Where does the second Op go? • ??? • y=(y+10)+5

  16. Java like Example • Op • Ops • { Ops } • Op • OpOps • y = (3*x+7)*53 • z = ((2*3*4+5+6)+7 • From Op, this grammar cannot generate more than one operation without them being in brackets. • From Ops, no problem.

  17. Java like Example • Op • If • IfElse • if (Boolean) then Op else Op • if (Boolean) then Op • i=y • i=x • i=0 • y=7 • There is a second parsing.

  18. Java like Example • Op • If • IfElse • if (Boolean) then Op else Op • if (Boolean) then Op • i=y • i=x • i=0 • y=7 • { • { • } • } • There is a second parsing. • Again brackets help.

  19. Human Languages • Number of rules:<SENTENCE>  <NOUN-PHRASE><VERB-PHRASE> <NOUN-PHRASE>  <CMPLX-NOUN> | <CMPLX-NOUN><PREP-PHRASE><VERB-PHRASE>  <CMPLX-VERB> | <CMPLX-VERB><PREP-PHRASE> • <PREP-PHRASE>  <PREPOSITION><CMPLX-NOUN> <CMPLX-NOUN>  <ARTICLE><NOUN> <CMPLX-VERB>  <VERB> | <VERB><NOUN-PHRASE> … • <ARTICLE>  a | the • <PREPOSITION>  with<NOUN>  boy | girl | flower<VERB>  sees | hits • Possible element: • The girl hit the boy with the flower.

  20. Human Languages • A grammar is ambiguous if some strings are derivedwith two different parse trees. • This can lead to different interpretations of the string. • Possible element: • The girl hit the boy with the flower.

  21. Union, Concat, Spew & Plop • All a CFG can do is • union, • concatenate, • and spew & plop. • spew: to vomit, to ooze out, to come forth in a flood or gush with violence or in great quantity. • plop: to drop suddenly with a sound like that of something dropping into water

  22. Union, Concat, Spew & Plop • Grammar for unionL1 L2: • SS1  S2 S S or S2 S1 L1 L2

  23. Union, Concat, Spew & Plop • Grammar for concatenationL1 L2: • SS1S2 S S1 S2 L1 L2

  24. Union, Concat, Spew & Plop • Grammar for spew & plop an # bn: • Spew: SaSb • Plop: S # S aSb aaSbb aaaSbbb aaaaSbbbb aaaaaSbbbbb aaaaa#bbbbb

  25. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Grammar for S  {0,1}* an bmc2m+3dn{0,1}* Linked because must be the same size n. Linked because must be the double the size. Not Linked because * means anything.

  26. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Extra concatenated on Extra concatenated on Extra stuck in Grammar for S  {0,1}* an bmc2m+3dn{0,1}*

  27. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • Step 2: Give names to the parts. • Step 3: Build Grammar rules. • Step 4: Recurse. A Q A Grammar for S  {0,1}* an bmc2m+3dn{0,1}* • Concatenate: S  AQA

  28. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • Step 2: Give names to the parts. • Step 3: Build Grammar rules. • Step 4: Recurse. Grammar for A  {0,1}i Spew C A CA • Spew: A  CA CCA (empty string) • Plop: A  ε CCCA CCCCA CCCCCA CCCCC

  29. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • Step 2: Give names to the parts. • Step 3: Build Grammar rules. Grammar for C  {0,1} Union • Union: C  0 | 1 CCCCC 1 0 0 1 0

  30. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations. Grammar for Q  an bmc2m+3dn Linked because must be the same size n. Linked because must be the double the size. • Which to spew first a&d or b&c

  31. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • Step 2: Give names to the parts. • Step 3: Build Grammar rules. • Step 4: Recurse. Grammar for Q  an bmc2m+3dn Q T aQd aaQdd aaaQddd aaaaQdddd • Spew: Q  aQd aaaaaQddddd • Plop: Q  Tccc aaaaaTcccddddd

  32. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • Step 2: Give names to the parts. • Step 3: Build Grammar rules. Grammar for T  bmc2m Q aaaaaTcccddddd aQd aaQdd aaaQddd aaaaQdddd • Spew: T  bTcc aaaaaQddddd • Plop: T  ε aaaaabTcccccddddd aaaaabbTcccccccddddd aaaaabbbTcccccccccddddd aaaaabbbcccccccccddddd

  33. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • Step 2: Give names to the parts. • Step 3: Build Grammar rules. Grammar for S  {0,1}* an bmc2m+3dn{0,1}* T A A Q S  AQA (concatenate) S  A  {0,1}* A  CA | ε (spew & plop) C  {0,1} C  0 | 1 (union) Q  an bmc2m+3dn Q  aQd | Tccc (spew & plop) T  bTcc | ε T  bmc2m (spew & plop)

  34. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Grammar for S  an bmcndm Linked because must be the same size.

  35. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Plop Grammar for S  an bmcndm Spew Not linked

  36. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Plop Grammar for S  an bmcndm Not linked Spew Links over lap and hence can’t be done by a CFG! Pumping Lemma (ugly)

  37. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Grammar for S  an bncn Linked because must be the same size. Links over lap and hence can’t be done by a CFG!

  38. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Grammar for S  α1 α2… αn # α1 α2… αn … Linked because must be the same string. Links over lap and hence can’t be done by a CFG!

  39. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Grammar for S  α1 α2… αn # αn αn-1… α1 … Linked because must be the same string. Links do not over lap and hence can be done by a CFG! S  0S0 | 1S1 | #

  40. Union, Concat, Spew & Plop • Step 1: Look for links and concatenations • S  • begin Alg • x = 5 • begin loop • x = ( [ { 3 } ] ) • exit loop • return x • end Alg Linked This is what CFG is really good at!

  41. NFA to Context Free Garammar • Context Free Grammar • DFA=NFA exp term  term + exp • We will give an algorithm • GCFG = NFAtoCFG(MNFA)that converts an NFA into a context free grammar • such that L(GCFG) = L(MNFA). Languages which have a context free grammar to generate them. • =Extended Regular • =Regular • L

  42. NFA to Context Free Garammar • a = 0001010 • We say a string a is accepted iff there is an path through the NFAlabeled by the stringending at an accept state.

  43. NFA to Context Free Garammar • 0 • 0 • 0 • a = 0001010 • start • 1 • 0 • 1 • 0 S Q1 0Q1 00Q1 000Q2 0001Q3 If a partial string αhas a path to state qi, then the grammar can generate string S αQi 00010Q4 000101Q5 0001010Q5

  44. NFA to Context Free Garammar • 0 • 0 • 0 • a = 0001010 • start • 1 • 0 • 1 • 0 S a  L Q1 0Q1 S  Q1 Q1  0Q1 | 1Q1 | 0Q2 Q2  1Q3 Q3  0Q4 Q4  1Q5 Q5  0Q5 | 1Q5 | 0Q2 00Q1 000Q2 0001Q3 00010Q4 000101Q5 0001010Q5 |  0001010

  45. Closure and Proof of Big Enough L = { α {a,b}* | #a’s in α = #b’s in α } Goal: Build a CFG that generates this language. Before we cared what the “parsing” looks like. • S • S • S • S •  • S • S • + • 0 • S • S • + • S • 1 • S •  • 1 • 0 • 1

  46. Closure and Proof of Big Enough L = { α {a,b}* | #a’s in α = #b’s in α } Goal: Build a CFG that generates this language. Now we only care which strings are generated. Throw in any grammar rules that does not add unallowed strings. Later we try to prove that every allowed string can be generated.

  47. Closure and Proof of Big Enough L = { α {a,b}* | #a’s in α = #b’s in α } The concept of closure can be helpful. A set L is said to be closed under an operation iff x,yL, f(x,y)L. • x+1 • x+y • xy • x/y (y0) • Integers • x,y

  48. Closure and Proof of Big Enough L = { α {a,b}* | #a’s in α = #b’s in α } The concept of closure can be helpful. A set L is said to be closed under an operation iff x,yL, f(x,y)L. • x+1 • x+y • xy • x/y (y0) • Integers • x,y

  49. Closure and Proof of Big Enough L = { α {a,b}* | #a’s in α = #b’s in α } The concept of closure can be helpful. A set L is said to be closed under an operation iff x,yL, f(x,y)L. • 1/2 • Suppose you want to make the integers. • Start by adding zero • Close it under successorie if x is in the set, then add x+1 • Close it under additionie if x&y are in the set add x+y • This does not add nonintegers • Neither does it add new integers. • Close it under division ie if x&y are in the set add x/y • This adds nonintegers 1,2,3,4,… 0

  50. Closure and Proof of Big Enough • L = { α {a,b}* | #a’s in α = #b’s in α } • The concept of closure can be helpful. • What operations is our L closed under? αL, what bigger string is in L? • aαb, bαa, abα, baα, αab, αba  L • What is the grammar rule that adds these? • S  aSb | bSa | abS | baS | Sba | Sab • What strings does this generate? • None!

More Related