1 / 26

Intro to Theory of Computation

L ECTURE 6 Last time: Pumping lemma Proving a language is not regular Today: Pushdown automata (PDAs) Context-free grammars (CFGs). Intro to Theory of Computation. CS 332. Sofya Raskhodnikova. On Friday:. Homework 2 due Homework 3 out. CS332 so far. MODEL OF A PROBLEM: LANGUAGE.

ralphh
Télécharger la présentation

Intro to Theory of Computation

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. LECTURE 6 • Last time: • Pumping lemma • Proving a language is not regular • Today: • Pushdown automata (PDAs) • Context-free grammars (CFGs) Intro to Theory of Computation CS 332 Sofya Raskhodnikova On Friday: Homework 2 due Homework 3 out SofyaRaskhodnikova; based on slides by Nick Hopper

  2. CS332 so far MODEL OF A PROBLEM: LANGUAGE MODEL OF A PROGRAM: DFA EQUIVALENT MODELS: NFA, REGEXP PROBLEMS THAT A DFA CAN’T SOLVE ARE WE DONE? Sofya Raskhodnikova; based on slides by Nick Hopper

  3. NONE OF THESE ARE REGULAR • Σ = {0, 1}, L = { 0n1n | n ≥ 0 } • Σ = {a, b, c, …, z}, L = { w | w = wR } • Σ = { (, ) }, L = { balanced strings of parens } We can write a Cor JAVA program for any of them! Sofya Raskhodnikova; based on slides by Nick Hopper

  4. PUSHDOWNAUTOMATA (PDA) FINITE STATE CONTROL INPUT STACK (Last in, first out) Sofya Raskhodnikova; based on slides by Nick Hopper

  5. string pop push ε,ε → $ 0,ε→ 0 11 0011 0011 011 1,0 →ε ε,$ → ε 1,0 →ε 1 0 STACK $ 0 $ $

  6. string pop push ε,ε → $ 0,ε→ 0 1 001 001 01 1,0 →ε ε,$ → ε 1,0 →ε 0 STACK $ 0 $ $ PDA to recognize L = { 0n1n | n ≥ 0 }

  7. Formal Definition A PDAis a 6-tuple P = (Q, Σ, Γ, , q0, F) Q is a finite set of states Σ is the alphabet Γis the stack alphabet  : Q ΣεΓε → P(Q Γε) is the transition function q0 Q is the start state F  Q is the set of accept states Σε= Σ {ε}and P(Q Γε) is the set of subsets of Q Γε Note: A PDA is defined to be nondeterministic. Sofya Raskhodnikova; based on slides by Nick Hopper

  8. Formal Definition A PDAis a 6-tuple P = (Q, Σ, Γ, , q0, F) Q is a finite set of states Σ is the alphabet Γis the stack alphabet  : Q ΣεΓε → P(Q Γε) is the transition function q0 Q is the start state F  Q is the set of accept states A PDA starts with an empty stack. Itaccepts a string if at least one of its computational branches reads all the input and gets into an accept state at the end of it. Sofya Raskhodnikova; based on slides by Nick Hopper

  9. An example PDA ε,ε → $ 0,ε→ 0 q0 q1 1,0 →ε ε,$ → ε q3 q2 1,0 →ε Q = {q0, q1, q2, q3} Σ = {0,1} Γ = {$,0}  : Q Σε Γε→ P(Q  Γε) (q1,1,0) = { (q2,ε) } (q2,1,1) = 

  10. PDA: algorithmic description ε,ε → $ 0,ε→ 0 q0 q1 • Place the marker symbol $ onto the stack. • Keep reading a 0 and pushing it onto the stack. If you read a 1, pop a 0 and go to the next step. • Nondeterministically keep reading a 1 and popping a 0 or go to the next step. • If the top of the stack is $, enter the accept state. (Then PDA accepts if the input has been read). 1,0 →ε ε,$ → ε q3 q2 1,0 →ε

  11. Exercise Σ = {a, b, c, …, z} ε,ε → $ ,ε→ q0 q1 ε,ε→ε ε,$ → ε ,→ε q3 q2 What strings are accepted by this PDA? • Only • Palindromes • Even-length palindromes • All strings that start and end with the same letter • None of the above

  12. Give an ALGORITHMIC description Σ = {a, b, c, …, z} ε,ε → $ ,ε→ q0 q1 • Place the marker symbol $ onto the stack. • Nondeterministically keep reading a character and pushing it onto the stack or go to the next step. • Nondeterministically keep reading and popping a matching character or go to the next step. • If the top of the stack is $, enter the accept state. ε,ε→ε ε,$ → ε ,→ε q3 q2

  13. q2 q3 q1 q4 q6 q5 Build a PDA to recognize L = { aibjck | i, j, k ≥ 0 and (i = j or i = k) } c,ε→ε b,a →ε q0 ε,$ → ε ε,ε → ε ε,ε → $ ε,ε → ε ε,ε → ε ε,$ → ε a,ε→ a b,ε→ε c,a →ε

  14. CONTEXT-FREEGRAMMARS (CFGs) production rules start variable A → 0A1 A → B B → # variables terminals A  0A1  00A11  00B11  00#11 Sofya Raskhodnikova; based on slides by Nick Hopper

  15. VALLEY GIRL GRAMMAR <PHRASE> → <FILLER><PHRASE> <PHRASE> → <START><END> <FILLER> → LIKE <FILLER> → UMM <START> → YOU KNOW <START> → ε <END> → GAG ME WITH A SPOON <END> → AS IF <END> → WHATEVER <END> → LOSER Sofya Raskhodnikova; based on slides by Nick Hopper

  16. VALLEY GIRL GRAMMAR <PHRASE> → <FILLER><PHRASE> | <START><END> <FILLER> → LIKE | UMM <START> → YOU KNOW | ε <END> → GAG ME WITH A SPOON | AS IF | WHATEVER | LOSER Sofya Raskhodnikova; based on slides by Nick Hopper

  17. Formal Definition A CFGis a 4-tuple G = (V, Σ, R, S) Vis a finite set of variables Σ is a finite set of terminals (disjoint from V) R is set of production rules of the form A → W, where A  V and W  (VΣ)* S  V is the start variable L(G) is the set of strings generated by G A language is context-free if it is generated by some CFG. Sofya Raskhodnikova; based on slides by Nick Hopper

  18. Formal Definition A CFGis a 4-tuple G = (V, Σ, R, S) Vis a finite set of variables Σ is a finite set of terminals (disjoint from V) R is set of production rules of the form A → W, where A  V and W  (VΣ)* S  V is the start variable R = { S → 0S1, S → ε } Example: G = {{S}, {0,1}, R, S} L(G) = { 0n1n | n ≥ 0 } Sofya Raskhodnikova; based on slides by Nick Hopper

  19. CFG terminology production rules start variable A → 0A1 A → B B → # variables terminals A  0A1  00A11  00B11  00#11 uVwyields uvw if (V → v)  R. A derives 00#11 in 4 steps. Sofya Raskhodnikova; based on slides by Nick Hopper

  20. Example GIVE A CFG FOR EVEN-LENGTH PALINDROMES S → S for all   Σ S → ε Sofya Raskhodnikova; based on slides by Nick Hopper

  21. Example GIVE A CFG FOR THE EMPTY SET G = { {S}, Σ, , S } Sofya Raskhodnikova; based on slides by Nick Hopper

  22. GIVE A CFG FOR… L3 = { strings of balanced parens } L4 = { aibjck | i, j, k ≥ 0 and (i = j or j = k) } Sofya Raskhodnikova; based on slides by Nick Hopper

  23. CFGs in the real world The syntactic grammar for the Java programming language BasicForStatement: for ( ; ; ) Statement for ( ; ; ForUpdate ) Statement for ( ; Expression ; ) Statement for ( ; Expression ; ForUpdate ) Statement for ( ForInit ; ; ) Statement for ( ForInit ; ; ForUpdate ) Statement for ( ForInit ; Expression ; ) Statement for ( ForInit ; Expression ; ForUpdate ) Statement Sofya Raskhodnikova; based on slides by Nick Hopper

  24. COMPILER MODULES LEXER PARSER SEMANTIC ANALYZER TRANSLATOR/INTERPRETER Sofya Raskhodnikova; based on slides by Nick Hopper

  25. Parse trees A A A B 0 0 # 1 1 A  0A1  00A11  00B11  00#11 Sofya Raskhodnikova; based on slides by Nick Hopper

  26. Equivalence of CFGs & PDAs A language is generated by a CFG  It is recognized by a PDA Sofya Raskhodnikova; based on slides by Nick Hopper

More Related