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

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

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Theory of Computation (IV) Yijia Chen Fudan University

CISC4090: Theory of Computation

Context-Free Languages (Pre Lecture)

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Introduction to Theory of Computing

Pushdown Automata (Pre Lecture)

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

Context-Free and Noncontext-Free Languages

SCHEME FOR INTERNAL ASSESSMENT TEST 3

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

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)

Foundations of Informatics: a Bridging Course

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Context-Free Languages

Solution Scoring: SD Reg exp.: a(a

CSE 355 Test 2, Fall 2016

Theory of Computation

CSE 105 THEORY OF COMPUTATION

Closure Properties of Context-Free Languages. Foundations of Computer Science Theory

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

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL, DPDA PDA)

Computational Models - Lecture 4

Computability and Complexity

CPS 220 Theory of Computation Pushdown Automata (PDA)

Lecture 17: Language Recognition

DM17. Beregnelighed. Jacob Aae Mikkelsen

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

CS500 Homework #2 Solutions

Pumping Lemma for CFLs

MA/CSSE 474 Theory of Computation

MA/CSSE 474 Theory of Computation

CSE 105 THEORY OF COMPUTATION

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

Computational Models - Lecture 5 1

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Properties of Context-Free Languages. Closure Properties Decision Properties

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 17 april Classrum Edition

CS 341 Homework 16 Languages that Are and Are Not Context-Free

Theory of Computation - Module 3

Computational Models - Lecture 4 1

V Honors Theory of Computation

Properties of Context-Free Languages

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

Final exam study sheet for CS3719 Turing machines and decidability.

Fundamentele Informatica II

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

Section 1 (closed-book) Total points 30

Computational Models - Lecture 5 1

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

Miscellaneous. Closure Properties Decision Properties

Pushdown Automata. Reading: Chapter 6

CISC 4090 Theory of Computation

Lecture 3: Nondeterministic Finite Automata

CSE 105 THEORY OF COMPUTATION

Einführung in die Computerlinguistik

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

Computational Models - Lecture 4 1

CISC 4090 Theory of Computation

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

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

CpSc 421 Final Exam December 15, 2006

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 (Classroom Practice Booklet Solutions)

Accept or reject. Stack

Pushdown Automata: Introduction (2)

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

T (s, xa) = T (T (s, x), a). The language recognized by M, denoted L(M), is the set of strings accepted by M. That is,

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

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

CS481F01 Prelim 2 Solutions

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

Computability Theory

CS20a: summary (Oct 24, 2002)

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

Non-context-Free Languages. CS215, Lecture 5 c

Notes for Comp 497 (Comp 454) Week 10 4/5/05

Computational Theory

This lecture covers Chapter 7 of HMU: Properties of CFLs

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

CSE 105 THEORY OF COMPUTATION

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

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

Automata and Computability. Solutions to Exercises

(pp ) PDAs and CFGs (Sec. 2.2)

Computational Models: Class 5

Context-free Languages and Pushdown Automata

Finite Automata and Regular languages

CPSC 421: Tutorial #1

Homework 4. Chapter 7. CS A Term 2009: Foundations of Computer Science. By Li Feng, Shweta Srivastava, and Carolina Ruiz

Context-Free Languages

NPDA, CFG equivalence

Pushdown Automata. Chapter 12

Pushdown Automata (2015/11/23)

The View Over The Horizon

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

Transcription:

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs Harry Lewis October 8, 2013 Reading: Sipser, pp. 119-128.

Pushdown Automata (review) Pushdown Automata = Finite automaton + pushdown store The pushdown store is a stack of symbols of unlimited size which the machine can read and alter only at the top. Finite automaton + pushdown store The pushdown store is a stack of symbols of unlimited size which the machine can read and a only at the top. Input a b b a b a reading head (L to R only, one symbol at a time, or stays put) F.C. pushdown store head can push (add symbols) or pop (remove and check symbols) a d c b b Stack Transitions of PDA are of form (q, σ, γ) (q, γ ), which means: If in state q with σ on the input tape and γ on top of the stack, replace γ by γ on the stack and enter state q while advancing the reading head over σ. Transitions of PDA are of form (q, σ, γ) (q, γ ) If in state q with σ on the input tape and γ on top of the stack, replace γ by γ on the stack 1

(Nondeterministic) PDA for even palindromes {ww R : w {a, b} } (q, a, ε) (q, a) (q, b, ε) (q, b) (q, ε, ε) (r, ε) (r, a, a) (r, ε) (r, b, b) (r, ε) Push a s and b s switch to other state pop a s matching input pop b s matching input So the precondition (q, σ, γ) means that the next σ symbols (0 or 1) of the input are σ and the top γ symbols (0 or 1) on the stack are γ 2

(Nondeterministic) PDA for even palindromes {ww R : w {a, b} } (q, a, ε) (q, a) (q, b, ε) (q, b) (q, ε, ε) (r, ε) (r, a, a) (r, ε) (r, b, b) (r, ε) Push a s and b s switch to other state pop a s matching input pop b s matching input Need to test whether stack empty: push $ at beginning and check at end. (q 0, ε, ε) (q, $) (r, ε, $) (q f, ε) 3

