(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

PUSHDOWN AUTOMATA (PDA)

Introduction to Theory of Computing

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

Pushdown Automata (Pre Lecture)

Computational Models - Lecture 5 1

CFG and PDA accept the same languages

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

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.

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

CISC4090: Theory of Computation

MTH401A Theory of Computation. Lecture 17

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

Lecture 17: Language Recognition

Theory of Computation (VI) Yijia Chen Fudan University

Equivalence of CFG s and PDA s

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

Theory of Computation (IV) Yijia Chen Fudan University

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

Miscellaneous. Closure Properties Decision Properties

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

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

Intro to Theory of Computation

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

Push-down Automata = FA + Stack

5 Context-Free Languages

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Accept or reject. Stack

Computational Models Lecture 5

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

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

CSE 105 THEORY OF COMPUTATION

Section 1 (closed-book) Total points 30

CPS 220 Theory of Computation Pushdown Automata (PDA)

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

Pushdown Automata: Introduction (2)

Pushdown Automata. Reading: Chapter 6

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

Undecidable Problems and Reducibility

Theory of Computation

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

Computational Models: Class 5

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

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,

Theory of Computation Turing Machine and Pushdown Automata

Pushdown Automata. Chapter 12

Context-Free Languages

MA/CSSE 474 Theory of Computation

NPDA, CFG equivalence

Computability and Complexity

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

CPSC 421: Tutorial #1

CS481F01 Prelim 2 Solutions

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

ECS 120 Lesson 15 Turing Machines, Pt. 1

Einführung in die Computerlinguistik

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

CSE 105 THEORY OF COMPUTATION

Context-free Languages and Pushdown Automata

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

CSE 105 THEORY OF COMPUTATION

Finite Automata. Mahesh Viswanathan

CS481F01 Solutions 6 PDAS

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

Pushdown Automata (2015/11/23)

Introduction to Languages and Computation

Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.

CS375: Logic and Theory of Computing

Nondeterministic Finite Automata

CSCE 551: Chin-Tser Huang. University of South Carolina

UNIT-VI PUSHDOWN AUTOMATA

CDM Parsing and Decidability

Part I: Definitions and Properties

Foundations of Informatics: a Bridging Course

CpSc 421 Final Exam December 15, 2006

CISC 4090 Theory of Computation

THE AUSTRALIAN NATIONAL UNIVERSITY Second Semester COMP2600/COMP6260 (Formal Methods for Software Engineering)

Fundamentele Informatica II

Theory of Computation (II) Yijia Chen Fudan University

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

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

Theory of Computation

Solution Scoring: SD Reg exp.: a(a

Automata and Computability. Solutions to Exercises

Most General computer?

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.

CISC 4090 Theory of Computation

C2.1 Regular Grammars

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

C2.1 Regular Grammars

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

Automata and Computability. Solutions to Exercises

Nondeterministic Finite Automata

CS21 Decidability and Tractability

Chapter Five: Nondeterministic Finite Automata

Turing Machines Part III

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 construct 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 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 a rule for A Pop the variable Push the RHS (in reverse order) 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.11(0 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 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->xyz Shorthand 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 5

(p. 121) Now prove if PDA accepts L, L must be CF Again by construction of a CFG from PDA 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 use any terminal x Transition from original state pushes x: a,ε->x Transition from new state pops that x: ε,x->ε 6

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 For each p,q,r in Q Then add grammar rule A pq -> A pr A rs For each p in Q Then add grammar rule A pp -> ε See p.122 Figs. 2.28 for notional pictures of stack height 7

(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 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 with k+1 steps Two possibilities 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.r. it pops u 8

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 context free. 9