Pushdown Automata (PDA) Reading: Chapter 6

Similar documents
Pushdown automata. Informatics 2A: Lecture 9. Alex Simpson. 3 October, School of Informatics University of Edinburgh als@inf.ed.ac.

Automata and Computability. Solutions to Exercises

Pushdown Automata. place the input head on the leftmost input symbol. while symbol read = b and pile contains discs advance head remove disc from pile

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Finite Automata. Reading: Chapter 2

The Halting Problem is Undecidable

3515ICT Theory of Computation Turing Machines

SRM UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF SOFTWARE ENGINEERING COURSE PLAN

CS154. Turing Machines. Turing Machine. Turing Machines versus DFAs FINITE STATE CONTROL AI N P U T INFINITE TAPE. read write move.

Finite Automata. Reading: Chapter 2

Turing Machines: An Introduction

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

Chapter 7 Uncomputability

Automata on Infinite Words and Trees

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

On Winning Conditions of High Borel Complexity in Pushdown Games

Automata and Formal Languages. Push Down Automata. Sipser pages Lecture 13. Tim Sheard 1

Model 2.4 Faculty member + student

Automata and Formal Languages

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 15 april Master Edition

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

How To Compare A Markov Algorithm To A Turing Machine

Honors Class (Foundations of) Informatics. Tom Verhoeff. Department of Mathematics & Computer Science Software Engineering & Technology

Introduction to Automata Theory. Reading: Chapter 1

Overview of E0222: Automata and Computability

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Theoretical Computer Science (Bridging Course) Complexity

C H A P T E R Regular Expressions regular expression

T Reactive Systems: Introduction and Finite State Automata

Regular Languages and Finite State Machines

Turing Machines, Part I

Lecture Notes on Polynomials

Scanner. tokens scanner parser IR. source code. errors

Sect Greatest Common Factor and Factoring by Grouping

Computability Theory

24 Uses of Turing Machines

Lecture summary for Theory of Computation

Push-down Automata and Context-free Grammars

Introduction to Turing Machines

Boolean Algebra Part 1

Computational Models Lecture 8, Spring 2009

Composability of Infinite-State Activity Automata*

The Optimum One-Pass Strategy for Juliet

Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

Properties of Stabilizing Computations

Regular Expressions and Automata using Haskell

Lecture 2: Universality

Automata Theory. Şubat 2006 Tuğrul Yılmaz Ankara Üniversitesi

Pushdown Automata. International PhD School in Formal Languages and Applications Rovira i Virgili University Tarragona, Spain

Electrical Symbols and Line Diagrams

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

Informatique Fondamentale IMA S8

Welcome to... Problem Analysis and Complexity Theory , 3 VU

Two Way F finite Automata and Three Ways to Solve Them

Intrusion Detection via Static Analysis

Cartesian Products and Relations

Computer Architecture Syllabus of Qualifying Examination

Philadelphia University Faculty of Information Technology Department of Computer Science First Semester, 2007/2008.

Logic in Computer Science: Logic Gates

Using Hands-On Visualizations to Teach Computer Science from Beginning Courses to Advanced Courses

Reading 13 : Finite State Automata and Regular Expressions

Introduction to NP-Completeness Written and copyright c by Jie Wang 1

CS5236 Advanced Automata Theory

CS 301 Course Information

CS 341: Foundations of Computer Science II elearning Section Syllabus, Spring 2015

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

The Trip Scheduling Problem

Simulation-Based Security with Inexhaustible Interactive Turing Machines

Deterministic Finite Automata

Bottom-Up Parsing. An Introductory Example

Complexity Theory. Jörg Kreiker. Summer term Chair for Theoretical Computer Science Prof. Esparza TU München

6.080/6.089 GITCS Feb 12, Lecture 3

4.3 TABLE 3 TABLE five

Implementation of Recursively Enumerable Languages using Universal Turing Machine in JFLAP

CSEE 3827: Fundamentals of Computer Systems. Standard Forms and Simplification with Karnaugh Maps

CS143 Handout 08 Summer 2008 July 02, 2007 Bottom-Up Parsing

Modeling of Graph and Automaton in Database

Row Echelon Form and Reduced Row Echelon Form

MACM 101 Discrete Mathematics I

Computationally Complete Spiking Neural P Systems Without Delay: Two Types of Neurons Are Enough

Fast nondeterministic recognition of context-free languages using two queues

Two-dimensional Languages

