CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Slides:



Advertisements
Similar presentations
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Advertisements

Chapter 5 Pushdown Automata
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
PDAs => CFGs Sipser 2.2 (pages ). Last time…
PDAs => CFGs Sipser 2.2 (pages ). Last time…
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
CFG => PDA Sipser 2 (pages ). CS 311 Fall Formally… A pushdown automaton is a sextuple M = (Q, Σ, Γ, δ, q 0, F), where – Q is a finite set.
CS5371 Theory of Computation
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 9, 2006.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2009 with acknowledgement.
Fall 2006Costas Busch - RPI1 Pushdown Automata PDAs.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Prof. Busch - LSU1 Pushdown Automata PDAs. Prof. Busch - LSU2 Pushdown Automaton -- PDA Input String Stack States.
CS5371 Theory of Computation Lecture 12: Computability III (Decidable Languages relating to DFA, NFA, and CFG)
Finite State Machines Data Structures and Algorithms for Information Processing 1.
CS 3240: Languages and Computation Pushdown Automata & CF Grammars NOTE: THESE ARE ONLY PARTIAL SLIDES RELATED TO WEEKS 9 AND 10. PLEASE REFER TO THE TEXTBOOK.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
Pushdown Automata.
Pushdown Automata (PDAs)
Pushdown Automata Part I: PDAs Chapter Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)
Definition Moves of the PDA Languages of the PDA Deterministic PDA’s Pushdown Automata 11.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
PushDown Automata. What is a stack? A stack is a Last In First Out data structure where I only have access to the last element inserted in the stack.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Formal Languages, Automata and Models of Computation
Lecture 14UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 14.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 3 The Church-Turing Thesis Contents Turing Machines definitions, examples,
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Theory of Computation Automata Theory Dr. Ayman Srour.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
CSCI 2670 Introduction to Theory of Computing September 22, 2004.
Formal Languages, Automata and Models of Computation
Pushdown Automata.
PDAs Accept Context-Free Languages
CSE 105 theory of computation
Pushdown Automata PDAs
Pushdown Automata PDAs
Pushdown Automata PDAs
Pushdown Automata.
Pushdown Automata.
فصل دوم Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
… NPDAs continued.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Pushdown automata The Chinese University of Hong Kong Fall 2011
Recap lecture 40 Recap of example of PDA corresponding to CFG, CFG corresponding to PDA. Theorem, HERE state, Definition of Conversion form, different.
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)

Introduce Pushdown Automaton (PDA) Show that PDA = CFG –In terms of descriptive power Objectives

Roughly speaking, PDA = NFA + stack with unlimited size How does a PDA operate? In each step, it can read a character from the input string, can pop (remove) a symbol from the stack Then, depending on the character and the symbol, the PDA enters another state and can push (place) a symbol to the stack Pushdown Automaton (PDA) A stack is a “last in, first out” storage device

Stack is powerful Recall that an NFA cannot recognize the language {0 n 1 n | n  0} However, a PDA can do so (informally): –Read the characters from input –For any 0 it reads, push it onto the stack –As soon as 1s are seen**, pop a 0 off the stack for each 1 read ** after this point, if we read a 0 again, we reject the string immediately - Accept the string if the stack is just empty after the last 1 is read; Reject the string otherwise

PDA (Example) The following state diagram gives the PDA that recognizes {0 n 1 n | n  0}. The notation a, b  c means that the machine reads a from input, replace b by c from the top of stack. That is, pop b then push c. , $   1, 0   0,   0 1, 0   ,   $

PDA (Formal Definition) A PDA is a 6-tuple (Q, , , , q 0, F) –Q is a finite set of states –  is a finite set of characters –  is a finite set of stack symbols –  is the transition function of the form:  : Q x  ’ x  ’  2 Q x  ’, where  ’ =  ∪ {  } and  ’ =  ∪ {  } –q 0 is the start state –F is the set of accept states

Acceptance by PDA A PDA M = (Q, , , , q 0, F) accepts the input string w if –w can be written as w 1 w 2 …w m where w i in  ’, –there exist states r 0, r 1, …, r m in Q, and –there exist strings s 0, s 1, …, s m in  * satisfying the following three conditions: (see next slide) Intuitively, r i denotes the sequence of states visited by the PDA, and s i denotes the corresponding contents in the stack (reading from top to bottom)

Acceptance by PDA (cont.) Condition 1: r 0 = q 0, s 0 =  Condition 2: For i = 0, 1, …, m-1, we have (r i+1, b)   (r i, w i+1, a), where s i = at, s i+1 = bt for some a, b in  ’ Condition 3: r m  F This ensures that PDA starts at q 0, with an empty stack This ensures that PDA moves properly according to the state, the input character, and the stack This ensures PDA accepts only when the PDA is in an accept state after processing the whole input string

PDA (example 1) Recall that the following PDA recognizes {0 n 1 n | n  0} , $   1, 0   0,   0 1, 0   ,   $

PDA (example 1) Some points to notice: –The formal definition of PDA does not allow us to test if the stack is empty –The previous PDA tries to get the same effect by first placing $ to the stack, so that if it ever sees $ again, it knows the stack is empty –Similarly, PDA cannot test if the input has all been processed –The previous PDA can have the same effect because it can stay at the accept states only at the end of the input

PDA (example 1) We can also write the formal definition of the previous PDA, call it M, as follows: M = ({q 1,q 2,q 3,q 4 }, {0,1}, {0,$}, , q 1, {q 1,q 4 }), where  is given by  (q 1, ,  ) = { (q 2, $) },  (q 2, 0,  ) = { (q 2, 0) }  (q 2, 1, 0) = { (q 3,  ) },  (q 3, 1, 0) = { (q 3,  ) }  (q 3, , $) = { (q 4,  ) }, and for the remaining combinations of (q, x, y),  (q, x, y) = { }

