Pushdown Automata Part II: PDAs and CFG Chapter 12.

Slides:



Advertisements
Similar presentations
Context-Free and Noncontext-Free Languages
Advertisements

Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Chapter 5 Pushdown Automata
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
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,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
PDAs => CFGs Sipser 2.2 (pages ). Last time…
PDAs => CFGs Sipser 2.2 (pages ). Last time…
CFG => PDA Sipser 2 (pages ).
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.
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Fall 2004COMP 3351 Pushdown Automata PDAs. Fall 2004COMP 3352 Pushdown Automaton -- PDA Input String Stack States.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
1 Pushdown Automata PDAs. 2 Pushdown Automaton -- PDA Input String Stack States.
1 PDAs Accept Context-Free Languages. 2 Context-Free Languages (Grammars) Languages Accepted by PDAs Theorem:
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.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
Final Exam Review Cummulative Chapters 0, 1, 2, 3, 4, 5 and 7.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
1 Dr. torng CFG → PDA construction Shows that for any CFL L, there exists a PDA M such that L(M) = L The reverse is true, but we skip the proof Parsing.
Pushdown Automata (PDA) Part 2 While I am distributing graded exams: Design a PDA to accept L = { a m b n : m  n; m, n > 0} MA/CSSE 474 Theory of Computation.
Pushdown Automata (PDA) Intro
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
TM Design Universal TM MA/CSSE 474 Theory of Computation.
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
Pushdown Automata (PDAs)
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
Pushdown Automata Part I: PDAs Chapter Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2)
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.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Context-Free and Noncontext-Free Languages Chapter 13 1.
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.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Context-Free and Noncontext-Free Languages Chapter 13.
Formal Languages, Automata and Models of Computation
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
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,
1 Chapter Pushdown Automata. 2 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations.
CS 154 Formal Languages and Computability March 10 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Bottom-up parsing Pumping Theorem for CFLs MA/CSSE 474 Theory of Computation.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Theory of Languages and Automata By: Mojtaba Khezrian.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Table-driven parsing Parsing performed by a finite state machine.
Pushdown Automata PDAs
PDAs Accept Context-Free Languages
Pushdown Automata PDAs
Chapter 7 PUSHDOWN AUTOMATA.
Bottom-up parsing Pumping Theorem for CFLs
Chapter 2 Context-Free Language - 01
Pushdown Automata (PDA) Part 2
Chapter 1 Regular Language
MA/CSSE 474 Theory of Computation
Presentation transcript:

Pushdown Automata Part II: PDAs and CFG Chapter 12

PDAs and Context-Free Grammars Theorem: The class of languages accepted by PDAs is exactly the class of context-free languages. Recall: context-free languages are languages that can be defined with context-free grammars. Restate theorem: Can describe with context-free grammar Can accept by PDA

Going One Way Lemma: Each context-free language is accepted by some PDA. Proof (by construction): The idea: Let the stack do the work. Two approaches: Top down Bottom up

Top Down The idea: Let the stack keep track of expectations. Example: Arithmetic expressions E  E + T E  T T  T  F T  F F  (E) F  id (1) (q, , E), (q, E+T) (7) (q, id, id), (q, ) (2) (q, , E), (q, T) (8) (q, (, ( ), (q, ) (3) (q, , T), (q, T*F) (9) (q, ), ) ), (q, ) (4) (q, , T), (q, F) (10) (q, +, +), (q, ) (5) (q, , F), (q, (E) ) (11) (q, , ), (q, ) (6) (q, , F), (q, id)

A Top-Down Parser Answering “Could G generate w?” starting from S. The outline of M is: M = ({p, q}, , V, , p, {q}), where  contains: ● The start-up transition ((p, , ), (q, S)). ● For each rule X  s1s2…sn. in R, the transition: ((q, , X), (q, s1s2…sn)). ● For each character c  , the transition: ((q, c, c), (q, )).

Example of the Construction L = {anb*an} 0 (p, , ), (q, S) (1) S   1 (q, , S), (q, ) (2) S  B 2 (q, , S), (q, B) (3) S  aSa 3 (q, , S), (q, aSa) (4) B   4 (q, , B), (q, ) (5) B  bB 5 (q, , B), (q, bB) 6 (q, a, a), (q, ) input = a a b b a a 7 (q, b, b), (q, ) trans state unread input stack p a a b b a a  0 q a a b b a a S 3 q a a b b a a aSa 6 q a b b a a Sa 3 q a b b a a aSaa 6 q b b a a Saa 2 q b b a a Baa 5 q b b a a bBaa 7 q b a a Baa 5 q b a a bBaa 7 q a a Baa 4 q a a aa 6 q a a 6 q  

Bottom-Up The idea: Let the stack keep track of what has been found. (1) E  E + T (2) E  T (3) T  T  F (4) T  F (5) F  (E) (6) F  id Reduce Transitions: (1) (p, , T + E), (p, E) (2) (p, , T), (p, E) (3) (p, , F  T), (p, T) (4) (p, , F), (p, T) (5) (p, , )E( ), (p, F) (6) (p, , id), (p, F) Shift Transitions (7) (p, id, ), (p, id) (8) (p, (, ), (p, () (9) (p, ), ), (p, )) (10) (p, +, ), (p, +) (11) (p, , ), (p, )

