Context-Free Languages (Pre Lecture)

Similar documents
Pushdown Automata (Pre Lecture)

Context-Free and Noncontext-Free Languages

NPDA, CFG equivalence

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

Ogden s Lemma for CFLs

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

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

Properties of Context-Free Languages. Closure Properties Decision Properties

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Context-Free Languages

Pumping Lemma for CFLs

CSE 355 Test 2, Fall 2016

Properties of Context-Free Languages

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

SCHEME FOR INTERNAL ASSESSMENT TEST 3

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Computability Theory

Computational Models - Lecture 5 1

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

Introduction to Theory of Computing

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

Foundations of Informatics: a Bridging Course

This lecture covers Chapter 7 of HMU: Properties of CFLs

MA/CSSE 474 Theory of Computation

Theory of Computation

Regular Expressions (Pre Lecture)

Computational Models - Lecture 4

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

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

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

The Pumping Lemma for Context Free Grammars

Final exam study sheet for CS3719 Turing machines and decidability.

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

Lecture 17: Language Recognition

Context Free Languages: Decidability of a CFL

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

DM17. Beregnelighed. Jacob Aae Mikkelsen

CS500 Homework #2 Solutions

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

Computational Models - Lecture 4 1

MA/CSSE 474 Theory of Computation

CS20a: summary (Oct 24, 2002)

Intro to Theory of Computation

Theory of Computation (IV) Yijia Chen Fudan University

CISC4090: Theory of Computation

V Honors Theory of Computation

Properties of context-free Languages

Formal Languages, Automata and Models of Computation

Properties of Context-free Languages. Reading: Chapter 7

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

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

The View Over The Horizon

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

Chap. 7 Properties of Context-free Languages

Functions on languages:

Homework 5 - Solution

Section 1 (closed-book) Total points 30

Context Free Language Properties

Notes for Comp 497 (454) Week 10

PUSHDOWN AUTOMATA (PDA)

Einführung in die Computerlinguistik

CSE 105 THEORY OF COMPUTATION. Spring 2018 review class

CISC 4090 Theory of Computation

More Properties of Regular Languages

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

TDDD65 Introduction to the Theory of Computation

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

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

CPS 220 Theory of Computation

Chapter 16: Non-Context-Free Languages

10. The GNFA method is used to show that

CpSc 421 Final Exam December 15, 2006

Let P(n) be a statement about a non-negative integer n. Then the principle of mathematical induction is that P(n) follows from

CSE 105 THEORY OF COMPUTATION

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

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

Theory Bridge Exam Example Questions

CSE 105 THEORY OF COMPUTATION

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

Decidability (What, stuff is unsolvable?)

CSE 105 THEORY OF COMPUTATION

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

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

Computational Models - Lecture 3 1

Computational Models - Lecture 3

Question Bank UNIT I

CSE 105 THEORY OF COMPUTATION

CS375: Logic and Theory of Computing

Chapter 6. Properties of Regular Languages

Finite Automata and Regular languages

