Intro to Theory of Computation

Slides:



Advertisements
Similar presentations
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Advertisements

Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
CS 3240: Languages and Computation Properties of Context-Free Languages.
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture7: The Pumping Lemma for Context Free Languages Prof. Amos Israeli.
Introduction to Computability Theory
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
CS5371 Theory of Computation Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.3: Non-Context-Free Languages) David Martin With.
Transparency No. P2C5-1 Formal Language and Automata Theory Part II Chapter 5 The Pumping Lemma and Closure properties for Context-free Languages.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
CSCI 2670 Introduction to Theory of Computing September 22, 2005.
Context-Free and Noncontext-Free Languages Chapter 13 1.
Cs3102: Theory of Computation Class 8: Non-Context-Free Languages Spring 2010 University of Virginia David Evans.
Test # 2 Friday, November 2 nd Covers all of Chapter 2 Test # 2 will be 20% of final score (same as Test #1) Final Exam will be 40% of the final score.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
Non-CF Languages The language L = { a n b n c n | n  0 } does not appear to be context-free. Informal: A PDA can compare #a’s with #b’s. But by the time.
Non-Context-Free Languages Section 2.3 CSC 4170 Theory of Computation.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
January 20, 2016CS21 Lecture 71 CS21 Decidability and Tractability Lecture 7 January 20, 2016.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CS 154 Formal Languages and Computability March 17 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:
Bottom-up parsing Pumping Theorem for CFLs MA/CSSE 474 Theory of Computation.
CS 461 – Oct. 5 Pumping lemma #2 –Understanding –Use to show a language is not a CFL Next: Applications of CFLs –Expression grammars and Compiling.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
Context-Free and Noncontext-Free Languages Chapter 13.
Context-Free Languages
CSE 105 theory of computation
Lecture 17 Oct 25, 2011 Section 2.1 (push-down automata)
Non-Context-Free Languages Sections: 2.3 page 123 October 17, 2008
Intro to Theory of Computation
Intro to Theory of Computation
Intro to Theory of Computation
Non-Context-Free Languages
Context-free Languages
Intro to Theory of Computation
Definition: Let G = (V, T, P, S) be a CFL
Context-Free Grammars
CSE 105 theory of computation
فصل دوم Context-Free Languages
COSC 3340: Introduction to Theory of Computation
Pumping Lemma for Context-free Languages
Bottom-up parsing Pumping Theorem for CFLs
CS21 Decidability and Tractability
Intro to Theory of Computation
COSC 3340: Introduction to Theory of Computation
CS21 Decidability and Tractability
Chapter 2 Context-Free Language - 02
Computer Language Theory
CS21 Decidability and Tractability
Limitations of pushdown automata
CSE 105 theory of computation
Applications of Regular Closure
Pumping Theorem for CFLs
Intro to Theory of Computation
Intro to Theory of Computation
CSE 105 theory of computation
Presentation transcript:

Intro to Theory of Computation LECTURE 7 Last time: Pushdown automata (PDAs) Context-free grammars (CFG) Today: Pumping lemma for CFLs Proving that a language is not CF CS 332 Sofya Raskhodnikova Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper GIVE A CFG FOR… L3 = { strings of balanced parens } L4 = { aibjck | i, j, k ≥ 0 and (i = j or j = k) } Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper CFGs in the real world The syntactic grammar for the Java programming language BasicForStatement: for ( ; ; ) Statement for ( ; ; ForUpdate ) Statement for ( ; Expression ; ) Statement for ( ; Expression ; ForUpdate ) Statement for ( ForInit ; ; ) Statement for ( ForInit ; ; ForUpdate ) Statement for ( ForInit ; Expression ; ) Statement for ( ForInit ; Expression ; ForUpdate ) Statement Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper COMPILER MODULES LEXER PARSER SEMANTIC ANALYZER TRANSLATOR/INTERPRETER Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper Parse trees A A A B # 1 1 A  0A1  00A11  00B11  00#11 Sofya Raskhodnikova; based on slides by Nick Hopper

Equivalence of CFGs & PDAs A language is generated by a CFG  It is recognized by a PDA Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper Context-free or not? NOT L₁ = { xy | x,y ∈ {0,1}* and x=y} YES L₂ = {xy | x,y ∈ {0,1}*, |x|=|y| and x ≠ y} Sofya Raskhodnikova; based on slides by Nick Hopper

there exist uvxyz=w, where: Pumping lemma for CFLs Let L be a context-free language Then there exists P such that for every w  L with |w| ≥ P there exist uvxyz=w, where: 1. |vy| > 0 2. |vxy| ≤ P 3. uvixyiz  L for all i ≥ 0 Sofya Raskhodnikova; based on slides by Nick Hopper

