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: Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

  6. 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: ε Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

  7. 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 Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

  8. 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 Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

  9. 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 Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

  10. 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      Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 5 / 16 Rules for CFGs →

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

  12. 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 Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  13. 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 S 1: Write down the start variable (form the first production rule, typically S ). Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  14. 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: S 0 S 1 1: ε 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. “Replace variable with expression , no matter where (independent of context)” Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  15. 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: S 0 S 1 1: ε 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)” Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  16. 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: S 0 S 1 00 S 11 1: 1: ε 01 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)” Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  17. 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: S 0 S 1 00 S 11 000 S 111 1: 1: 1: ε 01 0011 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)” Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

  18. 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: S 0 S 1 00 S 11 000 S 111 0000 S 1111 1: 1: 1: 1: ε 01 0011 000111 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)” Creator: Malik Magdon-Ismail Context Free Grammars (CFGs): 6 / 16 cfg bal →

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