260 likes | 275 Views
Understanding Pushdown Automata (PDAs), formal definition, behavior with stack, CFGs, formal definition, production rules, language generation. Topics cover PDA transitions, accepting strings, constructions, and CFGs for language modeling. Explore PDAs recognizing specific Languages. Formalize CFG using variables, terminals, production rules, and the start variable. Learn the basics of CFG formal definition and language generation. Dive into examples and exercises for practical understanding.
E N D
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
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
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
PUSHDOWNAUTOMATA (PDA) FINITE STATE CONTROL INPUT STACK (Last in, first out) Sofya Raskhodnikova; based on slides by Nick Hopper
string pop push ε,ε → $ 0,ε→ 0 11 0011 0011 011 1,0 →ε ε,$ → ε 1,0 →ε 1 0 STACK $ 0 $ $
string pop push ε,ε → $ 0,ε→ 0 1 001 001 01 1,0 →ε ε,$ → ε 1,0 →ε 0 STACK $ 0 $ $ PDA to recognize L = { 0n1n | n ≥ 0 }
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
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
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) =
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 →ε
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
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
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 →ε
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
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
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
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
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
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
Example GIVE A CFG FOR EVEN-LENGTH PALINDROMES S → S for all Σ S → ε Sofya Raskhodnikova; based on slides by Nick Hopper
Example GIVE A CFG FOR THE EMPTY SET G = { {S}, Σ, , S } Sofya Raskhodnikova; based on slides by Nick Hopper
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
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
COMPILER MODULES LEXER PARSER SEMANTIC ANALYZER TRANSLATOR/INTERPRETER Sofya Raskhodnikova; based on slides by Nick Hopper
Parse trees A A A B 0 0 # 1 1 A 0A1 00A11 00B11 00#11 Sofya Raskhodnikova; based on slides by Nick Hopper
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