Hint 1. Answer (b) first. Make the set as simple as possible and try to generalize the phenomena it exhibits. [Caution: the next hint is an answer to

Predicate Logic. Example: All men are mortal. Socrates is a man. Socrates is mortal.

Finite Automata and Regular Languages

Regular Languages and Finite Automata

Fundamentele Informatica II

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

6.3 Conditional Probability and Independence

Formal Grammars and Languages

Section 4.2: The Division Algorithm and Greatest Common Divisors

Lecture 1: Oracle Turing Machines

ω-automata Automata that accept (or reject) words of infinite length. Languages of infinite words appear:

Automata, languages, and grammars

a 1 x + a 0 =0. (3) ax 2 + bx + c =0. (4)

Notes on Complexity Theory Last updated: August, Lecture 1

4.6 The Primitive Recursive Functions

S on n elements. A good way to think about permutations is the following. Consider the A = 1,2,3, 4 whose elements we permute with the P =

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Preventing Denial of Service Attacks in a Calculus of Mobile Resources

Transcription:

Pushdown Automata (PDA) Reading: Chapter 6 1

PDA - the automata for CFLs What is? FA to Reg Lang, PDA is to CFL PDA == [ -NFA + a stack ] Why a stack? Input string -NFA Accept/reject A stack filled with stack symbols 2

Pushdown Automata - Definition A PDA P := ( Q,,, δ,q 0,Z 0,F ): Q: states of the -NFA : input alphabet : stack symbols δ: transition function q 0 : start state Z 0 : Initial stack top symbol F: Final/accepting states 3

old state input symb. Stack top δ : Q x x => Q x new state(s)new Stack top(s) δ : The Transition Function δ(q,a,x) = {(p,y), } 1. state transition from q to p 2. a is the next input symbol 3. X is the current stack top symbol 4. Y is the replacement for X; it is in * (a string of stack symbols) i. Set Y = for: Pop(X) ii. If Y=X: stack top is unchanged iii. If Y=Z 1 Z 2 Z k : X is popped and is replaced by Yin reverse order (i.e., Z 1 will be the new stack top) q a p X Y=? Action i) Y= Pop(X) ii) Y=X Pop(X) Push(X) iii) Y=Z 1 Z 2..Z k Pop(X) Push(Z k ) Push(Z k-1 ) Push(Z 2 ) Push(Z 1 ) Y 4

Example Let L wwr = {ww R w is in (0+1)*} CFG for L wwr : S==> 0S0 1S1 PDA for L wwr : P := ( Q,,, δ,q 0,Z 0,F ) = ( {q 0, q 1, q 2 },{0,1},{0,1,Z 0 },δ,q 0,Z 0,{q 2 }) 5

Initial state of the PDA: Stack top PDA for L wwr Z 0 q 0 1. δ(q 0,0, Z 0 )={(q 0,0Z 0 )} 2. δ(q 0,1, Z 0 )={(q 0,1Z 0 )} 3. δ(q 0,0, 0)={(q 0,00)} 4. δ(q 0,0, 1)={(q 0,01)} 5. δ(q 0,1, 0)={(q 0,10)} 6. δ(q 0,1, 1)={(q 0,11)} 7. δ(q 0,, 0)={(q 1, 0)} 8. δ(q 0,, 1)={(q 1, 1)} 9. δ(q 0,, Z 0 )={(q 1, Z 0 )} 10. δ(q 1,0, 0)={(q 1, )} 11. δ(q 1,1, 1)={(q 1, )} 12. δ(q 1,, Z 0 )={(q 2, Z 0 )} First symbol push on stack Grow the stack by pushing new symbols on top of old (w-part) Switch to popping mode, nondeterministically (boundary between w and w R ) Shrink the stack by popping matching symbols (w R -part) Enter acceptance state 6

PDA as a state diagram δ(q i,a, X)={(q j,y)} Current state Next input symbol q i Current stack top Stack Top Replacement (w/ string Y) a, X / Y Next q j state 7

PDA for L wwr : Transition Diagram Grow stack 0, Z 0 /0Z 0 1, Z 0 /1Z 0 0, 0/00 0, 1/01 1, 0/10 1, 1/11 Pop stack for matching symbols 0, 0/ 1, 1/ = {0, 1} = {Z 0, 0, 1} Q={q 0,q 1,q 2 }, Z 0 /Z 0 q 0 q 1 q 2, Z 0 /Z 0, 0/0, 1/1 Switch to popping mode, Z 0 /Z 0 Go to acceptance This would be a non-deterministic PDA 8