Solution Scoring: SD Reg exp.: a(a

The Pumping Lemma and Closure Properties

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

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

Theory of Computation

Languages, regular languages, finite automata

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

Transcription:

Context-Free Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 1 / 34

Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 2 / 34

Pumping Lemma for CFL s Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 3 / 34

Pumping Lemma for CFL s Regular Pumping Lemma Idea σ [j]... σ [k 1] σ [0]... σ [j 1] σ [k]... σ [n 1] q [0] q [j] = q [k] q [n] Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 4 / 34

Pumping Lemma for CFL s Context-Free Pumping Lemma Idea PDA Interpretation σ [i]... σ [j 1] ɛ β [0]... β [t] σ [k]... σ [l 1] β [t]... β [0] ɛ σ [0]... σ [i 1] ɛ α [0]... α [s] q [0] q [i] = q [j] q [k] = q [l] q [n] u v σ [j]... σ [k 1] ɛ ɛ x y σ [l]... σ [n 1] α [s]... α [0] ɛ z Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 5 / 34

Pumping Lemma for CFL s Context-Free Pumping Lemma Idea Grammar Interpretation S S... S... R... R... R... R u x z R... u v x y z R u v v x y y z Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 6 / 34

Pumping Lemma for CFL s Theorem: Pumping Lemma for CFLs Theorem: Pumping Lemma for CFLs If L is a context-free language, there is a number p (the pumping length) where if: σ L σ p then we can divide σ into five pieces σ = uvxyz such that: for each i 0, uv i xy i z L vy > 0 vxy p Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 7 / 34

Pumping Lemma for CFL s Pumping Lemma for CFLs Restated For every context-free language L, There exists an integer p, such that For every string σ L where σ p There exists an u, v, x, y, and z where σ = uvxyz for alli 0, uv i xy i z L vy > 0 vxy p L C, p Z, σ L, ( σ p) = ( u, v, x, y, z) ((σ = uvxyz) ( vxy p) ( vy > 0) ( i, uv i xy i z L )) Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 8 / 34

Pumping Lemma for CFL s Example 0: Pumping Lemma A = {a n b n c n n 0} is not context-free Proof Assume A is context-free and apply the pumping lemma Choose σ = a p b p c p = uvxyz Any way to divide σ into uvxyz will violate the pumping lemma Case 1: Both v and y contain only one type of symbol. Then uv 2 xy 2 z will not contain equal numbers of a s, b s, and c s. Case 2: Either v or y contain more than one type of symbol. Then uv 2 xy 2 z will not have symbols in the correct order. Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 9 / 34

Pumping Lemma for CFL s Example 1: Pumping Lemma B = { a k b l c k d l k 1 l 1 } is not context-free Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 10 / 34

Pumping Lemma for CFL s Example 2: Pumping Lemma C = {ωω ω {0, 1} } is not context-free Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 11 / 34

Decision Properties Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 12 / 34

Decision Properties Summary of CFL Decision Properties Can Answer: Membership: ω? C Emptiness: C? = Infinite: C? = Regular Subset: C? R Cannot Answer: Equivalence: C 1? = C2 Disjoint: C 1 C 2? = Subset: C 1? C2 Need more powerful computation model (Turing Machine) to prove no algorithm exists Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 13 / 34

Decision Properties Membership Given: CFL A, string w Find: Is w in A? w? L (A) Solution (simple): Simulate PDA for A on w Solution (fast): Many specialized parsing algorithms Earley: An CFL, O(n 3 ) in length of string CYK: An CFL, O(n 3 ) in length of string LALR: Some CFL, O(n) in length of string LL(1): Some CFLs, O(n) in length of string LL(*): Some CFL, O(n 2 ) in length of string Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 14 / 34

Decision Properties Useless symbols Useful Nonterminal X is useful if: There exists a derivation S α X β ω where S is the start symbol and ω T Requirements Two requirements for usefulness: 1. A terminal string is derivable from X 2. X is in string derivable from S Useless No such derivation using X Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 15 / 34

Decision Properties Useless symbols Terminal-deriving symbols Algorithm 1: Find terminal-deriving symbols 1 V old ; 2 V new A A σ for some σ T }{{} ; 3 while V old V new do 4 V old V new ; 5 V new V old A 6 return V new ; A derives string of terminals A α for α (T V old) }{{} A derives string of terminals and terminal-deriving nonterminals ; Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 16 / 34

Decision Properties Useless symbols Reachable symbols Algorithm 2: Find terminal-deriving symbols 1 V old ; 2 V new {S}; 3 while V old V new do 4 V old V new ; 5 V new V old B A V old, A αbβ }{{} ; 6 return V new ; A derives B Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 17 / 34

Decision Properties Useless symbols Useful Set V useful = V terminal-deriving V reachable P useful are productions containing only V useful Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 18 / 34

Decision Properties Useless symbols Example Original Grammar Symbols Simplified Grammar S A B [a] A [a] Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 19 / 34

Decision Properties Emptiness Given: Grammar G Find: Is the language of G empty? L (G) =? Solution: Is start symbol S a useless symbol? Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 20 / 34

Decision Properties Infiniteness Given: Context-Free Language L Find: Is L infinite? L? = Solution: Based on pumping lemma For pumping length p Is there a string in the language between p and 2p 1? Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 21 / 34

Closure Properties Outline Pumping Lemma for CFL s Decision Properties Closure Properties Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 22 / 34

Closure Properties CFL Closure under Concatenation Given: CFGs A = (V A, T A, P A, S A ) B = (V B, T B, P B, S B ) Find: C = (V C, T C, P C, S C ) where L (C) = L (A)L (B) Solution (simple): Union on start symbols: V C = V A V B {S } T C = T A T B S C = S P C = P A P B {S S A S B } Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 23 / 34

Closure Properties Example: Concatenation on CFGs A B AB A 0 A 1 ɛ B 1 B 0 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 24 / 34

Closure Properties CFL Closure under Union Given: CFGs A = (V A, T A, P A, S A ) B = (V B, T B, P B, S B ) Find: C = (V C, T C, P C, S C ) where L (C) = L (A) L (B) Solution (simple): Union on start symbols: V C = V A V B {S } T C = T A T B S C = S P C = P A P B {S S A, S S B } Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 25 / 34

Closure Properties Example: Union on CFGs A B A B A 0 A 1 ɛ B 1 B 0 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 26 / 34

Closure Properties CFL Closure under Repetition Given: CFGs A = (V A, T A, P A, S A ) Find: C = (V C, T C, P C, S C ) where L (C) = L (A) Solution (simple): Union on start symbols: V C = V A {S } T C = T A S C = S P C = P A {S S A S, S ɛ} Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 27 / 34

Closure Properties Example: Repetition on CFGs A A A 0 A 1 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 28 / 34

Closure Properties CFL Non-closure under Intersection Theorem The context-free set is not closed under intersection ( ). Proof Proof by counterexample L 1 = {a n b n c n n 1} is not context-free L 2 = { a n b n c i n 1, i 1 } is context-free S A B A a A b ab B c B c L 3 = { a i b n c n n 1, i 1 } is context-free S A B A a A a B b B c bc L 1 = L 2 L 3 Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 29 / 34

Closure Properties CFL Non-closure under Difference Theorem The context-free set is not closed under difference (\). Proof Proof by contradiction Generally: L M = L \ (L \ M) If CFLs were closed under difference, they would also be closed under intersection. But CFLs are not closed under intersection. Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 30 / 34

Closure Properties CFL-Regular Intersection Given: A context-free language and a regular language as: PDA P = (Q P, Σ, Γ P, δ P, q 0,P, F P ) DFA D = (Q D, Σ, δ D, q 0,D, F D ), where Find: C such that L (C) = L (P) L (D) Solution: Simulate P and D in parallel. Result is a PDA. Q C = Q P Q D and q 0,C = (q 0,P, q 0,D ) and F C = F P F D Γ C = Γ P predecessor input {}}{{}}{ (q P, q D ), a, δ C ( q P, ) q D, γ }{{} successor }{{} pushed popped {}}{ γ (( q P, γ ) δ P (q P, a, γ) ) ( q D }{{} = δ D(q D ) ) }{{} PDA Transition DFA Transition Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 31 / 34

Closure Properties CFL-Regular Intersection Diagram PDA q p a, γ γ q p Intersect. DFA a, γ γ (q p, q d ) (q p, q d ) q d a q d Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 32 / 34

Closure Properties CFL-Regular Intersection Example {ω {0, 1} ω has an equal number of 0 s and 1 s} 0 1 start 0, ɛ 0 0, 1 ɛ ɛ, ɛ $ ɛ, $ ɛ p 0 p 1 p 2 start 0 1 d 0 1 d 1 1, ɛ 1 1, 0 ɛ Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 33 / 34

Closure Properties CFL-Regular Intersection Example continued Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 34 / 34