foundations of computer science lecture 25 context free
play

Foundations of Computer Science Lecture 25 Context Free Grammars - PowerPoint PPT Presentation

Foundations of Computer Science Lecture 25 Context Free Grammars (CFGs) Solving a Problem by Listing Out the Language Rules for CFGs Parse Trees Pushdown Automata Last Time DFAs: State transitioning machines which can be implemented


  1. Foundations of Computer Science Lecture 25 Context Free Grammars (CFGs) Solving a Problem by “Listing Out” the Language Rules for CFGs Parse Trees Pushdown Automata

  2. Last Time DFAs: State transitioning machines which can be implemented using basic technology. Powerful: can solve any regular expression. (Finite sets, complement, union, intersection, concatenation, Kleene-star). Computing Model Analyze Model Do we need a Rules to: 1. Capabilities: what can be solved? 1. Construct machine; new model? 2. Limitations: what can’t be solved? 2. Solve problems. DFAs fail at so simple a problem as equality. That’s not acceptable. We need a more powerful machine. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 2 / 16 Adding Memory →

  3. Adding Memory DFAs have no scratch paper. It’s hard to compute entirely in your head. Stack Memory. Think of a file-clerk with a stack of papers. 0 0 0 0 0 1 1 1 1 1 The clerk’s capabilities: see the top sheet; remove the top sheet ( pop ) q 0 q 7 q 1 yes push something new onto the top of the stack. or q 6 q 2 0 no access to inner sheets without removing top. q 5 q 3 no q 4 DFA with a stack is a pushdown automaton (PDA) How does the stack help to solve { 0 • n 1 • n | n ≥ 0 } ? 1: When you read in each 0, write it to the stack. 2: For each 1, pop the stack. At the end if the stack is empty, accept . The memory allows the automaton to “remember” n . Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 3 / 16 Today →

  4. Today: Context Free Grammars (CFGs) Solving a problem by listing out the language. 1 Rules for Context Free Grammars (CFG). 2 Examples of Context Free Grammars. 3 English. Programming. Proving a CFG solves a problem. 4 Parse Trees. 5 Pushdown Automata and non context free languages. 6 Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 4 / 16 Recursively Defined Language →

  5. Recursively Defined Language: Listing a Language. L 0 n 1 n = { 0 • n 1 • n | n ≥ 0 } 1 ε ∈ L 0 n 1 n . [basis] 2 x ∈ L 0 n 1 n → 0 • x • 1 ∈ L 0 n 1 n . [constructor rule] 3 Nothing else is in L 0 n 1 n . [minimality] To test if 0010 ∈ L 0 n 1 n : generate strings in order of length and test each for a match: ε → 01 → 0011 → 000111 no A Context Free Grammar is like a recursive definition.   1: S → ε  ε ∈ L 0 n 1 n     2: S → 0 S 1 x ∈ L 0 n 1 n → 0 • x • 1 ∈ L 0 n 1 n  Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

  6. Rules for Context Free Grammars (CFGs) Production rules of the CFG: 1: S → ε 2: S → 0 S 1 Each production rule has the form → variable expression ↑ ↑ P, Q, R, S, T, . . . string of variables and terminals 2: 2: 2: 2: 2: · · · S 0 S 1 00 S 11 000 S 111 0000 S 1111 1: 1: 1: 1: 1: ε 01 0011 000111 00001111 1: Write down the start variable (form the first production rule, typically S ). 2: Replace one variable in the current string with the expression from a pro- duction rule that starts with that variable on the left. 3: Repeat step 2 until no variables remain in the string. “Replace variable with expression , no matter where (independent of context)” Shorthand: 1: S → ε | 0 S 1 Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  7. Language of Equality, cfg bal 1: S → ε | 0 S 1 S | 1 S 0 S cfg bal A derivation of 0110 in cfg bal (each step is called an inference). ∗ 1: 1: 1: ⇒ 0110 S ⇒ 0 S 1 S ⇒ 0 S 11 S 0 S ⇒ 0 ε 11 S 0 S Notation: S ∗ ( ∗ ⇒ 0110 ⇒ means “A derivation starting from S yields 0110”) Distinguish S from a mathematical variable (e.g. x ), 0 S 1 S versus 0 x 1 x Two S ’s are replaced independently. Two x ’s must be the same, e.g. x = 11 . Pop Quiz. Determine if each string can be generated and if so, give a derivation. 1: S → ε | T 0 T 1 | T 0 A 0011 (a) 2: X → T 0 T 1 | T 0 A 0110 (b) 3: A → XT 1 00011 (c) 4: T 0 → 0 010101 (d) 5: T 1 → 1 Give an informal description for the CFL of this CFG. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 7 / 16 A CFG for English →

  8. A CFG for English → <phrase><verb> S 1: <phrase> → <article><noun> 2: 3: <article> → A ␣ | The ␣ <noun> → cat ␣ | dog ␣ 4: <verb> → walks. | runs. | walks. ␣ S | runs. ␣ S 5: 1 : ⇒ <phrase><verb> S 5 : ⇒ <phrase> walks. 2 : ⇒ <article><noun> walks. 3 : ⇒ A ␣<noun> walks. 4 : ⇒ A ␣ cat ␣ walks. Pop Quiz. Give a derivation for: A ␣ cat ␣ runs. ␣ The ␣ dog ␣ walks. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 8 / 16 A CFG for Programming →

  9. A CFG for Programming → <stmt>; S | <stmt>; S 1: <stmt> → <assign> | <declare> 2: <declare> → int␣<variable> 3: <assign> → <variable>=<integer> 4: <integer> → <integer><digit> | <digit> 5: <digit> → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 6: 7: <variable> → x | x<variable> Pop Quiz. Give derivations for these snippets of code. (a) int␣x;int␣xx;x=22;xx=8; (b) x=8;int␣x; int␣x;xx=8; (c) Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 9 / 16 Constructing a CFG →

  10. Constructing a CFG to Solve a Problem L bal = { strings with an equal number of 1’s and 0’s } . 001011010110 = 0 • 0101 • 1 • 010110 ↑ ↑ in L bal in L bal = 0 S 1 S ( S represents “a string in L bal ”) Every large string in L bal can be obtained (recursively) from smaller ones. S → ε | 0 S 1 S | 1 S 0 S. We must prove that: Every string generated by this CFG is in L bal ? (i) Every string in L bal can be derived by this grammar? (ii) Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 10 / 16 Proving a CFG Solves a Problem →

  11. Proving a CFG Solves a Problem L bal = { strings with an equal number of 1’s and 0’s } S → ε | 0 S 1 S | 1 S 0 S (i) Every derivation in cfg bal generates a string in L bal . Strong induction on the length of the derivation (number of production rules invoked). Base Case. length-1 derivation gives ε . Induction. The derivation starts in one of two ways: S → 0 S 1 S → · · · or S → 1 S 0 S → · · · ⇓∗ ⇓∗ ⇓∗ ⇓∗ x y x y The derivations of x and y are shorter. By the induction hypothesis, x, y ∈ L bal , so the final strings are in L bal . (ii) Every string in L bal can be derived within cfg bal . Strong induction on the length of the string. Base case: length-1 string, ε . Induction. Any string w in L bal has one of two forms: w = 0 w 1 1 w 2 or w = 1 w 1 0 w 2 , where w 1 , w 2 ∈ L bal and have smaller length. By the induction hypothesis, S ∗ ⇒ w 1 and S ∗ ⇒ w 2 , so S ∗ ⇒ w . Practice. Exercise 25.5. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 11 / 16 Union, Concatenation, Kleene-star →

  12. Union, Concatenation, Kleene-star L 1 = { 0 • n 1 • n | n ≥ 0 } L 2 = { 1 • n 0 • n | n ≥ 0 } A → ε | 0 A 1 B → ε | 1 B 0 L 1 ∪ L 2 : 1: S → A | B L 1 • L 2 : 1: S → AB 2: A → ε | 0 A 1 2: A → ε | 0 A 1 3: B → ε | 1 B 0 3: B → ε | 1 B 0 Kleene-star. L ∗ 1 is generated by the CFG 1: S → ε | SA ← generates A • i 2: A → ε | 0 A 1 ← each A becomes a 0 • n 1 • n Example 25.2. CFGs can implement DFAs, and so are strictly more powerful. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 12 / 16 Parse Trees →

  13. Parse Trees S → # | 0 S 1 Here is a derivation of 000 # 111 , S ⇒ 0 S 1 ⇒ 00 S 11 ⇒ 000 S 111 ⇒ 000 # 111 The parse tree gives more information than a derivation ⇒ ⇒ ⇒ ⇒ S S S S S S 0 1 0 1 0 1 0 1 S S S S S 0 S 1 0 S 1 0 S 1 S 0 1 0 1 S S S 0 0 0 1 1 1 # # Clearly shows how a substring belongs to the language of its parent variable. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 13 / 16 CFG for Arithmetic →

  14. CFG for Arithmetic S → S + S | S × S | ( S ) | 2 (the terminals are + , × , (, ) and 2 ) Two derivations of 2 + 2 × 2 along with parse trees, ∗ ∗ S ⇒ S + S ⇒ S + S × S ⇒ 2 + 2 × 2 S ⇒ S × S ⇒ S + S × S ⇒ 2 + 2 × 2 S S + × S S S S × + 2 2 S S S S 2 2 2 2 (multiply 2 × 2 and add to 2) (add 2 + 2 and multiply by 2) Parse tree ↔ How you interpret the string. Unambiguous grammer Different parse trees ↔ different meanings. 1: S → P | S + P 2: P → T | P × T BAD! We want unambiguous meaning → 2 | ( S ) 3: T programs, html-code, math, English, . . . Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 14 / 16 Pushdown Automata →

  15. Pushdown Automata: DFAs with Stack Memory L = { w # w r | w ∈ { 0 , 1 } ∗ } S → # | 0 S 0 | 1 S 1 0 # 0 0 1 1 0 0 1 1 0 DFA with stack memory (push, pop, read). q 0 q 7 q 1 yes Push the first half of the string (before # ). or q 6 q 2 0 For each bit in the second half, pop the stack and compare. q 5 q 3 no q 4 DFAs with stack memory closely related to CFGs. Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 15 / 16 Non Context Free →

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