Accept or reject. Stack

Similar documents
UNIT-VI PUSHDOWN AUTOMATA

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

Pushdown Automata (2015/11/23)

October 6, Equivalence of Pushdown Automata with Context-Free Gramm

CISC4090: Theory of Computation

TAFL 1 (ECS-403) Unit- IV. 4.1 Push Down Automata. 4.2 The Formal Definition of Pushdown Automata. EXAMPLES for PDA. 4.3 The languages of PDA

Context-Free Languages

Pushdown Automata (PDA) The structure and the content of the lecture is based on

Fundamentele Informatica II

Pushdown Automata. Chapter 12

Equivalence of CFG s and PDA s

Introduction to Formal Languages, Automata and Computability p.1/42

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

CS Pushdown Automata

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Theory of Computation (IV) Yijia Chen Fudan University

CSE 105 THEORY OF COMPUTATION

CPS 220 Theory of Computation Pushdown Automata (PDA)

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

CSE 105 THEORY OF COMPUTATION

(pp ) PDAs and CFGs (Sec. 2.2)

Pushdown Automata (Pre Lecture)

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

Please give details of your answer. A direct answer without explanation is not counted.

MA/CSSE 474 Theory of Computation

The Idea of a Pushdown Automaton

Einführung in die Computerlinguistik

Automata Theory (2A) Young Won Lim 5/31/18

Lecture 17: Language Recognition

Miscellaneous. Closure Properties Decision Properties

Push-down Automata = FA + Stack

Theory of Computation Turing Machine and Pushdown Automata

Pushdown Automata. Reading: Chapter 6

(pp ) PDAs and CFGs (Sec. 2.2)

PUSHDOWN AUTOMATA (PDA)

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

SCHEME FOR INTERNAL ASSESSMENT TEST 3

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

Nondeterministic Finite Automata

5 Context-Free Languages

Foundations of Informatics: a Bridging Course

Undecidable Problems and Reducibility

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Section 1 (closed-book) Total points 30

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Computational Models - Lecture 5 1

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Introduction to Theory of Computing

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

Pushdown Automata: Introduction (2)

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Turing machines and linear bounded automata

Part I: Definitions and Properties

CS311 Computational Structures More about PDAs & Context-Free Languages. Lecture 9. Andrew P. Black Andrew Tolmach

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Intro to Theory of Computation

Languages, regular languages, finite automata

Computational Models - Lecture 4

CISC 4090 Theory of Computation

Variants of Turing Machine (intro)

CS375: Logic and Theory of Computing

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Context-free Languages and Pushdown Automata

Midterm Exam 2 CS 341: Foundations of Computer Science II Fall 2018, face-to-face day section Prof. Marvin K. Nakayama

Theory of Computation - Module 3

MTH401A Theory of Computation. Lecture 17

Fooling Sets and. Lecture 5

Theory of Computation

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Decision, Computation and Language

CISC 4090 Theory of Computation

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

Chapter Five: Nondeterministic Finite Automata

Turing machines and linear bounded automata

Theory of Computation

CSE 105 THEORY OF COMPUTATION

Nondeterminism. September 7, Nondeterminism

Introduction to Languages and Computation

Computational Models: Class 5

Homework. Context Free Languages. Announcements. Before We Start. Languages. Plan for today. Final Exam Dates have been announced.

Turing machines and linear bounded automata

CS481F01 Prelim 2 Solutions

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

Automata Theory - Quiz II (Solutions)

CpSc 421 Final Exam December 15, 2006

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

CS4026 Formal Models of Computation

Deterministic (DFA) There is a fixed number of states and we can only be in one state at a time

3.13. PUSHDOWN AUTOMATA Pushdown Automata

Midterm Exam 2 CS 341: Foundations of Computer Science II Fall 2016, face-to-face day section Prof. Marvin K. Nakayama

CPS 220 Theory of Computation

ECS 120 Lesson 15 Turing Machines, Pt. 1

CDM Parsing and Decidability

Automata and Computability. Solutions to Exercises