A Bottom-Up Parser Answering “Could G generate w?” starting from w. The outline of M is: M = ({p, q}, , V, , p, {q}), where  contains: ● The shift transitions: ((p, c, ), (p, c)), for each c  . ● The reduce transitions: ((p, , (s1s2…sn.)R), (p, X)), for each rule X  s1s2…sn. in G. ● The finish up transition: ((p, , S), (q, )).

Example of the Construction L = {anb*an} 0 (p, , S), (q, ) (1) S   1 (p, , ), (p, S) (2) S  B 2 (p, , B), (p, S) (3) S  aSa 3 (p, , aSa), (p, S) (4) B   4 (p, , ), (p, B) (5) B  bB 5 (p, , Bb), (p, B) 6 (p, a, ), (p, a) input = a a b b a a 7 (p, b, ), (p, b) trans state unread input stack p a a b b a a  6 p a b b a a a 6 p b b a a aa 7 p b a a baa 7 p a a bbaa 4 p a a Bbbaa 5 p a a Bbaa 5 p a a Baa 2 p a a Saa 6 p a aSaa 3 p a Sa 6 p  aSa 3 p  S 0 q  

Notice Nondeterminism Machines constructed with the algorithm are often nondeterministic, even when they needn't be. This happens even with trivial languages. Example: AnBn = {anbn: n  0} A grammar for AnBn is: A PDA M built top-down for AnBn is: (0) ((p, , ), (q, S)) [1] S  aSb (1) ((q, , S), (q, aSb)) [2] S   (2) ((q, , S), (q, )) (3) ((q, a, a), (q, )) (4) ((q, b, b), (q, )) But transitions 1 and 2 make M nondeterministic. A directly constructed machine for AnBn: How about bottom-up?

Going The Other Way Lemma: If a language is accepted by a pushdown automaton M, it is context-free (i.e., it can be described by a context-free grammar). Proof (by construction): (We will skip this to Slide 16, as it’s rarely necessarily going this way in practice!) Step 1: Convert M to restricted normal form: ● M has a start state s that does nothing except push a special symbol # onto the stack and then transfer to a state s from which the rest of the computation begins. There must be no transitions back to s. ● M has a single accepting state a. All transitions into a pop # and read no input. ● Every transition in M, except the one from s, pops exactly one symbol from the stack.

Converting to Restricted Normal Form Example: {wcwR : w  {a, b}*} Add s and a: Pop no more than one symbol:

M in Restricted Normal Form [1] [3] [2] Pop exactly one symbol: Replace [1], [2] and [3] with: Must have one transition for everything that could have been on the top of the stack so it can be popped and then pushed back on. [1] ((s, a, #), (s, a#)), ((s, a, a), (s, aa)), ((s, a, b), (s, ab)), [2] ((s, b, #), (s, b#)), ((s, b, a), (s, ba)), ((s, b, b), (s, bb)), [3] ((s, c, #), (f, #)), ((s, c, a), (f, a)), ((s, c, b), (f, b))

Second Step - Creating the Productions Example: WcWR M = The basic idea – simulate a leftmost derivation of M on any input string.

Second Step - Creating the Productions Example: abcba

Nondeterminism and Halting 1. There are context-free languages for which no deterministic PDA exists. 2. It is possible that a PDA may ● not halt, ● not ever finish reading its input. 3. There exists no algorithm to minimize a PDA. It is undecidable whether a PDA is minimal.

Nondeterminism and Halting It is possible that a PDA may ● not halt, ● not ever finish reading its input. Let  = {a} and consider M = L(M) = {a}: (1, a, ) |- (2, a, a) |- (3, , ) On any other input except a: ● M will never halt. ● M will never finish reading its input unless its input is . Example: aa

Solutions to the Problem ● For NDFSMs: ● Convert to deterministic, or ● Simulate all paths in parallel. ● For NDPDAs: ● Impossible to simulate all paths in parallel – each path would need its won stack ● Formal solutions usually involve changing the grammar  parse tree may not make sense ● Practical solutions that: ● Preserve the structure of the grammar, but ● Only work on a subset of the CFLs.

Alternative Equivalent Definitions of a PDA (Skip to Slide 21) Accept by accepting state at end of string (i.e., we don't care about the stack). From M (in our definition) we build M (in this one): 1. Initially, let M = M. 2. Create a new start state s. Add the transition: ((s, , ), (s, #)). 3. Create a new accepting state qa. 4. For each accepting state a in M do, 4.1 Add the transition ((a, , #), (qa, )). 5. Make qa the only accepting state in M.

Example The balanced parentheses language

What About These? ● FSM plus FIFO queue (instead of stack)? ● FSM plus two stacks?

Comparing Regular and Context-Free Languages Regular Languages Context-Free Languages ● regular exprs. ● or ● regular grammars ● context-free grammars ● recognize ● parse ● = DFSMs ● = NDPDAs