(pp ) PDAs and CFGs (Sec. 2.2)

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

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Introduction to Theory of Computing

CSE 105 THEORY OF COMPUTATION

Computational Models - Lecture 5 1

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

PUSHDOWN AUTOMATA (PDA)

Pushdown Automata (Pre Lecture)

CISC4090: Theory of Computation

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Lecture 17: Language Recognition

CFG and PDA accept the same languages

Theory of Computation (VI) Yijia Chen Fudan University

MTH401A Theory of Computation. Lecture 17

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

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

Theory of Computation (IV) Yijia Chen Fudan University

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

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

CSE 105 THEORY OF COMPUTATION

Computational Models Lecture 5

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

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

Accept or reject. Stack

Equivalence of CFG s and PDA s

Miscellaneous. Closure Properties Decision Properties

Pushdown Automata. Reading: Chapter 6

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

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

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

Computational Models: Class 5

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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

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

NPDA, CFG equivalence

Undecidable Problems and Reducibility

Context-Free Languages

CPS 220 Theory of Computation Pushdown Automata (PDA)

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

MA/CSSE 474 Theory of Computation

Section 1 (closed-book) Total points 30

CSE 105 THEORY OF COMPUTATION

Intro to Theory of Computation

CISC 4090 Theory of Computation

Pushdown Automata (2015/11/23)

Push-down Automata = FA + Stack

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

CSE 105 THEORY OF COMPUTATION

Pushdown Automata: Introduction (2)

5 Context-Free Languages

Theory of Computation Turing Machine and Pushdown Automata

Pushdown Automata. Chapter 12

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Theory of Computation

CISC 4090 Theory of Computation

CPSC 421: Tutorial #1

Theory of Computation (II) Yijia Chen Fudan University

Testing Emptiness of a CFL. Testing Finiteness of a CFL. Testing Membership in a CFL. CYK Algorithm

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

Automata and Computability. Solutions to Exercises

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

Foundations of Informatics: a Bridging Course

Introduction to Languages and Computation

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

Automata and Computability. Solutions to Exercises

Clarifications from last time. This Lecture. Last Lecture. CMSC 330: Organization of Programming Languages. Finite Automata.

CpSc 421 Final Exam December 15, 2006

CS481F01 Prelim 2 Solutions

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

Turing Machines Part III

Einführung in die Computerlinguistik

UNIT-VI PUSHDOWN AUTOMATA

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

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,

V Honors Theory of Computation

Deterministic Finite Automata

Properties of Context-Free Languages. Closure Properties Decision Properties

What languages are Turing-decidable? What languages are not Turing-decidable? Is there a language that isn t even Turingrecognizable?

Context-free Languages and Pushdown Automata

CPS 220 Theory of Computation

CS481F01 Solutions 6 PDAS

CS375: Logic and Theory of Computing

Theory of Computation p.1/?? Theory of Computation p.2/?? We develop examples of languages that are decidable

Fundamentele Informatica II

This lecture covers Chapter 6 of HMU: Pushdown Automata

September 7, Formal Definition of a Nondeterministic Finite Automaton

CSE 311: Foundations of Computing. Lecture 23: Finite State Machine Minimization & NFAs

Nondeterministic Finite Automata

Theory of Computation (IX) Yijia Chen Fudan University

Computability and Complexity

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata (cont )

MA/CSSE 474 Theory of Computation

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

Theory of Computation

Languages. Non deterministic finite automata with ε transitions. First there was the DFA. Finite Automata. Non-Deterministic Finite Automata (NFA)

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.

Problem 2.6(d) [4 pts] Problem 2.12 [3pts] Original CFG:

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Transcription:

(pp. 117-124) PDAs and CFGs (Sec. 2.2) A language is context free iff all strings in L can be generated by some context free grammar Theorem 2.20: L is Context Free iff a PDA accepts it I.e. if L is context free than some PDA accepts it AND if a PDA accepts L, then it is context free Outline of proof: must prove in both directions If language A is CF, then we show construction of a PDA P Use stack to keep the right hand of the intermediate string that includes the leftmost variable on top Create transition rules from grammar rules Use nondeterministic choice of rules to match terminals If a PDA P recognizes L, then L is CF Proof by constructing a CFG from P that matches 1

(p117) 1 st part: if G=(V,,R,S) is CFG for L, then some PDA P=(Q,,Γ,δ,q start,f) accepts it Proof: Build the PDA from the Grammar Overview of proof by construction Assume G=(V,,R,S) Γ ε = V U U {$, ε} = Alphabet + non-terminals + special characters $, ε 3 common states: q start, q loop, q accept F = {q accept } Additional states added for each grammar rule Extra states for rules like A->xyz Self-loop on q loop for rules like A->a A->xyz A->a 2

A->xyz A->a Overview of PDA P in operation (see Fig. 2.26): Start with pushing S$ onto stack (with S on top) Used $ to mark bottom of stack Repeatedly loop around state q loop If stack top is $, enter accept state If stack top is non-terminal A, select an edge (nondeterministically) based on one of rules for A Pop the variable Push the RHS (in reverse order leftmost on top) If stack top is terminal a, next symbol on input must be a to be accepted. Pop a. 3

