CFG and PDA accept the same languages

Similar documents
(pp ) PDAs and CFGs (Sec. 2.2)

(pp ) PDAs and CFGs (Sec. 2.2)

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

PUSHDOWN AUTOMATA (PDA)

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

Computational Models - Lecture 5 1

Equivalence of CFG s and PDA s

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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,

Lecture 17: Language Recognition

Introduction to Theory of Computing

UNIT-VI PUSHDOWN AUTOMATA

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

Automata and Formal Languages. Push Down Automata. Sipser pages Lecture 13. Tim Sheard 1

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

Accept or reject. Stack

Pushdown Automata. Reading: Chapter 6

Theory of Computation (IV) Yijia Chen Fudan University

Pushdown Automata (Pre Lecture)

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

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

CISC4090: Theory of Computation

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

5 Context-Free Languages

CSE 105 THEORY OF COMPUTATION

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

The Idea of a Pushdown Automaton

CSE 105 THEORY OF COMPUTATION

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

Einführung in die Computerlinguistik

Intro to Theory of Computation

Computational Models: Class 5

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

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

This lecture covers Chapter 6 of HMU: Pushdown Automata

Push-down Automata = FA + Stack

Next: Pushdown automata. Pushdown Automata. Informal Description PDA (1) Informal Description PDA (2) Formal Description PDA

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

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Theory of Computation (VI) Yijia Chen Fudan University

CS481F01 Prelim 2 Solutions

Pushdown Automata: Introduction (2)

Computational Models - Lecture 4

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

Computational Models Lecture 5

Miscellaneous. Closure Properties Decision Properties

Theory of Computation

CPS 220 Theory of Computation Pushdown Automata (PDA)

SCHEME FOR INTERNAL ASSESSMENT TEST 3

MTH401A Theory of Computation. Lecture 17

Fundamentele Informatica II

CS Pushdown Automata

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

Undecidable Problems and Reducibility

Theory of Computation Turing Machine and Pushdown Automata

Lecture III CONTEXT FREE LANGUAGES

Section 1 (closed-book) Total points 30

Automata Theory - Quiz II (Solutions)

Introduction to Bottom-Up Parsing

Context Free Grammars

Introduction to Bottom-Up Parsing

CS5371 Theory of Computation. Lecture 14: Computability V (Prove by Reduction)

Theory of Computation

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

Pushdown Automata (2015/11/23)

Introduction to Bottom-Up Parsing

CpSc 421 Final Exam December 15, 2006

Context-Free Languages

Introduction to Bottom-Up Parsing

What we have done so far

CS 373: Theory of Computation. Fall 2010

Context-Free Grammars (and Languages) Lecture 7

Properties of Context-Free Languages. Closure Properties Decision Properties

CDM Parsing and Decidability

Properties of Context-Free Languages

Theory of Computation - Module 3

Reducability. Sipser, pages

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Outline 1 PCP. 2 Decision problems about CFGs. M.Mitra (ISI) Post s Correspondence Problem 1 / 10

3.13. PUSHDOWN AUTOMATA Pushdown Automata

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Computability and Complexity

Context-Free and Noncontext-Free Languages

MA/CSSE 474 Theory of Computation

CSE 105 THEORY OF COMPUTATION

A Reduction of Finitely Expandable Deep Pushdown Automata

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

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

Theory Of Computation UNIT-II

Context-Free Grammars and Languages. Reading: Chapter 5

CSE 105 THEORY OF COMPUTATION

Context-Free Languages (Pre Lecture)

Computational Models - Lecture 4 1

CISC 4090 Theory of Computation

CPSC 421: Tutorial #1

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

Turing Machines Part III

Automata Theory. CS F-10 Non-Context-Free Langauges Closure Properties of Context-Free Languages. David Galles

Transcription:

Automata and Formal Languages CFG and PDA accept the same languages Sipser pages 115-122 Lecture 14 Tim Sheard 1

Equivalence of CFGs and PDAs The equivalence is expressed by two theorems. Theorem 1. Every context-free language is accepted by some PDA. Theorem 2. For every PDA M, the language L(M) is context-free. We will describe the constructions, see some examples and proof ideas.

