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

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

Properties of Context-Free Languages. Closure Properties Decision Properties

Context-Free and Noncontext-Free Languages

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

Computational Models - Lecture 4

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

Miscellaneous. Closure Properties Decision Properties

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

V Honors Theory of Computation

Sri vidya college of engineering and technology

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Context-Free Languages (Pre Lecture)

Theory of Computation (Classroom Practice Booklet Solutions)

MA/CSSE 474 Theory of Computation

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

Lecture 7 Properties of regular languages

The Pumping Lemma and Closure Properties

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

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

CS 154, Lecture 3: DFA NFA, Regular Expressions

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

More Properties of Regular Languages

Theory of Computation

Unit 6. Non Regular Languages The Pumping Lemma. Reading: Sipser, chapter 1

NPDA, CFG equivalence

Properties of Context-Free Languages

Computational Models - Lecture 5 1

CPS 220 Theory of Computation

Pumping Lemma for CFLs

Formal Languages, Automata and Models of Computation

CSE 355 Test 2, Fall 2016

MA/CSSE 474 Theory of Computation

CS 455/555: Finite automata

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION. Spring 2018 review class

Ogden s Lemma for CFLs

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

The View Over The Horizon

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

Context-Free Grammars (and Languages) Lecture 7

Properties of Context Free Languages

CS20a: summary (Oct 24, 2002)

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

Final exam study sheet for CS3719 Turing machines and decidability.

Finite Automata and Regular languages

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

Closure Properties of Regular Languages. Union, Intersection, Difference, Concatenation, Kleene Closure, Reversal, Homomorphism, Inverse Homomorphism

Decidability (What, stuff is unsolvable?)

Pushdown Automata. Chapter 12

GEETANJALI INSTITUTE OF TECHNICAL STUDIES, UDAIPUR I

CS375 Midterm Exam Solution Set (Fall 2017)

Context Free Languages: Decidability of a CFL

Foundations of Informatics: a Bridging Course

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

Before we show how languages can be proven not regular, first, how would we show a language is regular?

Closure under the Regular Operations

CSE 105 THEORY OF COMPUTATION

CS 154. Finite Automata vs Regular Expressions, Non-Regular Languages

Comment: The induction is always on some parameter, and the basis case is always an integer or set of integers.

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

Closure Properties of Regular Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

Inf2A: Converting from NFAs to DFAs and Closure Properties

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

The Pumping Lemma for Context Free Grammars

Properties of Regular Languages. BBM Automata Theory and Formal Languages 1

Functions on languages:

SCHEME FOR INTERNAL ASSESSMENT TEST 3

CISC4090: Theory of Computation

Properties of Regular Languages (2015/10/15)

Lecture 17: Language Recognition

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

Finite Automata Part Two

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,

Einführung in die Computerlinguistik

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

Context Free Languages and Grammars

CS500 Homework #2 Solutions

6.8 The Post Correspondence Problem

CPSC 421: Tutorial #1

Computational Models: Class 5

CSE 105 THEORY OF COMPUTATION

} Some languages are Turing-decidable A Turing Machine will halt on all inputs (either accepting or rejecting). No infinite loops.

CS 154. Finite Automata, Nondeterminism, Regular Expressions

Regular Expressions. Definitions Equivalence to Finite Automata

CSE 105 THEORY OF COMPUTATION

10. The GNFA method is used to show that

Context-Free Grammars and Languages. Reading: Chapter 5

Automata and Computability. Solutions to Exercises

DM17. Beregnelighed. Jacob Aae Mikkelsen

Computational Models Lecture 5

Concatenation. The concatenation of two languages L 1 and L 2

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

Theory of Computation (IV) Yijia Chen Fudan University

Computational Models: Class 3

I have read and understand all of the instructions below, and I will obey the University Code on Academic Integrity.

Transcription:

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

Closure Properties of CFLs CFLs are closed under: Union Concatenation Kleene closure Reversal CFLs are not closed under intersection, difference, or complement

Closure Under Union Let L and M be CFLs with grammars G and H, respectively Assume G and H have no variables in common Rename the variables if necessary Names of variables do not affect the language Let S 1 and S 2 be the start symbols of G and H Form a new grammar for L M by combining all the symbols and productions of G and H

Closure Under Union Then, add a new start symbol S Add productions S S 1 S 2 In the new grammar, all derivations start with S The first step replaces S with either S 1 or S 2 In the first case, the result must be a string in L(G) = L, and in the second case, the result must be a string in L(H) = M

