Computational Models - Lecture 5 1

Similar documents
Computational Models - Lecture 4 1

Computational Models - Lecture 4

The View Over The Horizon

Computational Models - Lecture 4 1

Context-Free Languages

Introduction to Theory of Computing

The Pumping Lemma for Context Free Grammars

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

Computational Models - Lecture 3 1

Computational Models - Lecture 3

Computational Models: Class 3

Computational Models: Class 5

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

Computational Models - Lecture 5 1

CSE 355 Test 2, Fall 2016

Context-Free Languages (Pre Lecture)

Computational Models Lecture 8 1

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

Chapter 16: Non-Context-Free Languages

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

Properties of Context-Free Languages

CISC4090: Theory of Computation

Computational Models - Lecture 3 1

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

What we have done so far

Context-Free and Noncontext-Free Languages

Computational Models Lecture 8 1

This lecture covers Chapter 7 of HMU: Properties of CFLs

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

Computational Models Lecture 2 1

CISC 4090 Theory of Computation

Computational Models Lecture 2 1

Chomsky and Greibach Normal Forms

MA/CSSE 474 Theory of Computation

Context Free Languages: Decidability of a CFL

Computational Models Lecture 8 1

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Intro to Theory of Computation

Final exam study sheet for CS3719 Turing machines and decidability.

SCHEME FOR INTERNAL ASSESSMENT TEST 3

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

CS375: Logic and Theory of Computing

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

CISC 4090 Theory of Computation

Computational Models - Lecture 1 1

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

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

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

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

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

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

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

Computational Models Lecture 5

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

NPDA, CFG equivalence

Properties of Context-free Languages. Reading: Chapter 7

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

DM17. Beregnelighed. Jacob Aae Mikkelsen

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

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

Notes for Comp 497 (454) Week 10

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

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

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Theory of Computation (IV) Yijia Chen Fudan University

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

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

Foundations of Informatics: a Bridging Course

Properties of context-free Languages

Pumping Lemma for CFLs

CPSC 313 Introduction to Computability

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

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

PUSHDOWN AUTOMATA (PDA)

Even More on Dynamic Programming

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CPS 220 Theory of Computation

CS 373: Theory of Computation. Fall 2010

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

Properties of Context-Free Languages. Closure Properties Decision Properties

Theory of Computation - Module 3

Einführung in die Computerlinguistik

Theory of Computation

Einführung in die Computerlinguistik

Ogden s Lemma for CFLs

Chap. 7 Properties of Context-free Languages

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

Chapter 5: Context-Free Languages

Lecture 17: Language Recognition

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

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

Context-Free Grammars and Languages. Reading: Chapter 5

CS500 Homework #2 Solutions

Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.

Context Free Languages and Grammars

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

Lecture III CONTEXT FREE LANGUAGES

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

Computational Models: Class 1

Transcription:

Computational Models - Lecture 5 1 Handout Mode Iftach Haitner. Tel Aviv University. November 28, 2016 1 Based on frames by Benny Chor, Tel Aviv University, modifying frames by Maurice Herlihy, Brown University. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 1 / 49

Talk Outline Chomsky Normal Form (CNF) Checking membership in a CNF grammer Pumping Lemma for context free languages Push Down Automata (PDA) Sipser s book, 2.1, 2.2 & 2.3 Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 2 / 49

CFGs and CFLs, reminder A context-free grammar is a 4-tuple (V, Σ, R, S), where V is a finite set of variables Σ is a finite set of terminals (V Σ = ) R is a finite set of rules of the form A x, where A V and x (V Σ). S V is the start symbol. Let u, v (V Σ). If A w R, then uav yields uwv, denoted uav uwv. u v if u = v, or u u 1... u k v for some sequence u 1, u 2,..., u k Definition 1 The language of the grammar G, denoted L(G), is {w Σ : S w} where is determined by G. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 3 / 49

Part I Chomsky Normal Form (CNF) Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 4 / 49

Checking membership in a CFL Challenge Given a CFG G and a string w, decide whether w L(G)? Initial Idea: Design an algorithm that tries all derivations. Problem: If G does not generate w, we ll never stop. Possible solution: Use special grammars that are: just as expressive! better for checking membership. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 5 / 49

