Properties of Context-Free Languages. Closure Properties Decision Properties

Similar documents
Ogden s Lemma for CFLs

NPDA, CFG equivalence

Miscellaneous. Closure Properties Decision Properties

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

Properties of Context-Free Languages

Context-Free Languages (Pre Lecture)

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

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

The Pumping Lemma and Closure Properties

Computational Models - Lecture 5 1

CSE 105 THEORY OF COMPUTATION

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

This lecture covers Chapter 7 of HMU: Properties of CFLs

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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

MA/CSSE 474 Theory of Computation

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

V Honors Theory of Computation

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

Sri vidya college of engineering and technology

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

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

Context-Free and Noncontext-Free Languages

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

CSE 105 THEORY OF COMPUTATION

Notes for Comp 497 (454) Week 10

Foundations of Informatics: a Bridging Course

Computational Models - Lecture 4

Properties of Context-free Languages. Reading: Chapter 7

CSE 105 THEORY OF COMPUTATION

Einführung in die Computerlinguistik

Computability Theory

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

DM17. Beregnelighed. Jacob Aae Mikkelsen

Chap. 7 Properties of Context-free Languages

Theory of Computation (Classroom Practice Booklet Solutions)

Pumping Lemma for CFLs

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs

Context Free Languages: Decidability of a CFL

Formal Languages, Automata and Models of Computation

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

Final exam study sheet for CS3719 Turing machines and decidability.

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

Lecture 7 Properties of regular languages

Computational Models: Class 5

Decidability (What, stuff is unsolvable?)

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Functions on languages:

CPSC 421: Tutorial #1

Context Free Language Properties

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

CPS 220 Theory of Computation

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

CSE 355 Test 2, Fall 2016

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Properties of context-free Languages

Automata and Computability. Solutions to Exercises

CS 455/555: Finite automata

Section 1 (closed-book) Total points 30

CSE 105 THEORY OF COMPUTATION. Spring 2018 review class

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

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

Pushdown Automata. Reading: Chapter 6

Theory of Computation

More Properties of Regular Languages

Theory of Computation (IX) Yijia Chen Fudan University

MA/CSSE 474 Theory of Computation

CSE 105 Homework 5 Due: Monday November 13, Instructions. should be on each page of the submission.

The View Over The Horizon

SCHEME FOR INTERNAL ASSESSMENT TEST 3

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

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

CSE 105 Theory of Computation

Properties of Context Free Languages

3515ICT: Theory of Computation. Regular languages

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

Computational Models - Lecture 4 1

CS20a: summary (Oct 24, 2002)

Computability and Complexity

1 More finite deterministic automata

Fooling Sets and. Lecture 5

CS154 Final Examination

Properties of Regular Languages (2015/10/15)

CS481F01 Prelim 2 Solutions

Context Free Grammars

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,

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Introduction to Turing Machines. Reading: Chapters 8 & 9

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

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Undecidable Problems and Reducibility

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

MTH401A Theory of Computation. Lecture 17

Regular expressions. Regular expressions. Regular expressions. Regular expressions. Remark (FAs with initial set recognize the regular languages)

Finite Automata and Regular languages

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

Transcription:

Properties of Context-Free Languages Closure Properties Decision Properties 1

Closure Properties of CFL s CFL s are closed under union, concatenation, and Kleene closure. Also, under reversal, homomorphisms and inverse homomorphisms. But not under intersection or difference. 2

Closure of CFL s Under Union Let L and M be CFL s with grammars G and H, respectively. Assume G and H have no variables in common. Names of variables do not affect the language. Let S 1 and S 2 be the start symbols of G and H. 3

Closure Under Union (2) Form a new grammar for L M by combining all the symbols and productions of G and H. Then, add a new start symbol S. Add productions S -> S 1 S 2. 4

Closure Under Union (3) In the new grammar, all derivations start with S. The first step replaces S by 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 a string in L(H) = M. 5

Closure of CFL s Under Concatenation Let L and M be CFL s 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. 6

Closure Under Concatenation (2) Form a new grammar for LM by starting with all symbols and productions of G and H. Add a new start symbol S. Add production S -> S 1 S 2. Every derivation from S results in a string in L followed by one in M. 7

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. 8

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

Closure of CFL s Under Homomorphism Let L be a CFL with grammar G. Let h be a homomorphism on the terminal symbols of G. Construct a grammar for h(l) by replacing each terminal symbol a by h(a). 10

Example: Closure Under Homomorphism G has productions S -> 0S1 01. h is defined by h(0) = ab, h(1) = ε. h(l(g)) has the grammar with productions S -> abs ab. 11

