Pushdown Automata: Introduction (2)

Similar documents
CPS 220 Theory of Computation Pushdown Automata (PDA)

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

Einführung in die Computerlinguistik

CSE 105 THEORY OF COMPUTATION

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Pushdown Automata (Pre Lecture)

Pushdown Automata (2015/11/23)

CPS 220 Theory of Computation

CSE 105 THEORY OF COMPUTATION

Foundations of Informatics: a Bridging Course

Pushdown Automata. Chapter 12

MA/CSSE 474 Theory of Computation

CISC4090: Theory of Computation

Introduction to Theory of Computing

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

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

Finite Automata - Deterministic Finite Automata. Deterministic Finite Automaton (DFA) (or Finite State Machine)

Equivalence of CFG s and PDA s

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

CS Pushdown Automata

Theory of Computation (IV) Yijia Chen Fudan University

Computational Models - Lecture 4

Computational Models - Lecture 5 1

Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

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

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

Pushdown Automata. Reading: Chapter 6

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,

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

5 Context-Free Languages

Lecture 17: Language Recognition

MA/CSSE 474 Theory of Computation

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

Context-Free Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Theory of Computation

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

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

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

Fundamentele Informatica II

SCHEME FOR INTERNAL ASSESSMENT TEST 3

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

UNIT-VI PUSHDOWN AUTOMATA

Context Free Languages: Decidability of a CFL

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

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

(pp ) PDAs and CFGs (Sec. 2.2)

CS481F01 Prelim 2 Solutions

Push-down Automata = FA + Stack

Automata and Languages

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

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

Nondeterministic Finite Automata

Computational Models: Class 5

Finite Automata. Finite Automata

Part I: Definitions and Properties

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

(pp ) PDAs and CFGs (Sec. 2.2)

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Theory Bridge Exam Example Questions

Peter Wood. Department of Computer Science and Information Systems Birkbeck, University of London Automata and Formal Languages

Accept or reject. Stack

Functions on languages:

CPSC 421: Tutorial #1

Automata and Computability. Solutions to Exercises

Intro to Theory of Computation

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Theory of Computation Turing Machine and Pushdown Automata

CSE 105 THEORY OF COMPUTATION

Automata and Computability. Solutions to Exercises

X-machines - a computational model framework.

CDM Parsing and Decidability

Final exam study sheet for CS3719 Turing machines and decidability.

Context-Free Grammars and Languages. Reading: Chapter 5

Miscellaneous. Closure Properties Decision Properties

Automata Theory - Quiz II (Solutions)

Context-free Languages and Pushdown Automata

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

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

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Einführung in die Computerlinguistik

The Idea of a Pushdown Automaton

Properties of Context-Free Languages

CSE 105 THEORY OF COMPUTATION

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

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

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 10 : Context-Free Grammars

Theory of Computation - Module 3

Undecidable Problems and Reducibility

CS20a: summary (Oct 24, 2002)

DM17. Beregnelighed. Jacob Aae Mikkelsen

Outline. Nondetermistic Finite Automata. Transition diagrams. A finite automaton is a 5-tuple (Q, Σ,δ,q 0,F)

ECS 120: Theory of Computation UC Davis Phillip Rogaway February 16, Midterm Exam

Lecture III CONTEXT FREE LANGUAGES

MTH401A Theory of Computation. Lecture 17

ECS 120 Lesson 15 Turing Machines, Pt. 1

Transcription:

Pushdown Automata: Introduction Pushdown automaton (PDA) M = (K, Σ, Γ,, s, A) where K is a set of states Σ is an input alphabet Γ is a set of stack symbols s K is the start state A K is a set of accepting states, and is a transition relation: (K (Σ {ɛ})) Γ ) (K Γ ) Simply put, a PDA is a FA with a stack Configuration of a PDA M K Σ Γ I.e., a triple consisting of a state, remaining input, and contents of the stack Yields-in-one-step relation: Denoted M Relates one configuration to another (q 1, cw, γ 1 γ) M (q 2, w, γ 2 γ) iff ((q 1, c, γ 1 ), (q 2, γ 2 )) Points of note: 1. M makes transitions only if γ 1 matches top of stack This results in pop of γ 1 and push of γ 2 Top of stack accessible only via a pop Can push and pop more than one symbol at a time Stack provides ability to count and to keep track of structure 2. If γ 1 = ɛ, corresponds to a transition that ignores stack contents Yields relation: Denoted M Reflexive, transitive closure of M Computation of M: Finite sequence of configurations C 0, C 1,..., C n, n 0, where C 0 is initial configuration C n is of the form (q, ɛ, γ) for some q K and γ Γ C 0 M C 1 M... M C n 1