PDA (example 2) Give a PDA that recognizing the language { a i b j c k | i,j,k  0 and i=j or i=k } How to construct it? First, for each ‘a’ read, we should push it onto the stack (for later matching) Then, we guess whether ‘a’ should be matched with ‘b’ or matched with ‘c’ We can guess because of the non-deterministic nature of PDA

PDA (example 2) , $   a,   a ,   $ ,    , $   b,    c, a   ,    c,    b, a  

PDA (example 3) Give a PDA recognizing { ww R | w in {0,1}* } How to construct it? First, the PDA should match the first character with the last character, the second character with the second last character, and so on… So, we push each character that is read to the stack in case it will be matched later At each point, we guess the middle of the string has been reached. We match the remaining characters with those stored in the stack (how?)

CFG = PDA Theorem: (1) If a language is generated by a CFG, it can be recognized by some PDA. (2) If a language is recognized by a PDA, it can be generated by some CFG. Proof: We shall prove both statements by construction.

Proof of (1) (1) If a language is generated by a CFG, it can be recognized by some PDA. Let L be a language generated by a CFG G. We show how to convert G into an equivalent PDA. Our PDA will do the following: –For an input string w, it can determine whether there is a derivation for w by G Recall that a derivation for w = a sequence of substitutions in the grammar that generates w

Proof of (1) The difficulty in testing whether there is a derivation for w is to figure out which substitutions to make –PDA can do so by guessing the sequence of correct substitutions

Proof of (1) Informally, our PDA does the following: –Place the mark symbol $ and then the start variable S (of G) on the stack –Repeat If the top of stack is a variable, say A, guess a rule A  u and substitute A by the string u If the top of stack is a terminal, say a, read the next symbol from the input and compare it with a. If they match, repeat. Otherwise, reject this branch of non-determinism If the top of stack is $, enter the accept state

An Example Run Input: 1100 CFG: S  SS | 1S0 | S $ At the beginning S 0 $ PDA uses the rule S  1S0 100 S 0 $ Input char is matched Next char

An Example Run (cont.) Input: 1100 CFG: S  SS | 1S0 | $ PDA uses the rule S  $ Input char is matched 0 0 $ Next char

An Example Run (cont.) Input: 1100 CFG: S  SS | 1S0 | 10 $ Whole input string is processed The $ in the top of stack tells PDA the stack is empty. PDA accepts the string Next char

Another Example Run Input: 1100 CFG: S  SS | 1S0 | S $ At the beginning $ PDA uses the rule S  $ Input char is matched Next char

Another Example Run (cont.) Input: 1100 CFG: S  SS | 1S0 | $ Input char cannot match top of stack. What will PDA do? It stops exploring this branch of computation Next char

Implementation Details We can see that, an input string w is accepted by our PDA if and only if there is a derivation from S to w What remains is to show that such a PDA can be constructed Pushing $, pushing S, or matching input chars with terminals in the stack is easy Difficulty: How to replace a variable in the top of stack by right side of a corresponding rule? (E.g., top of stack is A and we have with a rule A  xyz. How to replace A by xyz?) By using dummy states

Using dummy states , A  z ,   y ,   x Using two dummy states to replace A by xyz at the top of the stack , A  xyz A shorthand notation

PDA for Proof of (1) start ,   S$ , A  xyz for rule A  xyz a, a   for terminal a , $   shorthand notation used here

Example of Conversion Convert the following CFG into an equivalent PDA S  aTb | b T  Ta | 

Proof of (2) (2) If a language is recognized by a PDA, it can be generated by some CFG. We show how to convert a PDA into an equivalent CFG. Let L be the language, and P be the PDA recognizing L.

Proof of (2) We first change P slightly so that: –It has a single accept state, q accept –It empties the stack before accept –Each transition either pushes a symbol on the stack, or pops a symbol off the stack, but not both For the third change, we replace each transition in P that (i) pushes and pops at the same time with a two transition sequence that goes through a new state, (ii) neither pushes or pops with a two-transition sequence that pushes then pops a dummy stack symbol first and second changes are easy

Proof of (2) Next, for each pair of states p, q in P, we create a CFG variable A pq Our target is to make A pq generate exactly those strings that can bring P from p with an empty stack to q with an empty stack How to do so?

Creating A pq Note that PDA can get from p (with an empty stack) to q (with an empty stack) in two ways: (1) The stack gets empty before reaching q –This implies we get from p to some r (with empty stack) and then to q (2) The stack never gets empty before reaching q –This implies at p, we push some char t in stack, and then at q, we pop the same char t

Creating A pq (cont.) For each p, q, r, add the rule A pq  A pr A rq For each p, q, r, s with (r, t)   (p, a,  ) and (q,  )   (s, b, t), add the rule A pq  aA rs b That is, if we can get from p to r, and also from r to q, then we can get from p to q (Here, all starts and ends are with empty stack) That is, if we can get from p to r by reading a and pushing t, and we can get from s to q by reading b and popping t, then, if we start with p with an empty stack, we can reach q with an empty stack by reading a, going from r to s, then reading b.

Creating A pq (cont.) For each p, we also add the rule A pp   That is, if we can get from p to p by reading nothing If A pq really generates exactly those strings that brings P from p to q (with empty stacks), then what is A q start, q accept ? where q start denotes the start state of PDA

Proof of (2) So, in our grammar, we will have all the rules A pq and set A q start, q accept as the start variable What remains is to prove A pq generates exactly those strings that brings P from p to q (with empty stacks) That is, we need to prove –If A pq generates x, x brings P from p to q (with empty stacks) –If x brings P from p to q (with empty stacks), A pq generates x Exercise: Prove the above two statements (Hint: by induction)