Warning: Be Careful Using Union If L 1 and L 2 are context-free, then so is L 3 = L 1 L 2 But what if L 3 and L 1 are context-free? What can we say about L 2? L 2 may or may not be context-free For example, consider the following: a n b n c* = a n b n c* a n b n c n L 3 L 1 L 2

Closure Under Concatenation Let L and M be CFLs with grammars G and H, respectively Assume G and H have no variables in common Let S 1 and S 2 be the start symbols of G and H Form a new grammar for LM by starting with all symbols and productions of G and H Add a new start symbol S and production S S 1 S 2 Every derivation from S results in a string in L followed by one in M

Closure Under Star Let L have grammar G, with start symbol S 1 Form a new grammar for L* by introducing to G a new start symbol S and the productions S S 1 S ε A rightmost derivation from S generates a sequence of zero or more S 1 s, each of which generates some string in L

Closure of Under Reversal If L is a CFL with grammar G, form a grammar for L R by reversing the body of every production Example: Let G have S 0S1 01 The reversal of L(G) has grammar S 1S0 10

CFLs are Not Closed Under Intersection Unlike the regular languages, the class of CFLs is not closed under intersection For example, we know that L 1 = {0 n 1 n 2 n : n 1} is not context-free (use the pumping lemma) However, L 2 = {0 n 1 n 2 i : n 1, i 1} is context-free CFG: S AB, A 0A1 01, B 2B 2 So is L 3 = {0 i 1 n 2 n : n 1, i 1} But L 1 = L 2 L 3 is not context-free

CFLs are Not Closed Under Difference We can prove something more general: Any class of languages that is closed under difference is also closed under intersection Proof: L M = L (L M) Thus, if CFL s were closed under difference, they would also be closed under intersection, but they are not

CFLs are Not Closed Under Complement L 1 L 2 = ( L 1 L 2 ) Recall that the context-free languages are closed under union So if they were closed under complement, they would also be closed under intersection (which they are not)

CFLs are Not Closed Under Complement Recall A n B n C n = {a n b n c n : n 0}. Now consider L = (A n B n C n ), which is L 1 L 2, where: L 1 = {w {a, b, c}* : the letters are out of order} L 2 = {a i b j c k : i, j, k 0 and (i j or j k)} (in other words, unequal numbers of a s, b s, and c s) A simple DFA can recognize L 1. L 2 can be built similar to the one we created for accepting strings with an unequal number of a s and b s. Thus, (A n B n C n ) is context-free, whereas A n B n C n is not context-free (as we already proved).

Intersection With a Regular Language The intersection of two context-free languages may or may not be context-free Closure means the result is guaranteed to be context-free But the intersection of a CFL with a regular language is always context-free The proof involves running an NFA in parallel with a PDA, and noting that the combination is a PDA that accepts by final state

Difference With a Regular Language The difference (L C L R ) between a context-free language L C and a regular language L R is always context-free Proof: L C L R = L C L R If L R is regular then so is L R If L C is context-free then so is L C L R However, the difference (L R L C ) between a regular language L R and a context-free language L C may or may not be context-free

Difference With a Regular Language Example: Let L = {a n b n : n 0 where n 1776} Alternatively, L = {a n b n : n 0} {a 1776 b 1776 } We know that {a n b n : n 0} is not regular (but it is context-free) {a 1776 b 1776 } is regular (because it is finite) Therefore, L must also be context-free

Using Closures with Pumping L = {ww : w {a, b}*} L is not context-free, but using the pumping lemma for CFLs to prove that it is not can be tricky: suppose we choose w = (ab) k Then we could break up string w into uvxyz, by letting v = ab and y = ab, and this pumps fine w w ababab abababababab ababab

Using Closures with Pumping L = {ww : w {a, b}*} continued Suppose we choose w = a k ba k b This also pumps fine if v is a in the first group of a s, and y is a in the second group of a s w aaaaaa aaaaaaabaaaa aaaaaaaaab w

Using Closures with Pumping L = {ww : w {a, b}*} continued Consider L 2 = L a*b*a*b* = a n b m a n b m If L were context-free, then L 2 would also be context-free because context-free languages are closed under intersection with regular languages But L 2 is not context-free, which we have already proven using the pumping lemma for context-free languages Choose a k b k a k b k for pumping Therefore L cannot be context-free either

Using Closures with Pumping Let L = {w {a, b, c}* : # a (w) = # b (w) = # c (w) } If L were context-free, then L 2 = L a*b*c* would also be context-free But L 2 = A n B n C n If L were context-free, then L 2 would also be contextfree because it is the intersection of a context-free language with a regular language But L 2 is not context-free, as we have already proven using the pumping lemma for context-free languages Therefore L cannot be context-free either