Pushdown Automata: Introduction (2) Since is a relation, above definition defines a nondeterministic PDA Accepting computation: One in which (s, w, ɛ) (q, ɛ, ɛ) for some q A Rejecting computation: One in which (s, w, ɛ) (q, w, α) where M has no transitions from q given w, α Language accepted by PDA M denoted L(M) Label arcs of transition diagram with c/γ 1 /γ 2 to denote ((q 1, c, γ 1 ), (q 2, γ 2 )) 2

Pushdown Automata: Deterministic PDAs Deterministic PDA: One in which every configuration allows only a single transition Two conditions must hold: 1. M contains no pairs of transitions that compete 2. If q A M, there is no transition ((q, ɛ, ɛ), (p, a)) for any p, a Can represent operation of a nondeterministic PDA using trees Each node represents a configuration of the machine Path from root to leaf represents a computation Cannot represent all paths in parallel as with NFAs as need a stack for each path Nondeterminism is more important for CFLs as opposed to RLs Some CFLs cannot be represented by deterministic PDAs Two common situations contribute to nondeterministic PDAs 1. Transitions to be taken only when stack is empty 2. Transitions to be taken only when input stream is empty Nondeterminism can be reduced (not necessarily eliminated) by eliminating the above Both approaches use special markers 1. Checking for an empty stack Push special marker (#) onto stack at beginning of processing Stack is empty when this marker is on top Note that stack must be truly empty in order to accept input Final step in processing must be to pop special marker from stack 2. Checking for end of input Concatenate special marker ($) to end of input string 3

Theorem 12.1 Pushdown Automata: Equivalence of CFGs and PDAs - Converting CFGs to PDAs Statement: Given a CFG G = (V, Σ, R, S), there exists a PDA M such that L(M) = L(G) Proof: By construction Two approaches can be taken: 1. Build a bottom-up parser 2. Build a top-down parser Strategy is to build a PDA that simulates G by attempting to derive input string w from G The two algorithms are opposites in all aspects 1. Top-down parser Strategy is to generate a leftmost derivation for w Start by pushing S onto stack Whenever topmost symbol of stack matches the LHS of some rule, replace stack symbol with symbols on RHS of rule (pushing symbols from right to left) Whenever symbol on top of stack is terminal, match it against input string (read input character, pop stack) PDA M has exactly 2 states: (a) First pushes S onto stack (b) Second performs rest of processing Algorithm: PDA CFGToPDA-TD (CFG G); Create M = ({p, q}, Σ, V,, p, {q}), where contains ((p, ɛ, ɛ), (q, S)) ((q, ɛ, X), (q, γ 1 γ 2...γ n )) for each X γ 1 γ 2...γ n R G ((q, ɛ, X), (q, ɛ) for each X ɛ R G ((q, c, c), (q, ɛ)) for each c Σ 4

Pushdown Automata: Equivalence of CFGs and PDAs - Converting CFGs to PDAs (2) 2. Bottom-up parser Strategy is to generate a rightmost derivation for w Start by pushing input characters onto stack Whenever topmost sequence of symbols on stack matches the RHS of some rule, replace sequence with LHS symbol of rule Accept when input consumed with S on top of stack PDA M has exactly 2 states: (a) First performs all processing of input (and appropriate pushes and pops) (b) Second pops S from stack Algorithm: PDA CFGToPDA-BU (CFG G); Create M = ({p, q}, Σ, V,, p, {q}), where contains ((p, ɛ, S), (q, ɛ)) ((p, ɛ, (γ 1 γ 2...γ n ) R ), (p, X)) for each X γ 1 γ 2...γ n R G ((q, c, ɛ), (p, c)) for each c Σ Theorem important because 1. Proves that languages that are described by CFGs are equivalent to those accepted by PDAs 2. Provides useful tools for compilers 5

Pushdown Automata: Equivalence of CFGs and PDAs - Converting PDAs to CFGs Restricted Normal Form (RNF) To convert a PDA to a CFG, it must first be transformed into a particular format PDA M is in RNF iff 1. Start state does only 1 thing: Push a special marker (#) onto the stack No transitions may enter the start state, and only one may leave it 2. Has a single accepting state All transitions into this state consume no input, and pop # 3. Every transition -except the one from the start state - pops exactly one symbol 6

Algorithm: Pushdown Automata: Equivalence of CFGs and PDAs - Converting PDAs to CFGs (2) PDA convertpdatornf (PDA M) { M = M; M.K = M.K + s ; M.Gamma = M.Gamma + #; M.Delta = M.Delta + ((s, ɛ, ɛ), (s, #)); M.s = s ; M.K = M.K + a; for(each q in M.A) M.Delta = M.Delta + ((q, ɛ, #), (a, ɛ)); M.A = a; for(each t in M.Delta) if(t == ((q, c, a 1 a 2...a n ), (p, γ)) { M.K = M.K + qq 1 ; M.Delta = M.Delta + ((q, ɛ, a 1 ), (qq 1, ɛ)); for (i = 1; i < n - 1; i++) { M.K = M.K + qq i+1 ; M.Delta = M.Delta + ((qq i, ɛ, a i+1 ), (qq i+1, ɛ)); } M.Delta = M.Delta + ((qq n 1, c, a n ), (p, γ)); M.Delta = M.Delta - t; } for(each t in M.Delta) if(t == ((q, c, ɛ), (p, γ)) for(each a in M.Gamma) M.Delta = M.Delta + ((q, c, a), (p, γa)); M.Delta = M.Delta - t; return M ; } 7

Theorem 12.2 Pushdown Automata: Equivalence of CFGs and PDAs - Converting PDAs to CFGs (3) Statement: Given PDA M = (K, Σ, Γ,, s, A), there exists a CFG G such that L(G) = L(M) Proof: By construction Begin by converting PDA to RNF Primary concerns are generating nonterminal symbols and rules for expanding them Nonterminals will have form < q i γq j > This nonterminal generates only those strings that drive M from state q i to state q j while popping all symbols on the stack from the top through the first occurrence of γ For PDA in RNF, < s#a > generates all strings that drive M from the start state to a, popping everything off the stack These are the stings in L(M) First rule in grammar is thus S < s#a > To generate rest of rules, consider three kinds of transitions 1. Those that push no symbols Consider and transition ((q, c, γ), (r, ɛ)) M. where c Σ {ɛ} After M reaches r, it could reach w via any states in M Nonterminal < qγw > generates strings that drive M from q to w while popping γ Some of these strings are those that start with c and that drive M from r to w without popping anything So need rule < qγw > c < rɛw > 8

Pushdown Automata: Equivalence of CFGs and PDAs - Converting PDAs to CFGs (4) 2. Those that push one symbol Consider M reads c, pops γ, pushes alpha, and transitions to r Nonterminal < qγw > generates strings that drive M from q to w while popping γ Some of these strings are those that that drive M from q to r while popping γ and reading c, and then driving M from r to w while popping α So need rule < qγw > c < rαw > 3. Those that push more than one symbol Consider Since M is in RNF, can only pop one symbol per transition For every symbol pushed during one transition, will need an additional transition to pop each Some of the strings that drive M from q to w are those that drive M from q to r while popping γ They then drive M from r to r 1 while popping α 1, from r 1 to r 2 while popping α 2, etc. They finally transition from r n to w while popping α n So need rule < qγw > c < rα 1 r 1 >< r 1 α 2 r 2 >... < r n 1 α n w > 4. Need to be able to terminate rule expansion So need rule < qɛq > ɛ 9

Algorithm: Pushdown Automata: Equivalence of CFGs and PDAs - Converting PDAs to CFGs (5) CFG convertpdatocfg (PDA M) { M = convertpdatornf(m); V = M.S; R = {S -> <s # a>}; for (each ((q, c, gamma), (r, epsilon)) in M.Delta) for (each state w in M.K) if (w!= M.s) R = R + <q gamma w> -> c<r epsilon w>; for (each ((q, c, gamma), (r, alpha)) in M.Delta) if (q!= M.s) for (each state w in M.K) if (w!= M.s) R = R + <q gamma w> -> c<r alpha w>; for (each ((q, c, gamma), (r, alpha1 alpha2... alphan)) in M.Delta) if (q!= M.s) { create rules pi of form <q gamma w> -> c<q alpha1 r1><r1 alpha2 r2>...<rnminus1 alphan w>; R = R + {pi}; } for (each state q in M.K) if (w!= M.s) R = R + <q c q> -> epsilon; G = (M.V, M.Sigma, M.S, R); return G; } 10

Pushdown Automata: Halting Computation C of PDA M halts if at least one of the following holds: 1. C is an accepting computation 2. C ends in a configuration from which there is no transition in M halts on string w if every computation of M on w halts M rejects w if it halts on w and does not accept w PDAs differ from FAs in the following aspects 1. Some CFGs cannot be represented by deterministic PDAs 2. A given PDA may not (a) Halt (b) Consume all input 3. There is no algorithm for minimizing PDAs Two ways of dealing with these difficulties 1. Formal solutions that do not restrict the class of the language being considered These approaches - in general - restrict the representation of the language This, in turn, affects the usefulness of resulting parse trees 2. Practical solutions Moral: In general, these apply only to restricted subclasses of a language class Such subclasses are usually large enough to provide useful solutions Cannot guarantee solutions to problems like 1. Is string w L(M)? 2. Can a parse tree be generated for w? 3. Can a parse tree be generated for w in time linear wrt w? 4. Is w L(M)? 11