Nonclosure Under Intersection Unlike the regular languages, the class of CFL s is not closed under. We know that L 1 = {0 n 1 n 2 n n > 1} is not a CFL (use the pumping lemma). However, L 2 = {0 n 1 n 2 i n > 1, i > 1} is. 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. 12

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

Interesting examples L1 = {a n b n c n n >= 1} is not a CFL, but complement of L is. L2 = {ww R w in {0,1}*} is a CFL L3 = {ww w in {0,1}*} is NOT a CFL, but its complement is. L4 = {a i b j c k i > j, i,j,k 1} is a CFL 14

Interesting examples L4 = {a i b j c k i > j, i,j,k 1} is a CFL S XC C cc c X YZ Y ay a Z azb ab 15

L5 = {xy x = y, x not identical to y} S 0 -> 0 1 S 0 1 1 S 0 0 0 S 0 1 0 S 0 0 S 1 -> 0 1 S 1 1 1 S 1 0 0 S 1 1 0 S 1 0 S -> S 0 S 1 S 1 S 0 16

Decision Properties As usual, when we talk about a CFL we really mean a representation for the CFL, e.g., a CFG or a PDA accepting by final state or empty stack. There are algorithms to decide if: 1. String w is in CFL L. 2. CFL L is empty. 3. CFL L is infinite. 17

Non-Decision Properties Many questions that can be decided for regular sets cannot be decided for CFL s. Example: Are two CFL s the same? Example: Are two CFL s disjoint? How would you do that for regular languages? Need theory of Turing machines and decidability to prove no algorithm exists. 18

Testing Emptiness We already did this. We learned to eliminate variables that generate no terminal string. If the start symbol is one of these, then the CFL is empty; otherwise not. 19

Testing Membership Want to know if string w is in L(G). Assume G is in CNF. Or convert the given grammar to CNF. w = ε is a special case, solved by testing if the start symbol is nullable. Algorithm (CYK ) is a good example of dynamic programming and runs in time O(n 3 ), where n = w. 20

Testing Infiniteness The idea is essentially the same as for regular languages. Use the pumping lemma constant n. If there is a string in the language of length between n and 2n-1, then the language is infinite; otherwise not. 21

Intersection with a Regular Language Intersection of two CFL s need not be context free. But the intersection of a CFL with a regular language is always a CFL. Proof involves running a DFA in parallel with a PDA, and noting that the combination is a PDA. PDA s accept by final state. 22

DFA and PDA in Parallel Input DFA PDA Accept if both accept S t a c k Looks like the state of one PDA 23

Formal Construction Let the DFA A have transition function δ A. Let the PDA P have transition function δ P. States of combined PDA are [q,p], where q is a state of A and p a state of P. δ([q,p], a, X) contains ([δ A (q,a),r], ) if δ P (p, a, X) contains (r, ). Note a could be, in which case δ A (q,a) = q. 24

Formal Construction (2) Accepting states of combined PDA are those [q,p] such that q is an accepting state of A and p is an accepting state of P. Easy induction: ([q 0,p 0 ], w, Z 0 ) * ([q,p],, ) if and only if δ A (q 0,w) = q and in P: (p 0, w, Z 0 ) *(p,, ). 25

Closure of CFL s Under Inverse Homomorphism Here, grammars don t help us. But a PDA construction serves nicely. Intuition: Let L = L(P) for some PDA P. Construct PDA P to accept h -1 (L). P simulates P, but keeps, as one component of a two-component state a buffer that holds the result of applying h to one input symbol. 26

Architecture of P Input: 0 0 1 1 h(0) Buffer State of P Read first remaining symbol in buffer as if it were input to P. Stack of P 27

Formal Construction of P States are pairs [q, b], where: 1. q is a state of P. 2. b is a suffix of h(a) for some symbol a. Thus, only a finite number of possible values for b. Stack symbols of P are those of P. Start state of P is [q 0,ε]. 28

Construction of P (2) Input symbols of P are the symbols to which h applies. Final states of P are the states [q, ε] such that q is a final state of P. 29

Transitions of P 1. δ ([q, ε], a, X) = {([q, h(a)], X)} for any input symbol a of P and any stack symbol X. When the buffer is empty, P can reload it. 2. δ ([q, bw], ε, X) contains ([p, w], ) if δ(q, b, X) contains (p, ), where b is either an input symbol of P or ε. Simulate P from the buffer. 30

Proving Correctness of P We need to show that L(P ) = h -1 (L(P)). Key argument: P makes the transition ([q 0, ε], w, Z 0 ) *([q, x], ε, ) if and only if P makes transition (q 0, y, Z 0 ) *(q, ε, ), h(w) = yx, and x is a suffix of the last symbol of w. Proof in both directions is an induction on the number of moves made. 31