Example 2: language g of balanced paranthesis Pop stack for = { (, ) } matching symbols = {Z Grow stack 0, ( } Q={q 0,q 1,q 2 } (, Z 0 / ( Z 0 (, ( / ( ( ), ( / q 0 q 1 q 2, Z 0 / Z 0 ), ( /, Z Z0 /Z Z0 Switch to popping mode (, ( / ( ( (, Z 0 / ( Z 0, Z 0 / Z 0 Go to acceptance (by final state) when you see the stack bottom symbo To allow adjacent blocks of nested paranthesis 9

Example 2: language of balanced paranthesis (another design) (,Z 0 / ( Z 0 (,( / ( ( ), ( / = { (, ) } = {Z 0, ( } Q={q 0,q 1 } start,z 0 / Z 0,Z 0 / Z 0 q 0 q 1 10

PDA s Instantaneous Description (ID) A PDA has a configuration at any given instance: (q,w,y) q - current state w - remainder of the input (i.e., unconsumed part) y - current stack contents as a string from top to bottom of stack If δ(q,a, X)={(p, A)} is a transition, then the following are also true: (q, a, X ) --- (p,,a) (q, aw, XB ) --- (p,w,ab) --- sign is called a turnstile notation and represents one move ---* sign represents a sequence of moves 11

How does the PDA for L wwr work on input 1111? (q 0,1111,Z 0 ) All moves made by the non-deterministic PDA (q 0,111,1Z 0 ) (q 1,1111,Z 0 ) Path dies (q 0,11,11Z 11Z 0 ) (q 1,111,1Z 1Z 0 ) Path dies (q 0,1,111Z 0 ) (q 1,11,11Z 0 ) (q 0,,1111Z 0 ) (q 1,1,111Z 0 ) (q 1,1,1Z 0 ) Acceptance by final state: (q Z 1,,1111Z 0 ) (q 1,,11Z 0 ) (q 1,,Z 0 ) Path dies Path dies (q 2,,Z 0 ) = empty input AND final state 12

Principles about IDs Theorem 1: If for a PDA, (q, x, A) ---* (p, y, B), then for any string w * and *, it is also true that: t (q, x w, A ) ---* (p, y w, B ) Theorem 2: If for a PDA, (q, x w, A) ) ---* (p, y w, B), then it is also true that: (q, x, A) ---* (p, y, B) 13

There are two types of PDAs that one can design: those that accept by final state or by empty stack Acceptance by PDAs that accept by final state: For a PDA P, the language accepted by P, denoted d by L(P) by final state, t is: {w (q 0,w,Z 0 ) ---* (q,, A) }, s.t., q F Checklist: - input exhausted? - in a final state? PDAs that accept by empty stack: For a PDA P, the language accepted by P, denoted by N(P) by empty stack, is: {w (q 0,w,Z, 0 0) ---* (q,,, ) )}, for any q Q. Q) Does a PDA that accepts by empty stack need any final state specified in the design? Checklist: - input exhausted? - is the stack empty? 14

Example: L of balanced parenthesis PDA that accepts by final state P F : P N : (,Z 0 / ( Z 0 (,( / ( ( ), ( / An equivalent PDA that accepts by empty stack (Z (,Z 0 /(Z 0 (, ( / ( ( ), ( /,Z 0 / start q 0,Z 0 / Z 0 q 1 start q 0,ZZ 0 /Z 0,ZZ 0 /Z 0 How will these two PDAs work on the input: ( ( ( ) ) ( ) ) ( ) 15

PDA for L wwr : Proof of wwr correctness Theorem: The PDA for L wwr accepts a string x by final state if and only if x is of the form ww R. Proof: (if-part) If the string is of the form ww R then there exists a sequence of IDs that leads to a final state: (q 0,ww R,Z 0 ) ---* (q 0,w R,wZ 0 ) ---* (q 1,w R,wZ 0 ) ---* (q 1,,ZZ 0 ) --- * (q 2,,ZZ 0 ) (only-if part) Proof by induction on x 16

PDAs accepting by final state and empty stack are equivalent P F <= PDA accepting by final state P F = (Q F,,, δ F,q 0,Z 0,F) P N <= PDA accepting by empty stack P N = (Q N,,, δ N,q 0,Z 0 ) Theorem: (P N ==> P F ) For every P N, there exists a P F s.t. L(P F )=L(P N ) (P F ==> P N ) For every P F, there exists a P N s.t. L(P F )=L(P N ) 17

How to convert an empty stack PDA into a final state PDA? P N ==> P F construction Whenever P N s stack becomes empty, make P F go to a final state without consuming any addition symbol To detect empty stack in P N : P F pushes a new stack symbol X 0 (not in of P N ) initially before simultating P N P P : F : N New start, X 0 / X 0 p 0, X 0 /Z 0 X 0 q 0, X 0 / X 0, X 0 / X 0, X 0 / X 0, X 0 /X 0 p f P P F = (Q N U {p 0,p f },, N U {X 0 }, δ F, p 0, X 0, {p f }) 18

Example: Matching parenthesis ( ) P N : ( {q 0 }, {(,)}, {Z 0,Z 1 }, δ N, q 0, Z 0 ) δ N : δ N (q 0,(,Z 0 ) = { (q 0,Z 1 Z 0 ) } δ N (q 0,(,Z (Z)={(q 1 (q 0, Z 1 Z 1 )} δ N (q 0,),Z 1 ) = { (q 0, ) } δ N (q 0,,Z 0 ) = { (q 0, ) } P f : ( {p 0,q 0,p f }, {(,)}, {X 0,Z 0,Z 1 }, δ f, p 0, X 0, p f ) δ f : δ f (p 0,,X 0 ) = { (q 0,Z 0 ) } δ f (q 0,(,Z (Z)={(q 0 (q 0,Z 1 Z 0 )} δ f (q 0,(,Z 1 ) = { (q 0, Z 1 Z 1 ) } δ f (q 0,),Z 1 ) = { (q 0, ) } δ f (q 0,,Z 0 ) = { (q 0, ) } δ f (p 0,,X 0 ) = { (p f, X 0 ) } (,Z 0 /Z 1 Z 0 (,Z 1 /Z 1 Z 1 ),Z 1 /,Z 0 / 0 (,Z 0 /Z 1 Z 0 (,Z 1 /Z 1 Z 1 ),Z 1 /,Z 0 / start q 0 start p 0,X 0 /Z 0 X 0,X 0 / X 0 q 0 p f Accept by empty stack Accept by final state 19

How to convert an final state PDA into an empty stack PDA? P F ==> P N construction Main idea: Whenever P F reaches a final state, just make an -transition into a new end state, clear out the stack and accept Danger: What if P F design is such that it clears the stack midway without entering a final state? to address this, add a new start symbol X 0 (not in of P F F) P N = (Q U {p 0,p e },, U {X 0 }, δ N, p 0, X 0 ) P N : New start p 0, X 0 /Z 0 X 0 q 0, any/, any/ p e, any/, any/ P F 20

Equivalence of PDAs and CFGs 21

CFGs == PDAs ==> CFLs PDA by final state PDA by empty stack? CFG 22

This is same as: implementing a CFG using a PDA Converting CFG to PDA Main idea: The PDA simulates the leftmost derivation on a given w, and upon consuming it fully it either arrives at acceptance (by empty stack) or non-acceptance. INPU UT w PDA (acceptance by empty stack) accept reject OUTP PUT implements CFG 23

This is same as: implementing a CFG using a PDA Converting a CFG into a PDA Main idea: The PDA simulates the leftmost derivation on a given w, and upon consuming it fully it either arrives at acceptance (by empty py stack) or non-acceptance. Steps: 1. Push the right hand side of the production onto the stack, with leftmost symbol at the stack top 2. If stack top is the leftmost variable, then replace it by all its productions (each possible substitution will represent a distinct path taken by the non-deterministic PDA) 3. If stack top has a terminal symbol, and if it matches with the next symbol in the input string, then pop it State is inconsequential (only one state is needed) 24

Before: A Before: a a Formal construction of PDA from CFG Given: G= (V,T,P,S) Note: Initial stack symbol (S) same as the start variable in the grammar Output: P N = ({q}, T, V U T, δ, q, S) δ: For all A V, add the following transition(s) in the PDA: δ(q,,a) A)={(q (q, ) A ==> P} For all a T, add the following transition(s) in the PDA: δ(q,a,a)= { (q, ) } After: After: a a pop 25

Example: CFG to PDA G = ( {S,A}, {0,1}, P, S) P: S ==> AS A ==> 0A1 A1 01 PDA = ({q}, {0,1}, {0,1,A,S}, δ, q, S) δ: δ(q,, S) = { (q, AS), (q, )},S / S 1,1 / 0,0 /,A / 01,A / A1,A / 0A1,S S /,S / AS δ(q,, A) = { (q,0a1), (q,a1), (q,01) } δ(q, 0, 0) = { (q, ) } δ(q, 1, 1) = { (q, ) } How will this new PDA work? Lets simulate string 0011 q 26

Simulating string 0011 on the new PDA Leftmost deriv.: PDA (δ): δ(q,, S) = { (q, AS), (q, )} δ(q,, A) = { (q,0a1), (q,a1), (q,01) } δ(q, 0, 0) = { (q, ) } δ(q, 1, 1) = { (q, ) } Stack moves (shows only the successful path):,s / S 11/ 1,1 0,0 /,A / 01,A / A1,A / 0A1,S, /,S / AS q S => AS => 0A1S => 0011S => 0011 S A S 0 A 1 S 0 A 1 1 1 S S 0 1 1 S 0 1 S 1 S 1 Accept by empty stack S =>AS =>0A1S =>0011S => 0011 27

Proof of correctness for CFG ==> PDA construction Claim: A string is accepted by G iff it is accepted (by empty stack) by the PDA Proof: (only-if part) Prove by induction on the number of derivation steps (if part) If (q, wx, S) -- * (q,x,b) then S => * lm wb 28

Converting a PDA into a CFG Main idea: Reverse engineer the productions from transitions If δ(q,a,z) => (p, Y 1 Y 2 Y 3 Y k ): 1. State is changed from q to p; 2. Terminal a is consumed; 3. Stack top symbol Z is popped and replaced with a sequence of k variables. Action: Create a grammar variable called [qzp] which includes the following production: [qzp] => a[py 1 q 1 ][qy 1 2 q 2 ][qy 2 3 q 3 ] [q k-1 Y k q k ] Proof discussion (in the book) 29

Example: Bracket matching To avoid confusion, we will use b= ( and e= ) P N : ( {q 0 }, {b,e}, {Z 0,Z 1 }, δ, q 0, Z 0 ) 1. δ(q 0,b,Z 0 ) = { (q 0,Z 1 Z 0 ) } 2. δ(q 0,b,Z 1 ) = { (q 0,Z 1 Z 1 ) } 3. δ(q 0,e,Z 1 ) = { (q 0, ) } 4. δ(q 0,,Z 0 0) = { (q 0, )} If you were to directly write a CFG: S => b S e S 0. S => [q 0 Z 0 q 0 ] 1. [q 0 Z 0 q 0 ]=>b[qz 0 1 q 0 ][qz 0 0 q 0 ] 2. [q 0 Z 1 q 0 ] => b [q 0 Z 1 q 0 ] [q 0 Z 1 q 0 ] 3. [q 0 Z 1 q 0 ] => e 4. [q 0 Z 0 q 0 ] => Let A=[q 0 Z 0 q 0 ] Let B=[q 0 Z 1 q 0 ] 0. S => A 1. A => b B A 2. B => b B B 3. B => e 4. A => Simplifying, 0. S => b B S 1. B => bbb e B 30

Two ways to build a CFG Build a PDA Construct CFG from PDA (indirect) Derive CFG directly (direct) Similarly Two ways to build a PDA Derive a CFG Construct PDA from CFG (indirect) Design a PDA directly (direct) 31

Deterministic PDAs 32

This PDA for L wwr is non-deterministic Grow stack 0, Z 0 /0Z 0 1, Z 0 /1Z 0 0, 0/00 0, 1/01 1, 0/10 1, 1/11 Pop stack for matching symbols 0, 0/ 1, 1/ Why does it have to be nondeterministic? q 0 q 1 q 2, Z 0 /Z 0, 0/0, 1/1 Switch to popping mode, Z 0 /Z 0 Accepts by final state To remove guessing, impose the user to insert c in the middle 33

Example shows that: Nondeterministic PDAs D-PDAs D-PDA for L = R wcwr {wcw cissome some special symbol not in w} Grow stack 0, Z 0 /0Z 0 1, Z 0 /1Z 0 0, 0/00 0, 1/01 1, 0/10 1, 1/11 Pop stack for matching symbols 0, 0/ 1, 1/ Note: all transitions have become deterministic q 0 q 1 q 2 c, Z 0 /Z 0 c, 0/0 c, 1/1 Switch to popping mode, Z 0 /Z 0 Accepts by final state 34

Deterministic PDA: Definition A PDA is deterministic if and only if: 1. δ(qax)hasat δ(q,a,x) at most one member for any a U { } If δ(q,a,x) is non-empty for some a, then δ(q,,x) must be empty. 35

PDA vs DPDA vs Regular languages L wcwr L wwr Regular languages D-PDA non-deterministic PDA 36

Summary PDAs for CFLs and CFGs Non-deterministic Deterministic PDA acceptance types 1. By final state 2. By empty stack PDA IDs, Transition diagram Equivalence of CFG and PDA CFG => PDA construction PDA => CFG construction 37