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

Similar documents
Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack

CISC4090: Theory of Computation

CSE 105 THEORY OF COMPUTATION

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

CSE 105 THEORY OF COMPUTATION

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

Fundamentele Informatica II

Intro to Theory of Computation

(pp ) PDAs and CFGs (Sec. 2.2)

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

MTH401A Theory of Computation. Lecture 17

CS Pushdown Automata

Pushdown Automata: Introduction (2)

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

Homework. Turing Machines. Announcements. Plan for today. Now our picture looks like. Languages

(pp ) PDAs and CFGs (Sec. 2.2)

CSE 105 THEORY OF COMPUTATION

Pushdown Automata (Pre Lecture)

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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

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

CS 301. Lecture 18 Decidable languages. Stephen Checkoway. April 2, 2018

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

The Pumping Lemma. for all n 0, u 1 v n u 2 L (i.e. u 1 u 2 L, u 1 vu 2 L [but we knew that anyway], u 1 vvu 2 L, u 1 vvvu 2 L, etc.

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

CPS 220 Theory of Computation Pushdown Automata (PDA)

CS375: Logic and Theory of Computing

Theory of Computation

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

UNIT-VI PUSHDOWN AUTOMATA

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

Computational Models - Lecture 5 1

The Idea of a Pushdown Automaton

Pushdown Automata (2015/11/23)

Pushdown Automata. Chapter 12

Einführung in die Computerlinguistik

5 Context-Free Languages

Pushdown Automata. Reading: Chapter 6

Computational Models - Lecture 4

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,

Context-Free Languages

Automata Theory - Quiz II (Solutions)

Section: Pushdown Automata

Push-down Automata = FA + Stack

Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8

SCHEME FOR INTERNAL ASSESSMENT TEST 3

CSE 105 Theory of Computation

NPDA, CFG equivalence

PUSHDOWN AUTOMATA (PDA)

Properties of Context-Free Languages. Closure Properties Decision Properties

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

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

Computational Models: Class 5

input tape head moves current state a a

Languages, regular languages, finite automata

CS481F01 Prelim 2 Solutions

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

Accept or reject. Stack

Turing Machines Part III

Theory of Computation Turing Machine and Pushdown Automata

CMP 309: Automata Theory, Computability and Formal Languages. Adapted from the work of Andrej Bogdanov

Theory of Computation - Module 3

Cliff s notes for equivalence of CFLs and L(PDAs) LisaCFL L = L(M) for some PDA M L=L(M)forsomePDAM L = L(G) for some CFG G

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

Equivalence of CFG s and PDA s

Computability and Complexity

Some notes on the equivalence of npda s and context-free grammars.

Context Free Languages and Grammars

Foundations of Informatics: a Bridging Course

Deterministic Finite Automata. Non deterministic finite automata. Non-Deterministic Finite Automata (NFA) Non-Deterministic Finite Automata (NFA)

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Section 1 (closed-book) Total points 30

MA/CSSE 474 Theory of Computation

Automata Theory CS F-08 Context-Free Grammars

Context-free Languages and Pushdown Automata

CISC 4090 Theory of Computation

Automata and Computability. Solutions to Exercises

Computational Models Lecture 5

CISC 4090 Theory of Computation

CS481F01 Solutions 6 PDAS

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

Undecidable Problems and Reducibility

Computational Models - Lecture 4 1

DM17. Beregnelighed. Jacob Aae Mikkelsen

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Lecture 17: Language Recognition

Theory of Computation

Automata and Computability. Solutions to Exercises

Introduction to Languages and Computation

Computational Models: Class 3

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

NODIA AND COMPANY. GATE SOLVED PAPER Computer Science Engineering Theory of Computation. Copyright By NODIA & COMPANY

Theory of Computation (IV) Yijia Chen Fudan University

Automata: a short introduction

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

2.1 Solution. E T F a. E E + T T + T F + T a + T a + F a + a

CMSC 330: Organization of Programming Languages

Transcription:

Homework Context Free Languages PDAs and CFLs Homework #3 returned Homework #4 due today Homework #5 Pg 169 -- Exercise 4 Pg 183 -- Exercise 4c,e,i (use JFLAP) Pg 184 -- Exercise 10 Pg 184 -- Exercise 12 Pg 185 -- Exercise 17 (use mathematical formal definitions) Due 10 / 17 Announcements Final Exam Dates have been announced Friday, November 17 10:15am -- 12:15pm Room 70-3435 Before We Start Exam 1 will be returned in 2nd half Any questions? Conflicts? Let me know. Plan for today 1st half PDAs and CFLs 2nd half PDA homework session / Exam 1 Languages Recall. What is a language? What is a class of languages? 1

The Language Bubble Context Free Languages Regular Languages Finite Languages Context Free Languages Context Free Languages(CFL) is the next class of languages outside of Regular Languages: Language / grammar: Context Free Grammar Machine for accepting: Pushdown Automata Context Free Grammars Context Free Grammars Let s formalize this a bit: A context free grammar (CFG) is a 4-tuple: (V, T, S, P) where V is a set of variables T is a set of terminals V and Σ are disjoint (I.e. V Σ = ) S V, is your start symbol Let s formalize this a bit: Production rules Of the form A β where A V β (V ) * string with symbols from V and We say that γ can be derived from α in one step: A β is a rule α = α 1 A α 2 γ = α 1 β α 2 α γ Pushdown Automata Input tape 1 2 3 5 4 State machine Stack Pushdown Automata Let s formalize this: A pushdown automata (PDA) is a 7-tuple: M = (Q, Σ, Γ, q 0, Z 0, A, δ) where Q = finite set of states Σ = tape alphabet Γ = stack alphabet (may have symbols in common w/ Σ) q 0 Q = start state Z 0 Γ = initial stack symbol A Q = set of accepting states δ = transition function 2

Pushdown Automata About this transition function δ: During a move of a PDA: At most one character is read from the input tape λ transitions are okay The topmost character is popped from the stack The machine will move to a new state based on: The character read from the tape The character popped off the stack The current state of the machine 0 or more symbols from the stack alphabet are pushed onto the stack. Plan for today Show that PDAs and CFGs are equivalent. Questions? Equivalence of CFG and PDA Given a CFG, G, construct a PDA M, such that L(M) = L(G) Given a PDA, M, define a CGF, G such that L(G) = L(M) Given: A context free grammar G Construct: A pushdown automata M Such that: Language generated by G is the same as Language accepted by M. Basic idea Use the stack of the PDA to simulate the derivation of a string in the grammar. Push S (start variable of G) on the stack From this point on, there are two moves the PDA can make: If a variable A is on the top of the stack, pop it and push the right-hand side of a production A β from G. If a terminal, a is on the top of the stack, pop it and match it with whatever symbol is being read from the tape. Observations: There can be many productions that have a given variable on the left hand side: S λ 0S1 1S0 In these cases, the PDA must choose which string to push onto the stack after pushing a variable. I.e. the PDA being constructed in nondeterministic. 3

More observations: A string will only be accepted if: After a string is completely read The stack is empty Let s formalize this: Let G = (V, T, S, P) be a context free grammar. We define a pushdown automata M = (Q, Σ, Γ,δ, q 0, z,f) Such that L(M) = L(G) Define M as follows: Q = { q 0, q 1, q 2 } q o will be the start state q 1 will be where all the work gets done q 2 will be the accepting state Γ = V { z } z (V ) A = { q 2 } Transition function δ is defined as follows: δ (q 0, λ, z ) = { (q 1, Sz) } To start things off, push S onto the stack and immediately go into state 1 δ (q 1, λ, A) = { (q 1, α ) A α is a production of G} for all variables A Pop and replace variable. Transition function δ is defined as follows: δ (q 1, a, a) = { (q 1, λ )} for all terminals a Pop and match terminal. δ (q 1, λ, z) = { (q 2, z) } After all reading is done, accept only if stack is empty. No other transitions exist for M Let s look at an example: Remember the CFG for odd length palindromes: S a b S a S a b S b Let s convert this to a PDA. 4

Example: M = (Q, Σ, Γ,δ, q 0, z,f) Q = { q 0, q 1, q 2 } Σ = { a, b } Γ = { a, b, S, z } F = { q 2 } λ, Z 0 / SZ 0 λ, Z 0 / Z 0 q 0 q 1 q 2 Lots of moves (see next slide) State Tape input Stack Move(s) q 1 λ S (q 1, a) (q 1, b) (q 1, asa) (q 1, bsb) q 1 a a (q 1, λ) q 1 b b (q 1, λ) Let s run M on abbba (q 0, abbba, Z) a(q 1, abbba, SZ) a(q 1, abbba, asaz) // push a(q 1, bbba, SaZ) // match a(q 1, bbba, bsbaz) // push a(q 1, bba, SbaZ) // match a(q 1, bba, bbaz) // push a(q 1, ba, baz) // match a(q 1, a, az) // match a(q 1, ε, Z) // match a(q 2, ε, Z ) // accept Questions? Given: A pushdown automata M Define: A context free grammar G Such that: Language accepted by M is the same as Language generated by G 5

Pushdown Automata Pushdown Automata Strings accepted by a PDA by Final State Start at (q 0, x, z) Strings accepted by a PDA by Empty Stack Start at (q 0, x, z) Start state q 0 X on the input tape Empty stack End with (q, λ, β) End in an accepting state (q F) All characters of x have been read Some string on the stack (doesn t matter what). Start state q 0 X on the input tape Empty stack End with (q, λ, λ) End in any state All characters of x have been read Stack is empty Final State vs. Empty Stack The two means by which a PDA can accept are equivalent wrt the class of languages accepted Given a PDA M such that L = L(M), there exists a PDA M such that L = N(M ) Given a PDA M such that L = N(M), there exists a PDA M such that L = L(M ) Given: A pushdown automata M that accepts by empty stack Define: A context free grammar G Such that: Language accepted by M is the same as Language generated by G Basic idea We define variables in G to be triplets: [p, A, q] will represent a variable, that can generate all strings x that: Upon reading x on the PDA tape will Take you from state p to state q in the PDA and Have a net result of popping A off the stack Note that it may take many moves to get there. Productions of G 1. For all states q in M, add the production S [q 0 zq] Following these productions will generate all strings that start at q o, and result in an empty stack. Final state is not important. In other words, all strings accepted by M. 6

a, A / B 1 B m q q 1 More Productions of G 2. For every q, q 1 Q, a Σ {λ} and A Γ If δ (q, a, A) contains (q 1, λ) then add [qaq 1 ] a Even More Productions of G 3. For every q, q 1 Q, a Σ {λ} and A Γ If δ (q, a, A) contains (q 1, B 1 B 2 B m ) then For every possible sequence of states q 2, q m+1 Add [qaq m+1 ] a[q 1 B 1 q 2 ] [q 2 B 2 q 3 ] [q m B m q m+1 ] Meaning you can get from q to q 1 while popping A from the stack by reading an a. Meaning: one way to pop A off the stack and to get from q to q m+1 is to read an a use some input to pop B 1 off the stack (bring you from q 1 to q 2 in the process), While in q 2, use some input to pop B 2 off the stack (bringing you to q 3 in the process) And so on One can show by induction (though we won t) that [qap] * x iff (q, x, A) a * (p, λ, λ) More specifically [q 0 zp] * x and since we added the productions S [q 0 zp] for all p, then x L(G) On the flip side S [q 0 zp] will always be the first production of any derivation of G (q 0, x, z) a * (p, λ, λ) So x is accepted by empty stack x L(M) Example L = { 0 i 1 j i j 1} 1, X / λ q q 0 1 0,Z / XZ 0,X / XX λ,x / λ 1,X / λ λ,z / λ Example M = (Q, Σ, Γ,δ, q 0, z,f) Q = { q 0, q 1 } Σ = { 0, 1 } Γ = { X, Z } z = Z F = Corresponding CFG Type 1 productions For all states q, S [q 0 zq] S [q 0 Zq 1 ] S [q 0 Zq 0 ] 7

Corresponding CFG Type 2 productions If δ (q, a, A) contains (q 1, λ) then add [qaq 1 ] a δ (q 0, 1, X) = (q 1, λ) [q 0 Xq 1 ] 1 δ (q 1, 1, X) = (q 1, λ) [q 1 Xq 1 ] 1 δ (q 1, ε, X) = (q 1, λ) [q 1 Xq 1 ] λ δ (q 1, ε, Z) = (q 1, λ) [q 1 Zq 1 ] λ Corresponding CFG Type 3 production If δ (q, a, A) contains (q 1, B 1 B 2 B m ) then [qaq m+1 ] a[q 1 B 1 q 2 ] [q 2 B 2 q 3 ] [q m B m q m+1 ] Transitions to consider: δ (q 0, 0, Z) = (q 0, XZ) δ (q 0, 0, X) = (q 0, XX) q b q 0 q 0 q 1 q 0 q 0 q 1 q 1 q 1 Corresponding CFG Type 3 productions q c δ (q 0, 0, X) = (q 0, XX) [q 0 X q c ] 0 [q 0 Xq b ][q b Xq c ] Corresponding CFG Type 3 productions δ (q 0, 0, X) = (q 0, XX) Add productions [q 0 Xq 0 ] 0[q 0 Xq 0 ][q 0 Xq 0 ] [q 0 Xq 0 ] 0[q 0 Xq 1 ][q 1 Xq 0 ] [q 0 Xq 1 ] 0[q 0 Xq 0 ][q 0 Xq 1 ] [q 0 Xq 1 ] 0[q 0 Xq 1 ][q 1 Xq 1 ] q b q 0 q 0 q 1 q 0 q 0 q 1 q 1 q 1 Corresponding CFG Type 3 productions q c δ (q 0, 0, Z) = (q 0, XZ) [q 0 Z q c ] 0 [q 0 Xq b ][q b Zq c ] Corresponding CFG Type 3 productions δ (q 0, 0, Z) = (q 0, XZ) Add productions [q 0 Zq 0 ] 0[q 0 Xq 0 ][q 0 Zq 0 ] [q 0 Zq 0 ] 0[q 0 Xq 1 ][q 1 Zq 0 ] [q 0 Zq 1 ] 0[q 0 Xq 0 ][q 0 Zq 1 ] [q 0 Zq 1 ] 0[q 0 Xq 1 ][q 1 Zq 1 ] 8

Complete grammar G = (V, Σ, S, P) V = { S, [q 0 Xq 0 ], [q 0 Zq 0 ], [q 0 Xq 1 ], [q 0 Zq 1 ], [q 1 Xq 0 ], [q 1 Zq 0 ], [q 1 Xq 1 ], [q 1 Zq 1 ], [q 1 Xq 1 ] } P = S [q 0 Zq 1 ] (1) [q 0 Xq 0 ] 0[q 0 Xq 0 ][q 0 Xq 0 ] (7) S [q 0 Zq 0 ] (2) [q 0 Xq 0 ] 0[q 0 Xq 1 ][q 1 Xq 0 ] (8) [q 0 Xq 1 ] 1 (3) [q 0 Xq 1 ] 0[q 0 Xq 0 ][q 0 Xq 1 ] (9) [q 1 Xq 1 ] 1 (4) [q 0 Xq 1 ] 0[q 0 Xq 1 ][q 1 Xq 1 ] (10) [q 1 Xq 1 ] λ (5) [q 0 Zq 0 ] 0[q 0 Xq 0 ][q 0 Zq 0 ] (11) [q 1 Zq 1 ] λ (6) [q 0 Zq 0 ] 0[q 0 Xq 1 ][q 1 Zq 0 ] (12) [q 0 Zq 1 ] 0[q 0 Xq 0 ][q 0 Zq 1 ] (13) [q 0 Zq 1 ] 0[q 0 Xq 1 ][q 1 Zq 1 ] (14) Summary Summary What have we learned? (We really don t need to see the CFG corresponding to a PDA, do we?) What we have really learned? Given a CFG, we can build a PDA that accepts the same language generated by the CFG Given a PDA, we can define a CFG that can generate the language accepted by the PDA. Looking for a machine to accept CGLs? The pushdown automata fits the bill! Break 9