1.02k likes | 1.22k Views
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
E N D
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 • FactorTerm Jeff Edmonds York University • Factor • Factor • 3 • 7 • y • x • COSC 4111 Lecture4
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.
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.
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, …
Context Free Grammars • Which strings the does the following grammar generate? • SADB S or or • Aa • BbB 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.
Chomsky Hierarchy 1956
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: • CaBc • Context does not matter. • Context Sensitive Grammar: • dCcaBcc • Context does matter. • We will allow shortening rules • dCcaB • This allows the grammar to simulate a TM. 0n1n2n 0n1n 0*1*
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*
Ambiguity • Give a simple grammar for expressions with + • eg a = 01+1 • Rule S 0 | 1 | S+S | SS • 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.
Ambiguity • Give a simple grammar for expressions with + • eg a = 01+1 exp term • exp • exp • term • term • + • fact • term • 0 • • fact • fact • 1 • 0
Ambiguity • Give a simple grammar for expressions with + • eg a = 01+1 exp • exp term • term • term • + Not really a legal Context Free Grammar • fact • 0 • • fact • fact • 1 • 0
Ambiguity • Give a simple grammar for expressions with + • eg a = 01+1
Java like Example • Op • If • if (Boolean) then Op • Equ=Equ • Term • Term+Equ • Term • Factor • FactorTerm • Factor • Factor • 3 • 7 • y • x
Java like Example • Op • If • if (Boolean) then Op • While(Boolean) Op • y=3x7 • i<10 • i=0 • y=(y+10)+5 • While • i=i+1 • { Ops } • { Ops } • Op • OpOps • { • } • Sorry the programmer forgot the { }. • ??? • ???
Java like Example • Op • If • if (Boolean) then Op • While(Boolean) Op • y=3x7 • i<10 • i=0 • While • i=i+1 • { Ops } • Op • Op • OpOps • Ops • Where does the second Op go? • ??? • y=(y+10)+5
Java like Example • Op • Ops • { Ops } • Op • OpOps • 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.
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.
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.
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.
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.
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
Union, Concat, Spew & Plop • Grammar for unionL1 L2: • SS1 S2 S S or S2 S1 L1 L2
Union, Concat, Spew & Plop • Grammar for concatenationL1 L2: • SS1S2 S S1 S2 L1 L2
Union, Concat, Spew & Plop • Grammar for spew & plop an # bn: • Spew: SaSb • Plop: S # S aSb aaSbb aaaSbbb aaaaSbbbb aaaaaSbbbbb aaaaa#bbbbb
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.
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}*
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
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
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
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
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
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
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)
Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Grammar for S an bmcndm Linked because must be the same size.
Union, Concat, Spew & Plop • Step 1: Look for links and concatenations Plop Grammar for S an bmcndm Spew Not linked
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)
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!
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!
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 | #
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!
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
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.
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
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
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
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.
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,yL, f(x,y)L. • x+1 • x+y • xy • x/y (y0) • Integers • x,y
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,yL, f(x,y)L. • x+1 • x+y • xy • x/y (y0) • Integers • x,y
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,yL, 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
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!