Lemma 2.21 (page 115 Sipser) Given a CFG G=(V,T,P,S), we define a PDA M=({q start,q loop,q accept },T, T V {$}, δ, q accept,{q start }), with δ given by δ(q start,ε,ε) = {(q loop,s$)} If A V, then δ(q loop,ε,a) = { (q loop,α) A α is in P} If a T, then δ(q loop,a,a) = { (q loop,ε) } δ(q loop,ε,$) = {(q accept,ε)} 1. Note that the stack symbols of the new PDA contain all the terminal and non-terminals of the CFG plus $ 2. There is only 3 states in the new PDA, all the rest of the info is encoded in the stack. 3. Most transitions are on ε, one for each production 4. A few other transitions come one for each terminal. 5. The start and accept state each add a transition and use the marker $

The automaton simulates leftmost derivations of G producing w, accepting by empty stack. For every intermediate sentential form uaα in the leftmost derivation of w (note first that w = uv for some v), M will have Aα on its stack after reading u. At the end (case u=w and v=ε) the stack will be empty.

Example For our old grammar: S SS (S) ε The automaton M will have seven transitions, most from q loop to q loop : 1. δ(q start, ε, ε) = (q loop, S$) 2. δ(q loop, ε,s) = (q loop, SS) S SS 3. δ(q loop, ε,s) = (q loop, (S) ) S (S) 4. δ(q loop, ε,s) = (q loop, ε ) S ε 5. δ(q loop, (, ( ) = (q loop,ε) 6. 7. δ(q loop, ), ) ) = (q loop,ε) δ(q loop,ε,$) = (q accept,ε) 1. Most transitions are on ε, one for each production 2. A few other transitions come one for each terminal 3. Or from the start and accept conditions

Compare Now compare the leftmost derivation S SS (S)S ((S))S (())S (())(S) (())() with the looping part of M's execution on the same string given as input: (q, "(())()",S ) - [1] (q, "(())()",SS ) - [2] (q, "(())()",(S)S ) - [4] (q, "())()",S)S ) - [4] (q, "())()",(S))S ) - [4] (q, "))()",S))S ) - [3] (q, "))()",))S ) - [5] (q, ")()",)S ) - [5] (q, "()",S ) - [2] (q, "()",(S) ) - [4] (q, ")",S) ) - [3] (q, ")",) ) - [5] (q, ε,ε ) 2. δ(q, ε,s) = (q, SS) S SS 3. δ(q, ε,s) = (q, (S) ) S (S) 4. δ(q, ε,s) = (q, ε ) S ε 5. δ(q, (, ( ) = (q,ε) 6. δ(q, ), ) ) = (q,ε) Note we write q for q loop for brevity

Transitions simulate left-most derivation S SS (S)S ((S))S (())S (())(S) (())() (q, "(())()",S ) - [1] (q, "(())()",SS ) - [2] (q, "(())()",(S)S ) - [4] (q, "())()",S)S ) - [4] (q, "())()",(S))S ) - [4] (q, "))()",S))S ) - [3] (q, "))()",))S ) - [5] (q, ")()",)S ) - [5] (q, "()",S ) - [2] (q, "()",(S) ) - [4] (q, ")",S) ) - [3] (q, ")",) ) - [5] (q, ε,ε ) 2. δ(q, Λ,S) = (q, SS) S SS 3. δ(q, Λ,S) = (q, (S) ) S (S) 4. δ(q, Λ,S) = (q, ε ) S ε 5. δ(q, (, ( ) = (q,ε) 6. δ(q, ), ) ) = (q,ε) Note there is an entry in δ for each terminal and non-terminal symbol. The stack operations mimic a top down parse, replacing Non-terminals with the rhs of a production. Note we write q for q loop for brevity

Proof Outline To prove that every string of L(G) is accepted by the PDA M, prove the following more general fact: If S left-most * α then (q loop,uv,s) -* (q loop,v,β) where α = uβ is the leftmost factorization of α (u is the longest prefix of α that belongs to T *, i.e. all terminals). For example: if α = abcwdxa then u = abc, and β = WdXa, since the next symbol after abc is W V (a non-terminal or ε) S lm * abcw then (q loop, abcv,s) -* (q loop,v, W ) The proof is by induction on the length of the derivation of α.

We also need to prove that every string accepted by M belongs to L(G). Again, to make induction work, we need to prove a slightly more general fact: If (q loop,w,a) - * (q loop, ε, ε), then A w For all Stacks A, letting A = Start we have our proof. This time we induct on the length of execution of M that leads from the ID (q loop,w,a$) to (q loop, ε,$).

Grammar from a PDA lemma 2.27 Sipser pg 119 Assume the M = (Q,Σ,Γ,δ,q 0,F) is given, and that it accepts by empty stack. Alter it so that it has the following additional properties 1. It has a single accept state 2. Each transition either 1. Pushes a symbol onto the stack 2. Or pops a symbol off the stack 3. But not both Why can we do this? (hint add new states)