Language recognition with PDAs A PDA accepts an input string If there is a computation that starts in the start state with reading head at the beginning of string and the stack is empty and ends in a final state with all the input consumed A PDA computation becomes blocked (i.e. dies ) if no transition matches both the input and stack 4

Equivalence of CFGs and PDAs Thm: The class of languages recognized by PDAs is the CFLs. I: For every CFG G, there is a PDA M with L(M) = L(G). II: For every PDA M, there is a CFG G with L(G) = L(M). 5

Proof that every CFL is accepted by some PDA Let G = (V, Σ, R, S) We ll allow a generalized sort of PDA that can push strings onto stack. E.g., (q, a, b) (r, cd) 6

Proof that every CFL is accepted by some PDA Let G = (V, Σ, R, S) We ll allow a generalized sort of PDA that can push strings onto stack. E.g., (q, a, b) (r, cd) Then corresponding PDA has just 3 states: q start start state q loop main loop state q accept final state Stack alphabet = V Σ {$} 7

CFL PDA, Continued: The Transitions of the PDA δ(q start, ε, ε) = {(q loop, S$)} Start by putting S$ on the stack, & go to q loop For each A V, δ(q loop, ε, A) = {(q loop, w) : A w R} Remove a variable from the top of the stack and replace it with a corresponding righthand side For each σ Σ, δ(q loop, σ, σ) = {(q loop, ε)} Pop a terminal symbol from the stack if it matches the next input symbol δ(q loop, ε, $) = {(q accept, ε)}. Go to accept state if stack contains only $. 8

Example Consider grammar G with rules {S asb, S ε} (so L(G) = {a n b n : n 0}) Construct PDA M = ({q start, q loop, q accept }, {a, b}, {a, b, S, $}, δ, q start, {q accept }) Transition Function δ : 9

Computation Corresponding to a Derivation Derivation S asb aasbb aabb (p, aabb, ε) (q, aabb, S) (1) (q, aabb, asb) (2) (q, abb, Sb) (4) (q, abb, asbb) (2) (q, bb, Sbb) (4) (q, bb, bb) (3) (q, b, b) (5) (q, ε, ε) (5) 10

Every language accepted by a PDA is CF Proof: See Sipser 11

Closure Properties of CFLs Thm: The CFLs are the languages accepted by PDAs Thm: The CFLs are closed under Union Concatenation Kleene Intersection with a regular set 12

The intersection of a CFL and a regular set is a CFL Pf sketch: Let L 1 be CF and L 2 be regular L 1 = L(M 1 ), M 1 a PDA L 2 = L(M 2 ), M 2 a DFA Q 1 = state set of M 1 Q 2 = state set of M 2 Construct a PDA with state set Q 1 Q 2 which keeps track of computation of both M 1 and M 2 on input. 13

Q: Why doesn t this argument work if M 1 and M 2 are both PDAs? In fact, the intersection of two CFLs is not necessarily CF. And the complement of a CFL is not necessarily CF Q: How to prove that languages are not context free? 14

Pumping Lemma for CFLs Lemma: If L is context-free, then there is a number p (the pumping length) such that any s L of length at least p can be divided into s = uvxyz, where 1. uv i xy i z L for every i 0, 2. v ε or y ε, and 3. vxy p. 15

Pumping Lemma for CFLs (aka Yuvecksy stheorem ;) Lemma: If L is context-free, then there is a number p (the pumping length) such that any s L of length at least p can be divided into s = uvxyz, where 1. uv i xy i z L for every i 0, 2. v ε or y ε, and 3. vxy p. 16

Using the Pumping Lemma to Prove Non-Context-Freeness {a n b n c n : n 0} is not CF. aaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbb cccccccccccccccccccccc What are v, y? Contain 2 kinds of symbols Contain only one kind of symbol Corollary: CFLs not closed under intersection (why?) Corollary: CFLs not closed under complement (why?) Is the the intersection of 2 CFLs or the complement of a CFL sometimes a CFL? 17

What about {a, b} {a n b n : n 0}? 18

Recall: Parse Trees S ab ba ε A as baa B bs abb S ab ba ε A as baa B bs abb a S B a B B Parse tree for aabab, the yield of the tree b S b S a B ε Height = max length path from S to a term Height = max length path from S to a terminal symbol = 6 in above example. b S ε 19

Proof of Pumping Theorem Show that there exists a p such that any string s of length p has a parse tree of the form: S u A z v A y x 20

Proof of Pumping Theorem Show that there exists a p such that any string s of length p has a parse tree of the form: S S u A z u A z v A y v v A A y y x x 21

Finding Repetition in a big parse tree Since RHS of rules have bounded length, long strings must have tall parse trees. A tall parse tree must have a path with a repeated nonterminal. Let p = b m + 1, where: b = max length of RHS of a rule m = # of variables Suppose T is the smallest parse tree for a string s L of length at least p. Then Let h = height of T. Then b h p = b m + 1, h > m, Path of length h in T has a repeated variable. 22

Final annoying details Q: Why is v or y nonempty? Q: How to ensure vxy p? 23

The converse of the CF Pumping Lemma is False Some non-context-free languages satisfy conclusion of Pumping Lemma, e.g.? 24

Some Other CF Closure Properties Let L 1 /L 2 = {w : wx L 1 for some x L 2 }. Then If L is CF and R is regular then L/R is CF 25