Formal Construction of P from Grammar Remember PDA transition rule specifies pair (q, x) q is next state x is character to push on stack Q = {q start, q loop, q accept } U E q loop is special state where all grammar rules start & end E = all states generated by grammar rules as discussed below F = { q accept } Add startup transitions to push S$ on start δ(q start, ε, ε) ={(q 1, $)}, q 1 a new state in E δ(q 1, ε, ε) ={(q loop, S)} Note shorthand single edge ε, ε->s$ For each terminal a in, add the following self-loop δ(q loop, a, a) ={(q loop, ε)} (We match the a and pop from stack) To detect acceptance, add rule δ(q loop, ε, $) ={(q accept, ε)} A->a 4

(p.118 ) For kth rule S->u 1 u 2 u L, u i from U V δ(q loop, ε, S) includes (q k,1, u L ), q k,1 a new state Add L-1 transitions to push u 1 u 2 u L-1 onto stack, with u 1 on top as follows δ(q k,1, ε, ε) = {(q k,2, u L-1 )}, q k,2 a new state in E δ(q k,2, ε, ε) = {(q k,3, u L-2 )}, q k,3 a new state in E δ(q k,l-2, ε, ε) = {(q k,l-1, u 2 )}, q l a new state in E δ(q k,l-1, ε, ε) = {(q loop, u 1 )} (p. 119, Fig. 2.23) Book uses shorthand a,s->w (w a string) on edge for sequence of steps: a,s->w n ε,ε->w n-1 ε,ε->w 1 (p. 120) Final machine looks like Fig.2.24 (p.120) Example problem Fig.2.25 (p. 155) See also problems 2.5, 2.7, 2.9 esp. 2.11, and create PDAs from CFGs 2.13, 2.14. 2.46 A->xyz Shorthand 5

(p. 121) Now prove if some PDA accepts L, L must be CF Outline: Generate variable A pq in G for pair of states p & q in P Generate rules for A pq that correspond to strings that cause transitions between the 2 states p & q When stack starts as empty at p And ends as empty at q I.e. nothing needed or left on stack Make start variable that has or of all variables A pq where p is start state and q is a final state This will collect all strings from start to finish 6

Again by construction of a CFG Modify P slightly Ensure a single accept state q accept From any prior accept state, add set of transitions that ensure stack is empty before final accept state Ensure each transition either pushes or pops but not both or neither If a transition does both (δ(q,a,x)->{(q,y)}, ), add new intermediate state Transition from original state does pop: a,x->ε Transition from new state does push: ε,ε->y If a transition does neither (δ(q,ε,ε)->{(q,ε)}, add new state and select any terminal x Transition from original state pushes x: a,ε->x Transition from new state pops that x: ε,x->ε 7

Now construct G = (V,,R,S) the same V = {A pq p, q in Q} 1 symbol for each pair of states S = A q0,qaccept Construct grammar rules R as follows For each p,q,r,s in Q, u in Γ, and a,b in If δ(p, a, ε) contains (r,u) (we are pushing u) And δ(s, b, u) contains (q, ε) (we are popping u) Then add grammar rule A pq -> aa rs b Notes on what this does: From state p we push a u and go to state r From state r we can get to state s via transitions that leave nothing new on stack above u Transition from s to u P pops the original u See P. 122 Fig. 2.29 for stack changes For each p,q,r in Q Then add grammar rule A pq -> A pr A rs Again nothing added from p to q See p. 122 Fig. 2.28 For each p in Q Then add grammar rule A pp -> ε 8

(p. 123) Claim 2.30. If variable A pq generates string x, then x can bring P from state p with an empty stack to state q with empty stack Proof by induction on # of steps in derivation of x Basis step: it took 1 step Only grammar rules with no RHS variables are A pp ->ε i.e. ε must take P from p to p without pushing anything onto empty stack, or attempting a pop Induction Hypothesis: assume true for derivations of length at most k, k 1. Induction step: prove true for derivations of length k+1 Suppose A pq =*>x (x a string) with k+1 steps Two possibilities: A pq -> aa rs b or A pq =>A pr A rq First case: A pq -> aa rs b for some a,b, r,s A rs must have generated y where x = ayb But this must have happened in k steps, so P can go from r to s on empty stack Because A pq -> aa rs b is a rule in G δ(p, a, ε) contains (r, u) for some u i.e. it pushes u and δ(s, b, u) contains (q, ε) i.e. it pops u 9

Thus if P starts at p with empty stack After reading a it goes to state r with u on stack Then reading y brings P to s and leaves u on stack Second case: A pq =>A pr A rq Assume x=yz where A pr =>y in at most k steps A rs =>z in at most k steps Then induction hypothesis says y can bring P from p to r, and z can bring P from r to q, with empty stacks on both ends (p.123) Claim 2.31: If we can bring P from p to q with empty stacks on both sides then A pq generates x (p. 124) Corollary 2.32. Every regular language is CF. Every regular language recognized by some DFA Every DFA is a PDA All languages accepted by a PDA are CFL Sample: convert Fig. 2.15 (p. 115) back to CFG Also attempt: (p. 116) Fig. 2.17 and 2.18, (p.120) 2.26 Also try Probs. 2.11, 2.12, and convert grammars of problems 2.13, 2.14, 2.19, 2.46 10