Context-Free Languages

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

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

Computational Models - Lecture 5 1

The Pumping Lemma for Context Free Grammars

Computability Theory

Context-Free Languages (Pre Lecture)

MA/CSSE 474 Theory of Computation

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

Chapter 16: Non-Context-Free Languages

Context-Free and Noncontext-Free Languages

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

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

Computational Models - Lecture 4

Intro to Theory of Computation

Computational Models - Lecture 4 1

Introduction to Theory of Computing

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

Properties of Context-Free Languages

Fall 1999 Formal Language Theory Dr. R. Boyer. Theorem. For any context free grammar G; if there is a derivation of w 2 from the

CSE 355 Test 2, Fall 2016

Context Free Languages: Decidability of a CFL

Pumping Lemma for CFLs

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

SCHEME FOR INTERNAL ASSESSMENT TEST 3

CISC4090: Theory of Computation

Context-Free Languages

The View Over The Horizon

Chapter 2 The Pumping Lemma

CS5371 Theory of Computation. Lecture 5: Automata Theory III (Non-regular Language, Pumping Lemma, Regular Expression)

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

CS500 Homework #2 Solutions

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

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

CS375: Logic and Theory of Computing

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

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

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

Computability Theory

Lecture 17: Language Recognition

CISC 4090 Theory of Computation

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CpSc 421 Final Exam December 15, 2006

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

Chapter 6. Properties of Regular Languages

Einführung in die Computerlinguistik

Chap. 7 Properties of Context-free Languages

This lecture covers Chapter 7 of HMU: Properties of CFLs

PUSHDOWN AUTOMATA (PDA)

3.10. TREE DOMAINS AND GORN TREES Tree Domains and Gorn Trees

Parikh s theorem. Håkan Lindqvist

Properties of Context-free Languages. Reading: Chapter 7

Lecture III CONTEXT FREE LANGUAGES

Theory of Computation (IV) Yijia Chen Fudan University

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

Part 3 out of 5. Automata & languages. A primer on the Theory of Computation. Last week, we learned about closure and equivalence of regular languages

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

Functions on languages:

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

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

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

Even More on Dynamic Programming

Notes for Comp 497 (454) Week 10

Section 1 (closed-book) Total points 30

CS481F01 Solutions 6 PDAS

Final exam study sheet for CS3719 Turing machines and decidability.

CS375 Midterm Exam Solution Set (Fall 2017)

CS20a: summary (Oct 24, 2002)

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