Solution Scoring: SD Reg exp.: a(a

Properties of Context-Free Languages. Closure Properties Decision Properties

Transcription:

Pushdown Automata CS351 Just as a DFA was equivalent to a regular expression, we have a similar analogy for the context-free grammar. A pushdown automata (PDA) is equivalent in power to contextfree grammars. We can give either a context-free grammar generating a context-free language, or we can give a pushdown automaton. Certain languages are more easily described in forms of one or the other. A pushdown automata is quite similar to a nondeterministic finite automata, but we add the extra component of a stack. The stack is of infinite size, and this is what will allow us to recognize some of the non-regular languages. We can visualize the pushdown automata as the following schematic: input Finite State Control Accept or reject Stack The finite state control reads inputs, one symbol at a time. The pushdown automata makes transitions to new states based on the input symbol, current state, and the top of the stack. In the process we may manipulate the stack. Ultimately, the control accepts or rejects the input. In one transition the PDA may do the following: 1. Consume the input symbol. If ε is the input symbol, then no input is consumed. 2. Go to a new state, which may be the same as the previous state. 3. Replace the symbol at the top of the stack by any string. a. If this string is ε then this is a pop of the stack b. The string might be the same as the current stack top (does nothing) c. Replace with a new string (pop and push) d. Replace with multiple symbols (multiple pushes)

As an informal example, consider the language L = {0 n 1 n n 0 }. A finite automaton is unable to recognize this language because it cannot store an arbitrarily large number of values in its finite memory. A PDA is able to recognize this language because it can use its stack to store the number of 0 s it has seen. The unlimited nature of the stack allows the DPA to store numbers of unbounded size. Here is an informal description of the PDA to describe this language: 1. As each 0 is read, push it onto the stack 2. As soon as 1 s are read, pop a 0 off the stack 3. If reading the input is finished exactly when the stack is empty, accept the input else reject the input Note that this description of the PDA is deterministic. However, in general the PDA is nondeterministic. This feature is crucial because, unlike finite automata, nondeterminism adds power to the capability that a PDA would have if they were only allowed to be deterministic. For example consider the following language, which requires a nondeterministic PDA to describe: Consider the language L = { ww R w is in (0+1) * } This language corresponds to even-length palindromes. Informally we can design a PDA to accept this language as follows: 1. Start in state q 0 that represents the state where we haven t yet seen the reversed part of the string. While in state q 0 we read each input symbol and push them on the stack. 2. At any time, assume we have seen the middle; i.e. fork off a new branch that assumes we have seen the end of w. We signify this choice by spontaneously going to state q 1. This behaves just like a nondeterministic finite automaton; we ll continue in both the forked-branch and the original branch. One of these branches may die, but as long as one of them reaches a final state we accept the input. 3. In state q 1 compare input symbols with the top of the stack. If match, pop the stack and proceed. If no match, then the branch of the automaton dies. 4. If we empty the stack then we have seen ww R and can proceed to a final accepting state.

Formal Definition of the Pushdown Automaton Formally a PDA is defined as seven components: P = (Q,, Γ, δ, q 0, Z 0, F). Q = finite set of states, like the finite automaton = finite set of input symbols, the alphabet Γ = finite stack alphabet, components we are allowed to push on the stack δ = transition function, which takes the triple: δ(q, a, X) where q = state in Q a = input symbol in X = stack symbol in Γ The output of δ is the finite set of pairs (p, γ) where p is a new state and γ is a new string of stack symbols that replaces X at the top of the stack. If γ = ε then we pop the stack, if γ = X the stack is unchanged, and if γ = YZ then X is replaced by Z and Y is pushed on the stack. Note the new stack top is to the left end. q 0 = start state Z 0 = start symbol. Initially, the PDA s stack consists of one instance of this start symbol and nothing else. We can use it to indicate the bottom of the stack. F = Set of final accepting states. Here is a formal description of the PDA that recognizes L = {0 n 1 n n 0 }. Q = { q 1, q 2, q 3, q 4 } = {0, 1} Γ = {0, Z 0 } F = {q1, q4} And δ is described by the following table: Input: 0 0 1 1 ε ε Stack: 0 Z 0 0 Z 0 0 Z 0!q 1 {(q 2, Z 0 )} {(q 2, 00)} {(q 2, 0Z 0 )} {(q 3, ε)} q 2 q 3 {(q 3, ε)} {(q 4, Z 0 )}!q 4 Be careful of the notation; when we go to (q 2, 00) then this is pushing an extra 0 onto the stack (which initially contained a zero). When we go to state (q 2, Z 0 ) we are keeping Z 0 on the top of the stack. We can also use a graphical notation to describe this PDA;

Start q1 q2 0,Z 0 /0Z 0 0,0/00 1, 0/ε q4 q3 1, 0/ε This format uses dark circles to indicate final states just like a normal automaton. Arrows indicate transitions to new states. The edges use the format: Input-Symbol, Top-of-Stack / String-to-replace-top-of-stack This means that 0,0/00 is taken when the input is 0 and the top of stack is 0, replacing the top of the stack with 00 (i.e. we push a single 0). 1,0/ε is taken when the input is 1 and the top of stack is 0, replacing the top of stack with ε (i.e. we pop the 0 off). The only thing this graphical format doesn t tell us is which stack symbol is the start symbol. But this is going to be Z 0 unless indicated otherwise. Here is the graphical description of the PDA that accepts the language L = { ww R w is in (0+1) * } 0,Z 0 /0Z 0 1,Z 0 /1Z 0 0,0/00 0,1/01 1,0/10 1,1/11 0,0/ε 1,1/ε Start q0 q1 q2 ε, 0/0 ε, 1/1 This works by staying in state q 0 when we are reading w. Each time we push the input symbol on the stack. Every time we guess that we have reached the end of w and are beginning w R by going to q 1 on an epsilon-transition. In state q 1 we pop off each 0 or 1 we encounter that matches the input. Any other input will die for this branch of the PDA. If we ever reach the bottom of the stack, we go to an accepting state.

Instantaneous Descriptions of a PDA (ID s) For a FA, the only thing of interest about the FA is its state. For a PDA, we want to know its state and the entire content of its stack. Often the stack is one of the most useful pieces of information, since it is not bounded in size. We can represent the instantaneous description (ID) of a PDA by the following triple (q,w,γ): 1. q is the state 2. w is the remaining input 3. γ is the stack contents By convention the top of the stack is shown at the left end of γ and the bottom at the right end. Moves of a PDA To describe the process of taking a transition, we can adopt a notation similar to δ and δ^ like we used for DFA s. In this case we use the turnstile symbol which is used as: (q, aw, Xβ) (p, w, αβ) In other words, we took a transition such that we went from state q to p, we consumed input symbol a, and we replaced the top of the stack X with some new string α. We can extend the move symbol to taking many moves: * represents zero or more moves of the PDA. As an example, consider the PDA that accepted L = { ww R w is in (0+1) * }. We can describe the moves of the PDA for the input 0110: (q 0, 0110, Z 0 ) (q 0, 110, 0Z 0 ) (q 0, 10, 10Z 0 ) (q 1, 10, 10Z 0 ) (q 1, 0, 0Z 0 ) (q 1, ) (q 2, ). We could have taken other moves rather than the ones above, but they would have resulted in a dead branch rather than an accepting state.

Language of a PDA The PDA consumes input and accepts input when it ends in a final state. We can describe this as: L(P) = {w (q 0, w, Z 0 ) * (q, ε, α) } where q F. That is, from the starting state, we can make moves that end up in a final state with any stack values. The stack values are irrelevant as long as we end up in a final state. It turns out we can also describe a language of a PDA by ending up with an empty stack with no further input: N(P) = {w (q 0, w, Z 0 ) * (q, ε, ε) } where q is any state. That is, we arrive at a state such that P can consume and at the same time empty its stack. It turns out that we can show the classes of languages that are L(P) for some PDA P is equivalent to the class of languages that are N(P) for some PDA P. This class is also exactly the context-free languages. See the text for the proof. Equivalence with Context-Free Grammars A context-free grammar and pushdown automata are equivalent in power. To prove this, we must show that we can take any CFG and express it as a PDA. Then we must take a PDA and show we can construct an equivalent CFG. We ll show the CFG!PDA process, but only give an overview of the process of going from the PDA!CFG (more details in the textbook). Theorem: Given a CFG grammar G, then some pushdown automata P recognizes L(G). Proof idea: The PDA P will work by accepting its input w, if G generates that input, by determining whether there is a derivation for w. Each sentential form of the derivation yields variables and terminals. We will design P to determine whether some series of substitutions using the rules of G leads from the start variable to w. One tricky part arises in dealing with the sentential forms (intermediate strings) of the derivation. These strings consist of variables and terminals. However, the PDA can access only the top symbol on the stack and that might be a terminal instead of a variable. To get around this problem, we match terminal symbols on the stack before the first variable immediately with symbols in the input string. Here is the state diagram for a PDA we can construct for an arbitrary CFG:

Start q start /SZ 0 S is the start symbol q loop ε, A/Y For rule A!Y, Y is a string a,a/ε For terminal symbol a q accept Here is an example for the following grammar: S! atb b T! Ta ε Start q start q loop /SZ 0 ε, S/aTb ε, T/Ta ε, S/b ε, T/ε a, a/ε b, b/ε q accept For example, given the string aab derived by S atb atab aab We have the corresponding moves: (q s, aab, Z 0 ) (q loop, aab, SZ 0 ) (q loop, aab, atbz 0 ) (q loop, ab, TbZ 0 ) (q loop, ab, TabZ 0 ) (q loop, ab, abz 0 ) (q loop, b, bz 0 ) (q loop, ) (q accept, )

Theorem: Given a PDA P, then some CFG grammar G recognizes L(P). This direction is necessary to show that both CFG s and PDA s are equivalent. We will only give a high level overview of how the proof is constructed; it is somewhat more complex then the direction from CFG to PDA because it is harder to program a grammar than it is to program a PDA. The idea behind the proof is to use the version of the PDA that accepts the input when the stack is empty. This means that the PDA may push values on the stack, but eventually on the path to accepting the input, it must pop off all of the stack symbols. For example, if the stack contains Y 1 Y 2 Y k then there must be some states p 0, p 1, on the path to the final state where the stack contains no elements such that we remove elements from the stack: Y 1 p 0 Y 2 p 1 Y k p k x 1 x 2 x k Here, input x 1 is read while Y 1 is popped, and a similar effect for x 2. Note that the net effect is the pop and it might require many intermediate moves. Using this idea of the popping to an empty stack, we can construct an equivalent grammar to a PDA that uses variables for a change in state from p to q when Y has been replaced by ε on the stack. This event is represented by the variable using the composite symbols [pxq]. This is a single variable in the grammar (e.g., A!aA we might have [pxq]! a[pxq]). The start symbol of G can then be described as: S! [q 0 Z 0 p].

We then need to construct all variables to fill out the grammar, where variable [qzp] corresponds to (q, wx, ZY) * (p, x, Y) and also (q, w, Z) * (p, ε, ε). Here are a few examples: PDA goes from q to p on input a, pops Z: PDA goes from q to p on input a, replaces top of stack with new value: [qzp]! a [qzp]! a[ryp] PDA goes from q to p on input a, [qzp]! a[ryr 1 ][r 1 Y 2 p] replaces top of stack with two values: In the previous two examples we actually need to check all states r 1 r k. Deterministic Pushdown Automata A DPDA is simply a pushdown automata without nondeterminism. That is, we are not allowed to have epsilon transitions or multiple identical inputs leading to different states. We will only be in one state at a time. The deterministic pushdown automata is not as powerful as the nondeterministic pushdown automata. This machine accepts a class of languages somewhere between regular languages and context-free languages. For this reason, the DPDA is often skipped as a topic; however, in practice, the DPDA can be useful since determinism is much easier to implement. Parses in programs such as YACC are actually implemented using a DPDA. This means the question really becomes what do we want to express in something like a programming language, and how might a DPDA limit what we can express. A DPDA can accept all regular languages and all unambiguous context-free languages. Acceptance by empty stack is also difficult for a DPDA. These conditions are typically satisfactory for a programming language, but not satisfactory for something like representing English sentences.