Chomsky Normal Form (CNF) A simplified, canonical form of context free grammars. G = (V, Σ, R, S) is in a CNF, if every rule in in R has one of the following forms: A a, A V a Σ A BC, A V B, C V \ {S} S ε. Simpler to analyze: each derivation adds (at most) a single terminal, S only appears once, ε appears only at the empty word What does parse tree look like? Most internal nodes are degree 2 (except parents of leaves, which are degree 1) Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 6 / 49

Generality of CNF Theorem 2 Any context-free language is generated by a context-free grammar in Chomsky Normal Form. Proof Idea: Add new start symbol S 0. Eliminate all ε rules of the form A ε. Eliminate all unit rules of the form A B. Convert remaining long rules to proper form. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 7 / 49

Add new start symbol Add new start symbol S 0 and rule S 0 S (Guarantees that new start symbol is never on right hand side of a rule) e.g. S A ab ε A baa S becomes S 0 S S A ab ε A baa S Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 8 / 49

Convert "long rules": terminals S ccaba bc b A a bb becomes S CCABA BC b A a BB B b C c Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 9 / 49

Convert "long rules": multiple nonterminals S AAAB becomes S AN 1 N 1 AN 2 N 2 AB Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 10 / 49

Eliminate "ε-rules" Repeat until all A ɛ (A S) rules are gone: remove A ε for any rule of form C AB or C BA, add C B. for any rule of form C AA add C A and C ε (unless C ε has already been removed). for any rule of form C A add C ε (unless C ε has already been removed.) Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 11 / 49

Eliminate "unit rules" Repeat until all unit rules removed remove some A B for each B U (where U (Σ Γ) ), add A U (unless A U was a previously removed unit rule) Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 12 / 49

CNF: Example S ASA ab A B S B b ε Is transformed into: S 0 AA 1 UB a SA AS S AA 1 UB a SA AS A b AA 1 UB a SA AS A 1 SA U a B b Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 13 / 49

CNF has bounded derivation length Lemma 3 Let G be a CFG in CNF and let w L(G) be with w = n 1. Then every derivation of w by G has a derivation of length 2n 1. Proof? consider the parsing tree for w Advantage: Easier to check whether w L(G), see next part. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 14 / 49

Part II Checking Membership for CFGs in Chomsky Normal Form Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 15 / 49

Checking membership for CFG in CNF form Let G = (V, Σ, R, S) be CFG in CNF and let A V and w Σ Algorithm 4 (Derive(A, w)) w = ε: if A ε R (i.e., A = S) return TRUE, otherwise return FALSE. w = 1: if A w R return TRUE, otherwise return FALSE. w > 1: for each A BC and each non-trivial partition w = w 1 w 2 : Call Derive(B, w1 ) and Derive(C, w 2 ). Return TRUE if both return TRUE. Return FALSE. Claim: A w Derive(A, w) = TRUE. Proof? A w = Derive(A, w) = TRUE, by induction on # of derivation steps. Derive(A, w) = TRUE = A w, by induction on w. Hence, Derive(S, w) = TRUE w L(G). Procedure Derive can also output a parse tree for w Where have we used the fact that G is in CNF? Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 16 / 49

Time complexity of Derive What is the time complexity T : N N of Derive? Each recursive call tests R rules and n partitions. T (n) R n 2T (n 1) T (n) O(( R n) n ). Still exponential... Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 17 / 49

Efficient Algorithm Keep in memory the results of Derive(A, w). Number of different inputs: V n 2. Only V n 2 calls, each takes O( R n). T (n) O( R n3 V ). Polynomial time! This approach is called Dynamic Programming Basic idea: If number of different inputs is limited, say I(n). Each run (excluding recursive calls) takes at most R(n) time Total running time is bounded by T (n) R(n)I(n). Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 18 / 49

Part III Non-Context-Free Languages Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 19 / 49

Proving a Language is not a CFL The pumping lemma for finite automata and Myhill-Nerode theorem are our tools for showing that languages are not regular. We will now show a similar pumping lemma for context-free languages. It is slightly more complicated... Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 20 / 49