Solution Scoring: SD Reg exp.: a(a

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

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

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

What we have done so far

Context Free Language Properties

Chomsky and Greibach Normal Forms

Computational Models - Lecture 3

Finite Automata and Regular languages

302 CHAPTER 5. CONTEXT-FREE LANGUAGES AND PDA S. 5.6 Tree Domains and Gorn Trees

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

This lecture covers Chapter 5 of HMU: Context-free Grammars

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

Sample Midterm. Understanding the questions is part of the exam; you are not allowed to ask questions during the exam.

Computational Models - Lecture 3 1

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

The Chomsky Hierarchy(review)

CSci 311, Models of Computation Chapter 4 Properties of Regular Languages

Einführung in die Computerlinguistik

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

Chapter 5: Context-Free Languages

Pushdown Automata: Introduction (2)

5 Context-Free Languages

Tree Adjoining Grammars

Notes on Pumping Lemma

Pushdown Automata (Pre Lecture)

NPDA, CFG equivalence

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Theory of Computation

Transcription:

CS:4330 Theory of Computation Spring 2018 Context-Free Languages Non-Context-Free Languages Haniel Barbosa

Readings for this lecture Chapter 2 of [Sipser 1996], 3rd edition. Section 2.3.

Proving context-freeness We will use a similar mechanism as with regular languages Pumping lemma for context-free languages states that every CFL has a specif value, called pumping length, such that all longer strings in the language can be pumped However, the meaning of pumping is a bit more complex than in the case of regular languages Here pumping means that a string can be divided into five parts so that the second and fourth parts may be repeated any number of times and the resulting string is in the language 1 / 10

Pumping lemma for CFLs Theorem If A is a context-free language, then there is a number p (the pumping length) such that if s A and s p, then s may be divided into five pieces, s = uvxyz, satisfying the conditions: 1. For each i 0, uv i xy i z A 2. vy 0 3. vxy p Interpretation Condition 1 states that the length of strings of A can be unlimited but have a fixed structure uv i xy i z, i 0 Condition 2 says that in the structure uv i xy i z either v or y is not empty, otherwise the theorem would be trivially true Condition 3 states that pieces v,x,y together have length at most p 2 / 10

Proof idea Let A be a CFL and G be a CFG generating A. We must show that any sufficiently long s A can be pumped and remain in A. The idea behind this proof is: 1. Because s A, it is derivable from G and has a derivation tree D s 2. Since s is very long, the derivation tree D s of s must be very tall 3. This means that D s contains some long path from the start nonterminal of G at the root to one of the terminal symbols at a leaf. 4. On this long path, some nonterminal symbol X must be repeated due to the pigeonhole principle Note The repetition of X in D s allows us to replace subtrees under X and still get a legal derivation tree. Hence, we may cut s into five pieces and we may repeat the second and fourth pieces and obtain a string uv i xy i z A, for any i 0. 3 / 10

Proof Let G = (V, Σ, R, S) be a CFG that generates A. Let b 2 be the maximum number of symbols in the rhs of rules in R. In any derivation tree D using G we know that a node cannot have more than b children, i.e. at most b leaves in 1 step from S; at most b 2 leaves in 2 steps; at most b h in h steps If the height of D is at most h the length of the string generated is at most b h Conversely, if a string is at least b h+1 long, each of its derivation trees must be at least h + 1 high With V being the number of nonterminals in G, then we set p = b V +1 Consequently, a derivation tree of any string from A of length at least p requires a height at least V + 1. 4 / 10

Proof, continuation Suppose s A, s p. We will show how to pump s Let D s be the derivation tree of s; if s has several derivation trees we choose D s to be the tree with the smallest number of nodes. Since s p we know that D s has height at least V + 1, so the longest path in D s has length at least V + 1 The longest path in D s has at least V + 1 nodes labeled by nonterminals, since only the leaf is a terminal Since there are only V variables in V, some variable X appears more than once on the longest path in D s ; we may assume that X is the closest nonterminal to the leaf that is repeated on this path 5 / 10

Proof, continuation Now we divide s into uvxyz Each occurrence of X has a subtree under it, generating a portion of s The upper occurrence of X has the larger subtree and generates vxy, whereas the lower occurrence generates just x Since both of the trees mentioned above are generated by X, we may substitute one for the other and still obtain a valid derivation tree Replacing the smaller tree by the larger repeatedly, gives derivation trees for the strings uv i xy i z for each i > 1. Replacing the larger tree by the smaller one generates the string uxz. This establishes condition 1 of the lemma. 6 / 10

Proof, continuation To prove condition 2 we must ensure that not both v and y are ɛ: If both v and y were ɛ, the derivation tree obtained by substituting the smaller tree for the larger tree would have fewer nodes than D s, and would still generate s This however is a contradiction with out assumption that D s is the smallest derivation tree generating s 7 / 10

Proof, continuation To prove condition 3, we must ensure that vxy p: In the derivation tree D s the upper occurrence of X generates vxy We choose X so that both occurrences fall within the bottom V + 1 nonterminals on the path and we chose the longest path in D s Hence, the subtree where X generates vxy is at most V + 1 high. A tree of this height can generate a string of length at most b V +1 = p 8 / 10

Example We will use the pumping lemma to show that the language B = {a n b n c n n 0} is not context-free by assuming it is and deriving a contradiction. Proof Let p be the pumping length for B. Consider the string s = a p b p c p, for which it holds that s B and s p. By condition 2 of pumping lemma, in any division s = uvxyz either v or y is not empty 9 / 10

Example We will use the pumping lemma to show that the language B = {a n b n c n n 0} is not context-free by assuming it is and deriving a contradiction. Proof Let p be the pumping length for B. Consider the string s = a p b p c p, for which it holds that s B and s p. By condition 2 of pumping lemma, in any division s = uvxyz either v or y is not empty When both v and y contain only one type of symbol (a,b,c), v does not contain both a s and b s or both b s and c s; the same holds for y. In this case uv 2 xy 2 z cannot contain an equal number of a,b,c. 9 / 10

Example We will use the pumping lemma to show that the language B = {a n b n c n n 0} is not context-free by assuming it is and deriving a contradiction. Proof Let p be the pumping length for B. Consider the string s = a p b p c p, for which it holds that s B and s p. By condition 2 of pumping lemma, in any division s = uvxyz either v or y is not empty When both v and y contain only one type of symbol (a,b,c), v does not contain both a s and b s or both b s and c s; the same holds for y. In this case uv 2 xy 2 z cannot contain an equal number of a,b,c. When either v or y contain more than one type of symbols a,b,c, uv 2 xy 2 z may contain equal number of a,b,c, but they do not occur in the right order. 9 / 10

Example We will use the pumping lemma to show that the language B = {a n b n c n n 0} is not context-free by assuming it is and deriving a contradiction. Proof Let p be the pumping length for B. Consider the string s = a p b p c p, for which it holds that s B and s p. By condition 2 of pumping lemma, in any division s = uvxyz either v or y is not empty When both v and y contain only one type of symbol (a,b,c), v does not contain both a s and b s or both b s and c s; the same holds for y. In this case uv 2 xy 2 z cannot contain an equal number of a,b,c. When either v or y contain more than one type of symbols a,b,c, uv 2 xy 2 z may contain equal number of a,b,c, but they do not occur in the right order. Since one of these cases must occur, a contradiction is unavoidable, and therefore B is not a CFL. 9 / 10

Another example We will show that D = {ww w {0,1} } is not a CFL by assuming it is and deriving a contradiction. Proof Let p be the pumping length for D. Choosing a counter-example s is less obvious. 10 / 10

Another example We will show that D = {ww w {0,1} } is not a CFL by assuming it is and deriving a contradiction. Proof Let p be the pumping length for D. Choosing a counter-example s is less obvious. We will consider s = 0 p 1 p 0 p 1 p. Can we pump it? In dividing s = uvxyz, vxy must be at most p, and v or y is not empty. 10 / 10

Another example We will show that D = {ww w {0,1} } is not a CFL by assuming it is and deriving a contradiction. Proof Let p be the pumping length for D. Choosing a counter-example s is less obvious. We will consider s = 0 p 1 p 0 p 1 p. Can we pump it? In dividing s = uvxyz, vxy must be at most p, and v or y is not empty. If vxy would occur only in the first half, pumping uv 2 xy 2 z moves a 1 into the first position of the second half of s so it cannot be of the form ww 10 / 10

Another example We will show that D = {ww w {0,1} } is not a CFL by assuming it is and deriving a contradiction. Proof Let p be the pumping length for D. Choosing a counter-example s is less obvious. We will consider s = 0 p 1 p 0 p 1 p. Can we pump it? In dividing s = uvxyz, vxy must be at most p, and v or y is not empty. If vxy would occur only in the first half, pumping uv 2 xy 2 z moves a 1 into the first position of the second half of s so it cannot be of the form ww Similarly if vxy is in the second part of s, pumping uv 2 xy 2 z moves a 0 into the last position of the first half of s so it cannot be of the form ww 10 / 10

Another example We will show that D = {ww w {0,1} } is not a CFL by assuming it is and deriving a contradiction. Proof Let p be the pumping length for D. Choosing a counter-example s is less obvious. We will consider s = 0 p 1 p 0 p 1 p. Can we pump it? In dividing s = uvxyz, vxy must be at most p, and v or y is not empty. If vxy would occur only in the first half, pumping uv 2 xy 2 z moves a 1 into the first position of the second half of s so it cannot be of the form ww Similarly if vxy is in the second part of s, pumping uv 2 xy 2 z moves a 0 into the last position of the first half of s so it cannot be of the form ww If vxy contains the midpoint of s, however, when we pump s down to uxz, it has the form 0 p 1 k 0 l 1 p, where k and l cannot both be p. Therefore this string is not of the form ww either. 10 / 10

Another example We will show that D = {ww w {0,1} } is not a CFL by assuming it is and deriving a contradiction. Proof Let p be the pumping length for D. Choosing a counter-example s is less obvious. We will consider s = 0 p 1 p 0 p 1 p. Can we pump it? In dividing s = uvxyz, vxy must be at most p, and v or y is not empty. If vxy would occur only in the first half, pumping uv 2 xy 2 z moves a 1 into the first position of the second half of s so it cannot be of the form ww Similarly if vxy is in the second part of s, pumping uv 2 xy 2 z moves a 0 into the last position of the first half of s so it cannot be of the form ww If vxy contains the midpoint of s, however, when we pump s down to uxz, it has the form 0 p 1 k 0 l 1 p, where k and l cannot both be p. Therefore this string is not of the form ww either. Therefore, s cannot be pumped and D is not a CFL. 10 / 10