Symbols of the CFG For every pair of states p,q Q Make a variable (non-terminal) A pq A symbol A pq should derive a string if that string cause the PDA to move from state p (with an empty stack) to state q (with an empty stack). Such strings can do the same, starting and ending with the same arbitrary stack. Why?

Productions of the CFG Consider a string x that moves the PDA from p to q on empty stack. 1. The first move must be a push (why?) 2. The last move must be a pop (why?) (p,x,ε) - (_,_,Z) - - (_,_,T) - (q,ε,ε) There are 2 cases (Z=T)=True or (Z=T)=False 1. (Z=T)=True Stack is only empty at the beginning and at the end. (p,ay,ε) - (r,y,z) - - (s,b,t) - (q,ε,ε) A pq a A rs b 2. (Z=T)=False the stack is empty in the middle, at some state r (p,x,ε) - (r,_, ε) - - (q,ε,ε) A pq A pr A rq

Given M = (Q,Σ,Γ,δ,s,{f}) Construct G= (V,Σ,R,S) V = { A pq p,q Q} S = A sf Σ = Σ R = cases 1. For each p Q A pp ε 2. For each p,q,r Q A pq A pr A rq 3. For each p,q,r,s Q T Γ a,b Σ ε (r,t) δ(p,a,ε) (q,ε) δ(s,b,t) A pq a A rs b (p,ay,ε) - (r,y,z) - - (s,b,t) - (q,ε,ε)

Claim 2.30 If A pq generates x, then x can bring the PDA from p with empty stack to q with empty stack A pq x implies (p,x,ε) -* (q,ε,ε) Proof by induction on the number of steps in the derivation A pq x

Claim 2.31 If x can bring the PDA from p with empty stack to q with empty stack then A pq generates x (p,x,ε) -* (q,ε,ε) implies A pq x Proof by induction on the length of (p,x,ε) -* (q,ε,ε)

The following is additional material for the curious. It gives a second construction not described in Sipser. It is not required.

An another algorithm for CFG from a PDA Assume the M = (Q,Σ,Γ,δ,q 0,F) is given, and that it accepts by empty stack. Consider execution of M on an accepted input string. If at some point of the execution of M the stack is Zζ (Z is on top, ζ is the rest of stack) In terms of instantaneous descriptions (state i, input, Zζ)... Then we know that eventually the stack will be ζ. Why? Because we assume the input is accepted, and M accepts by empty stack, so eventually Z must be removed from the stack

(state i, αx, Zζ) (state j, X, ζ) The sequence of moves between these two instants is the net popping of Z from the stack. During this sequence of moves, the stack may grow and shrink several times, some input will be consumed (the α), and M will pass through a sequence of states, from state i to state j.

Net Popping Net popping is fundamental for the construction of a CFG G equivalent to M. We will have a variable (Non-terminal) [qzp] in the CFG G for every triple in (q,z,p) Q Γ Q from the PDA. Recall 1. Q is the set of states 2. Γ Is the set of stack symbols We want the rhs of a production whose lhs is [qzp] to generate precisely those strings w Σ * such that M can move from q to p while reading the input w and doing the net popping of Z. A production like [qzp] ->? This can be also expressed as (q,w,z) -* (p, Λ, Λ) Productions of G correspond to transitions of M.

If (p,ζ) δ(q,a,z), then there is one or more corresponding productions, depending on complexity of ζ. 1. If ζ = Λ, we have [qzp] a 2. If ζ = Y, we have [qzr] a[pyr] for every state r 3. If ζ = YY we have [qzs] a[pyr][ry's], for every pair of states r and s. 4. You can guess the rule for longer ζ.

Example Q = {0,1} S = {a,b} Γ = {X} δ(0,a,x) = { (0,X) } δ (0,Λ,X) = { (1,Λ) } δ (1,b,X) = { (1,Λ) } Q 0 = 0 Z 0 = X F = {}, accepts by empty stack Non-terminals (q,z,p) Q Γ Q (0,'X',0) (0,'X',1) (1,'X',0) (1,'X',1) Productions, At least one from each element in delta (p,z) δ(q,a,z) (0,a,X,0,X) (1,b,X,1,Λ) (0,Λ,X,1,Λ)] 0X0 -> a 0X0 0X1 -> a 0X1 1X1 -> b 0X1 -> Λ