Pumping Lemma for CFL (also known as, the uvxyz Theorem) Theorem 5 For any CFL L there exists l N ( critical length"), such that for any w L with w l, there exit u, v, x, y, z Σ such that w = uvxyz and For every i 0: uv i xy i z L vy > 0, ( non-triviality") vxy l Basic Intuition: Let L be a CFL and a let w be a very long string in L. Then w must have a tall parse tree. Hence, some root-to-leaf path must repeat a symbol. Why is that so? Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 21 / 49

Basic Intuition cont. T R R u v x y z We have: T urz, R vry, and R x. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 22 / 49

Proof of Thm 5 Let G be a CFG and let L = L(G). Let b be the max number of symbols in right-hand-side of any rule (what is b for a CNF grammar?). Since no node in a parse tree of G has more than b children, at depth d such tree has at most b d leaves. Let V be the number of variables in G, and set l = b V +2. Let w be a string with w l, and let T be parse tree for w (with respect to G) with fewest nodes T has height V + 2 Some path in T has length V + 2 Such path repeats a variable R Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 23 / 49

Proof of Thm 5 cont. Set w = uvxyz T R R u v x y z Each occurrence of R produces a string Upper produces string vxy Lower produces string x Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 24 / 49

Proving uv i xy i z L for all i > 1 Replacing smaller by larger yields uv i xy i z, for i > 0. T R R u v Rx y z Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 25 / 49

Proving uv i xy i z L for i = 0 Replacing larger by smaller yields uxz. T R x u z Together, they establish: uv i xy i z L for all i 0 Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 26 / 49

Proving vy > 0 If v and y are both ε, then T R x u z is a parse tree for w with fewer nodes than T, a contradiction. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 27 / 49

Proving vxy l T R R V +2 u v x y z b V +2 Without loss of generality both occurrences of R lie in bottom V + 1 variables on the path. The upper occurrence of R (from now on R 1 ) generates vxy. Subtree rooted at R 1 is of height at most V + 2. Hence, vxy b V +2 = l. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 28 / 49

Non CFL Example (1) Claim 6 L 1 = {a n b n c n : n N} is not a CFL. Proof: By contradiction. Assume L 1 is a CFL with grammar G, let l be the critical length of G and consider w = a l b l c l. Let u, v, x, y, z be the strings with w = uvxyz guaranteed by Thm 5 for w. Both v and y are monochromatic repetitions of of a single letter. Proof: Otherwise, uv 2 xy 2 z would have out-of-order symbols. Hence, uv 2 xy 2 z is imbalanced Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 29 / 49

Non CFL Example (2) Claim 7 L 2 = {a i b j c k : 0 i j k} is not context free. Proof: By contradiction. Assume L 2 is a CFL with grammar G, let l be the critical length of G and consider w = a l b l c l. Let u, v, x, y, z be the strings with w = uvxyz guaranteed by Thm 5 for w. Both v and y are monochromatic. If neither v nor y contains a, then uv 0 xy 0 z has too many a s. If neither v nor y contains c, then uv 2 xy 2 z has too few c s. If neither v nor y contains b, then either v > 0, and then uv 2 xy 2 z has more a s than b s. or y > 0, and then uv 0 xy 0 z has more b s than c s Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 30 / 49

Non CFL Example (3) Claim 8 L 3 = {ww : w {0, 1} } is not context-free. Proof: By contradiction. Assume L 3 is a CFL with grammar G, let l be the critical length of G and consider w = 0 l 1 l 0 l 1 l. Let u, v, x, y, z be the strings with w = uvxyz guaranteed by Thm 5 for w. Assuming vxy is in the first half of w, then uv 2 xy 2 z moves" a 1 into the first position of second half. Assuming vxy is in the second half, then uv 2 xy 2 z moves" a 0 into the last position of first half. Assuming vxy straddles the midpoint, then pumping down to uxz yields 0 l 1 i 0 j 1 l where i and j cannot both be l. Note that {ww R : w {0, 1} } is a CFL. Iftach Haitner (TAU) Computational Models, Lecture 5 November 28, 2016 31 / 49