there exist uvxyz=w, where: Examples there exist uvxyz=w, where: Example: L = { w ∈ {0,1}* | w = wR}. w = 0; u,v,x,y,z = ? w = 010; u,v,x,y,z=? |vy| > 0 |vxy| ≤ P uvixyiz  L for all i ≥ 0 Example: L = { w ∈ {a,b}* | #a > #b in w}. w = a; u,v,x,y,z = ? w = aab; u,v,x,y,z=?

Exercise Example: L = { w ∈ {a,b}* | #a > #b in w}. w = aab; u,v,x,y,z=? 𝑣=𝑎, 𝑥=𝑎,𝑦=𝑏 𝑣=𝑎𝑎,𝑥=𝜀,𝑦=𝑏 𝑣=𝑎𝑎,𝑥=𝜀,𝑦=𝜀 More than one choice above works. None of the choices above work. {madam, dad, but, tub, book}

Sofya Raskhodnikova; based on slides by Nick Hopper Recall: parse trees A A A B # 1 1 A  0A1  00A11  00B11  00#11 Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper Pumping lemma: proof idea If string w is long enough, then every parse tree for w must have a path that contains a variable more than once. T R u v x z y T u z R v y x Sofya Raskhodnikova; based on slides by Nick Hopper

Pumping lemma: proof Let b be the maximum number of symbols on the right-hand side of a rule. If the height of a parse tree is h, the length of the string generated is at most: bh Let |V| be the number of variables in G. Define p = b|V|+1 . Let w be a string of length at least p. Length of string generated is the number of leaves in the tree. Let T be the parse tree for w with the smallest number of nodes. T must have height at least |V|+1. Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper Pumping lemma: proof The longest path in T must have ≥ |V|+1 variables Select R to be the variable that repeats among the lowest |V|+1 variables 1. |vy| > 0 T R u v x z y T u z R v y x 2. |vxy| ≤ P Sofya Raskhodnikova; based on slides by Nick Hopper

Using the pumping lemma Prove L = {ww | w ∈ {0,1}*} is not context-free Assume for contradiction that L is context-free. Then there is a pumping length P. Sofya Raskhodnikova; based on slides by Nick Hopper

Pumping lemma as a game YOU pick the language 𝑳 to be proved not CFL. ADVERSARY picks 𝒑, but doesn't reveal to YOU what 𝒑 is; YOU must devise a play for all possible 𝒑's. YOU pick 𝒘∈ L, which should depend on 𝒑 and which must be of length at least 𝒑. ADVERSARY divides 𝒘 into 𝒖,𝒗,𝒙, 𝒚, 𝒛, obeying PL conditions: 𝒗𝒚 is not empty and 𝒗𝒙𝒚 has length ≤𝒑. Again, ADVERSARY does not tell YOU what 𝒖,𝒗,𝒙, 𝒚, 𝒛 are. YOU win by picking 𝒊, which may be a function of 𝒑, 𝒖,𝒗,𝒙, 𝒚, 𝐳, such that 𝒖𝒗 𝒊 𝒙 𝒚 𝒊 𝒛 is not in L.

Exercise What string can you choose in your next move? Prove {ww | w ∈ {0,1}*} is not context free. What string can you choose in your next move? 0101 0 𝑝 1 𝑝 01 𝑝 01 2𝑝 0 𝑝 1 𝑝 0 𝑝 1 𝑝 More than one choice above works. Proof: Assume … pumping length p. {madam, dad, but, tub, book}

Using the pumping lemma Prove L = {ww | w ∈ {0,1}*} is not context-free Assume for contradiction L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0P1P0P1P has |s| ≥ P and s ∈ L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀ i, uvixyiz ∈ L. P P P P s = 00…0011…1100…0011…11 Sofya Raskhodnikova; based on slides by Nick Hopper

{ww | w∈{0,1}*} is not a CFL: proof No matter what P is, the string s = 0P1P0P1P has |s| ≤ P and s ∈ L. vxy cannot be only in the first half, since pumping down would move a 0 to the end of the first half. Then pumping down must remove at least one 1 or one 0, e.g. uxz = 0P1i0j1P where i  p or j  p. So uxz  L, no matter how they are chosen; so L cannot be context-free! vxy cannot be only in the last half, since pumping up would move a 0 to the end of the first half. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀ i, uvixyiz ∈ L. P P P P s = 00…0011…1100…0011…11 vxy Sofya Raskhodnikova; based on slides by Nick Hopper

Exercise To prove that a language L is not context free, we can argue that a PDA cannot remember enough information to recognize L; use pumping lemma; give a CFG and show that it does not generate L. None of the above. More than one choice above works.

Using the pumping lemma Prove L = {w#wR | w ∈ {0,1}* and #1s = #0s} is not context-free Assume L is context-free. Then there is a pumping length P. No matter what P is, the string s = 0P1P#1P0P has |s| ≥ P and s ∈ L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀i, uvixyiz ∈ L. P P P P s = 00…0011…11#11…1100…00 Sofya Raskhodnikova; based on slides by Nick Hopper

{w#wR | w ∈{0,1}* and #1s = #0s} is not a CFL: proof Assume L is context-free. Then there is a pumping length P. vxy cannot be only in either half, since pumping would make one side of # longer than the other. No matter what P is, the string s = 0P1P#1P0P has |s| ≤ P and s ∈ L. # cannot be in v or y since pumping would add too many #s. So it must be in x. Since # is in x and |vxy| ≤ P, neither v nor y can have 0s, and at least one must have a 1. Then uv2xy2z has more 1s than 0s and is not in L. So there should be uvxyz=s with: 1) |vy| > 0, 2) |vxy| ≤ P, 3) ∀ i, uvixyiz ∈ L. P P P P s = 00…0011…11#11…1100…00 vxy Sofya Raskhodnikova; based on slides by Nick Hopper

Sofya Raskhodnikova; based on slides by Nick Hopper Context-free or not? NOT L₁ = { xy | x,y ∈ {0,1}* and x=y} YES L₂ = {xy | x,y ∈ {0,1}*, |x|=|y| and x ≠ y} Sofya Raskhodnikova; based on slides by Nick Hopper

Give an algorithmic description of a PDA for {𝒙𝒚∣𝒙,𝒚∈ 𝟎,𝟏 ∗ and 𝒙 =|𝒚| but 𝒙≠𝒚}. ≠ Hint: 𝒙 𝒚 𝒊 𝒋 𝒊 𝒋

Give an algorithmic description of a PDA for {𝒙𝒚∣𝒙,𝒚∈ 𝟎,𝟏 ∗ and 𝒙 =|𝒚| but 𝒙≠𝒚}. ≠ Hint: 𝒊 𝒊 𝒋 𝒋