theoretical computer
play

Theoretical Computer Science (Bridging Course) Context Free - PowerPoint PPT Presentation

Theoretical Computer Science (Bridging Course) Context Free Languages Gian Diego Tipaldi Topics Covered Context free grammars Pushdown automata Equivalence of PDAs and CFGs Non-context free grammars The pumping lemma Context


  1. Theoretical Computer Science (Bridging Course) Context Free Languages Gian Diego Tipaldi

  2. Topics Covered  Context free grammars  Pushdown automata  Equivalence of PDAs and CFGs  Non-context free grammars  The pumping lemma

  3. Context Free Grammars  Extend regular expressions  First studied for natural languages  Often used in computer languages  Compilers  Parsers  Pushdown automata

  4. Context Free Grammars  Collection of substitution rules  Rules: Symbol -> string  Variable symbols (Uppercase)  Terminal symbols (lowercase)  Start variable

  5. Context Free Grammars  Example grammar G1:  A, B are variables  0,1,# are terminals  A is the start variable

  6. Context Free Grammars Example string: 000#111 Does it belong to the grammar?

  7. Context Free Grammars Example string: 000#111  A -> 0A1  0A1 ->00A11  00A11 -> 000A111  000A111 -> 000B111  000B111 -> 000#111

  8. Context Free Grammars Example string: 000#111 A  A -> 0A1 A  0A1 ->00A11 A A  00A11 -> 000A111 B  000A111 -> 000B111 0 0 0 # 1 1 1 Parse tree for  000B111 -> 000#111 000#111 in 𝐻 1

  9. Context Free Grammars Example string: 000#111 A  A -> 0A1 A  0A1 ->00A11 A A  00A11 -> 000A111 B  000A111 -> 000B111 0 0 0 # 1 1 1 Parse tree for  000B111 -> 000#111 000#111 in 𝐻 1

  10. Natural Language Example < SENTENCE > → < NOUN-PHRASE >< >< VERB-PHRASE > < NOUN-PHRASE > → < CMPLX-NOUN > | < CMPLX-NOUN >< >< PREP-PHRASE > < VERB-PHRASE > → < CMPLX-VERB > | < CMPLX-VERB >< >< PREP-PHRASE > < PREP-PHRASE > → < PREP >< >< CMPLX-NOUN > < CMPLX-NOUN > → < ARTICLE >< >< NOUN > < CMPLX-VERB > → < VERB > | < VERB >< >< NOUN-PHRASE > < ARTICLE > → a | the < NOUN > → boy | girl | flower < VERB > → touches | likes | sees < PREP > → with  A boy sees  The boy sees the flower  A girl with the flower likes the boy

  11. Context Free Grammar Definition 2.2: A context-free grammar is a 4-tuple ( 𝑊 , Σ , 𝑆 , 𝑇 ) where:  𝑊 is the set of variables  Σ is the set of terminals, Σ ∩ 𝑊 = ∅  𝑆 is the set of rules  𝑇 ∈ 𝑊 is the start symbol

  12. Language of a grammar  u,v,w are strings, A->w a rule  uAv yields uwv: uAv uwv ∗  u derives v: u v if  Language of a grammar

  13. Parsing a string  Consider the following grammar     G ( V , , R , E xp r } 3        V { E xp r , T erm , F a cto r }     { a , , , (, )} R is           E xp r E xp r T erm | T erm           T erm T erm F a cto r | F a cto r      F a cto r ( E xp r ) | a  What are the parse trees of  a + a x a  (a + a) x a

  14. Parsing a string

  15. Designing Grammars Harder than designing automata Few techniques can be used  Union of context free languages  Conversion from DFA (regular)  Exploit linked variables (0 n 1 n )  Exploit recursive structure (trickier)

  16. Union of Different CFGs   n n   S 0 S 1 | L G ( ) {0 1 | n 0} 1 1 1   S 1 S 0 | n n   L G ( ) {1 0 | n 0} 2 2 2    S S | S L G ( ) L G ( ) L G ( ) 1 2 1 2

  17. Conversion from DFAs  Take the same vocabulary: Σ 𝑕 = Σ 𝑏  For each state q i insert a variable R i  For each transition 𝜀 𝑟 𝑗 , 𝑏 = 𝑟 𝑘 insert 𝑆 𝑗 → 𝑏𝑆 𝑘  For each accept state 𝑟 𝑙 insert 𝑆 𝑙 → 𝜗

  18. Conversion from DFAs 1 0 1 q 2 q 1 0  Take the same vocabulary: Σ = {0,1}  Insert all the variables: V = {𝑆 1 , 𝑆 2 }  Insert the rules:

  19. Designing Linked Strings  Languages of the type  Create rules of the form  For the language above

  20. Designing Recursive Strings  Example are arithmetic expressions  Create the recursive structure <Expr>  Place it where it appear <Factor>

  21. Ambiguity  Generate a string in several ways  E.g., grammar G5:  No usual notion of precedence  Natural language processing  “a boy touches a girl with the flower”

  22. Ambiguity  Consider the string: a + a x a

  23. Ambiguity – Definition  Leftmost derivation: At every step, replace the leftmost variable  A string is generated ambiguously if it has multiple leftmost derivations  A CFG is ambiguous if generates some string ambiguously  Some context free languages are inherently ambiguous

  24. Chomsky Normal Form (CNF) Definition 2.8: A context-free grammar is in Chomsky normal form if every rule is of the form 𝐵 → 𝐶𝐷 𝐵 → 𝑏 where 𝑏 is any terminal and 𝐵 , 𝐶 , and 𝐷 are any variables — except that 𝐶 and 𝐷 may not be the start variable. In addition we permit the rule 𝑇 → 𝜁 , where 𝑇 is the start variable.

  25. Chomsky Normal Form (CNF) Theorem 2.9: Any context-free language is generated by a context-free grammar in Chomsky normal form.

  26. Proof Idea  Rewrite the rules not in CNF  Introduce new variables  Four cases:  Start variable on the right side  Epsilon rules: 𝐵 → ε  Unit rules: 𝐵 → 𝐶  Long and/or mixed rules: 𝐵 → 𝑏𝐵𝑐𝑐𝐶𝑏𝐶

  27. Proof Idea  Start variable on the right side  Introduce a new start and 𝑇 1 → 𝑇 0  Epsilon rules: 𝐵 → ε  Introduce new rules without A  Unit rules: 𝐵 → 𝐶  Replace B with its production  Long and/or mixed rules: 𝐵 → 𝑏𝐵𝑐𝑐𝐶𝑏𝐶  New variables and new rules

  28. Formal Proof: by Construction 1. Add a new start symbol 𝑇_0 and the rule 𝑇 0 → 𝑇 , where 𝑇 is the old start 2. Remove all rules 𝐵 → 𝜗 :  For each 𝑆 → 𝑣𝐵𝑤 add 𝑆 → 𝑣𝑤  For each 𝑆 → 𝐵 add 𝑆 → 𝜗  Repeat until all gone (keep 𝑇 0 → 𝜗 ) 3. Remove all rules 𝐵 → 𝐶 :  For each 𝐶 → 𝑣 add 𝐵 → 𝑣  Repeat until all gone

  29. Formal Proof: by Construction 4. Convert all rules 𝐵 → 𝑣 1 … 𝑣 𝑙 , 𝑙 ≥ 3 in: 𝐵 → 𝑣 1 𝐵 1  𝐵 1 → 𝑣 2 𝐵 2 , …  𝐵 𝑙−2 → 𝑣 𝑙−1 𝑣 𝑙  5. Convert all rules 𝐵 → 𝑣 1 𝑣 2 : Replace any terminal 𝑣 𝑗 with 𝑉 𝑗  Add the rules 𝑉 𝑗 → 𝑣 𝑗   Be careful of cycles!

  30. CNF: Example 2.10 from Book  Convert the CFG in CNF 𝑇 → 𝐵𝑇𝐵 | 𝑏𝐶 𝐵 → 𝐶 | 𝑇 𝐶 → 𝑐 | 𝜁  Added rules in bold  Removed rules in stroke

  31. CNF: Example 2.10 from Book  Add the new start symbol 𝑻 𝟏 → 𝑻 𝑇 → 𝐵𝑇𝐵 | 𝑏𝐶 𝐵 → 𝐶 | 𝑇 𝐶 → 𝑐 | 𝜁

  32. CNF: Example 2.10 from Book  Remove the empty rule 𝐶 → 𝜁 𝑇 0 → 𝑇 𝑇 → 𝐵𝑇𝐵 𝑏𝐶 𝒃 𝐵 → 𝐶 𝑇 𝜻 𝐶 → 𝑐 | 𝜁

  33. CNF: Example 2.10 from Book  Remove the empty rule 𝐵 → 𝜁 𝑇 0 → 𝑇 𝑇 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑻𝑩 𝑩𝑻 | 𝑻 𝐵 → 𝐶 𝑇 𝜁 𝐶 → 𝑐

  34. CNF: Example 2.10 from Book  Remove unit rule: 𝑇 → 𝑇 𝑇 0 → 𝑇 𝑇 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑇𝐵 𝐵𝑇 | 𝑇 𝐵 → 𝐶 | 𝑇 𝐶 → 𝑐

  35. CNF: Example 2.10 from Book  Remove unit rule: 𝑇 0 → 𝑇 𝑇 0 → 𝑇 | 𝑩𝑻𝑩 𝒃𝑪 𝒃 𝑻𝑩 𝑩𝑻 𝑇 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝐵 → 𝐶 | 𝑇 𝐶 → 𝑐

  36. CNF: Example 2.10 from Book  Remove unit rule: 𝐵 → 𝐶 𝑇 0 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝑇 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝐵 → 𝐶 𝑇 𝒄 𝐶 → 𝑐

  37. CNF: Example 2.10 from Book  Remove unit rule: 𝐵 → 𝑇 𝑇 0 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝑇 → 𝐵𝑇𝐵 𝑏𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝐵 → 𝑇 𝑐 𝑩𝑻𝑩 𝒃𝑪 𝒃 𝑻𝑩 𝑩𝑻 𝐶 → 𝑐

  38. CNF: Example 2.10 from Book  Convert the remaining rules 𝑇 0 → 𝐵𝑩 𝟐 𝑽𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝑇 → 𝐵𝑩 𝟐 𝑽𝐶 𝑏 𝑇𝐵 𝐵𝑇 𝐵 → 𝑐 𝐵𝑩 𝟐 𝑽𝐶 𝑏 𝑇𝐵 | 𝐵𝑇 𝑩 𝟐 → 𝑻𝑩 𝑽 → 𝒃 𝐶 → 𝑐

  39. Pushdown Automata (PDA)  Extend NFAs with a stack  The stack provides additional memory  Equivalent to context free grammars  They recognize context free languages

  40. Finite State Automata  Can be simplified as follow state control a a b b input  State control for states and transitions  Tape to store the input string

  41. Pushdown Automata  Introduce a stack component state control a a b b a input stack a b  Symbols can be read and written there

  42. What is a Stack?  Stacks are special containers  Symbols are “pushed” on top  Symbols can be “popped” from top  Last in first out principle  Similar to plates in cafeteria

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