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

Similar documents
Notes for Comp 497 (454) Week 10

CPS 220 Theory of Computation

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

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

Properties of Context-Free Languages. Closure Properties Decision Properties

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Properties of Context-free Languages. Reading: Chapter 7

NPDA, CFG equivalence

Computability Theory

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Properties of Context-Free Languages

Chap. 7 Properties of Context-free Languages

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

MA/CSSE 474 Theory of Computation

Properties of Context Free Languages

CISC4090: Theory of Computation

Notes for Comp 497 (Comp 454) Week 5 2/22/05. Today we will look at some of the rest of the material in Part 1 of the book.

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

Foundations of Informatics: a Bridging Course

CS500 Homework #2 Solutions

CSE 355 Test 2, Fall 2016

Ogden s Lemma for CFLs

Context-Free Languages (Pre Lecture)

Introduction to Theory of Computing

This lecture covers Chapter 7 of HMU: Properties of CFLs

Context Free Languages and Grammars

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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

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

10. The GNFA method is used to show that

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

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

Context-Free and Noncontext-Free Languages

MA/CSSE 474 Theory of Computation

Chapter 6. Properties of Regular Languages

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

Theory of Computation (Classroom Practice Booklet Solutions)

Properties of context-free Languages

Section 1 (closed-book) Total points 30

INSTITUTE OF AERONAUTICAL ENGINEERING

HW6 Solutions. Micha l Dereziński. March 20, 2015

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

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

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

Context Free Grammars

Computational Models - Lecture 5 1

CS375: Logic and Theory of Computing

Concordia University Department of Computer Science & Software Engineering

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

CPSC 421: Tutorial #1

Theory of Computation

NODIA AND COMPANY. GATE SOLVED PAPER Computer Science Engineering Theory of Computation. Copyright By NODIA & COMPANY

Computational Models - Lecture 4 1

CS20a: summary (Oct 24, 2002)

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

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

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

SCHEME FOR INTERNAL ASSESSMENT TEST 3

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

Chapter 16: Non-Context-Free Languages

Computational Models - Lecture 4

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Question Bank UNIT I

Automata and Computability. Solutions to Exercises

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Lecture 17: Language Recognition

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

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

Solution Scoring: SD Reg exp.: a(a

Automata and Computability. Solutions to Exercises

The View Over The Horizon

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Einführung in die Computerlinguistik

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

Author: Vivek Kulkarni ( )

Theory of Computation (IV) Yijia Chen Fudan University

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

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

Sri vidya college of engineering and technology

Theory Bridge Exam Example Questions

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CS481F01 Prelim 2 Solutions

Computational Models - Lecture 4 1

Context-Free Grammars and Languages. Reading: Chapter 5

Testing Emptiness of a CFL. Testing Finiteness of a CFL. Testing Membership in a CFL. CYK Algorithm

The Pumping Lemma for Context Free Grammars

Automata Theory CS F-08 Context-Free Grammars

5 Context-Free Languages

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

Computability and Complexity

Decidability (intro.)

Notes for Comp 497 (Comp 454) Week 12 4/19/05. Today we look at some variations on machines we have already seen. Chapter 21

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Transcription:

Notes for Comp 497 (Comp 454) Week 10 4/5/05 Today look at the last two chapters in Part II. Cohen presents some results concerning context-free languages (CFL) and regular languages (RL) also some decidability issues. Errata (Chapter 17) Chapter 17 p. 383, two lines from the end, make that "(baabbbbb)(a)" p. 392, eight lines from the end, append "in" to the line. Earlier we looked at the union, intersection and Kleene closure of regular languages. Let us see what properties context-free languages have both in themselves and in conjunction with regular languages. First, where L 1 and L 2 are context-free languages, what can we say about (a) Union L 1 + L 2 (b) Product L 1 L 2 (c) * Closure L 1? All these turn out to be CFLs. We can prove this construction in a couple of ways (1) using grammars for the languages (2) using machines. Look at grammars here. Cohen also discusses machine-based proofs (a) Union THEOREM 36 If L 1 and L 2 are context free grammars, so is their union L 1 +L 2 For each of L 1 and L 2 there is a CFG. Call these grammars CFG 1 and CFG 2 respectively. We modify CFG 1 by adding a subscript (1) to each non terminal. Thus X becomes X 1. Similarly we modify CFG 2, adding a different subscript (2) to each non-terminal. Now we can combine the grammars into a single grammar with no ambiguity. Finally, we add a new rule S S 1 S 2 Thus we have devised a CFG that generates L1 + L2 and shown it is therefore a CFL.

(b) Product THEOREM 37 If L 1 and L 2 are context free grammars, so is their product L 1 L 2 Similar proof to that of Theorem 36 - subscripting followed by addition of S S 1 S 2 (c) Closure THEOREM 38 If L is a CFL then so is L*. Change S to S 1 throughout the existing grammar then add new rule S S S 1 Λ What about (d) intersection of two CFL and (e) complement of a CFL? (d) Intersection. THEOREM 39 The intersection of two CFL may or may not be a CFL. We can show that both possibilities exist. For example if both L 1 and L 2 are regular then L 1 L 2 is regular (Theorem 12) and therefore CF But what if L 1 = a n b n a m (m,n > 0) (This is CFL; we can easily devise a CFG for it) and L 2 = a n b m b m (m,n > 0) (Also CFL, we can easily devise a CFG for it) the intersection of these two languages is a n b n a n which we know is non-context-free. It turns out there is no algorithm to which we can give two CFL which will determine if the intersection is a CFL. (e) Complement THEOREM 40 The complement of a CLF may or may not be a CLF

Again we can show that both possibilities exist. If L is regular, and therefore CF, its complement is also regular (Theorem 11) and therefore CF. Consider the following proof by contradiction (that the complement of any CFL is CF). Assume that CFL is CF If L 1 and L 2 are CFL Then L 1 and L 2 are CFL (our assumption) Then (L 1 + L 2 ) is CFL (Theorem 36) Then (L 1 + L 2 ) is CFL (our assumption again) Then L 1 L 2 is CFL (de Morgan s law) But we know that the intersection of two CFL is not always CF so it must be the case that our assumption is wrong and CFL is not always CF. Mixing context-free and regular languages Union: CF + RL The union of CF and RL is CF because the RL is CF and we can apply Theorem 36. The union may or may not be regular depending which is the larger language E.g. 1: PALINDROME + (a+b)* (a+b)* is larger therefore union is regular E.g. 2: PALINDROME + a* PALINDROME is larger therefore union non-regular Intersection: CFL RL THEOREM 41 The intersection of a CFL and a RL is always CF. Proof is by construction. Given a PDA for the CFL and a FA for the RL we can construct a PDA for the intersection language. The states of the new PDA are combinations of the old PDA states and the FA states. Cohen sketches the construction logic on pages 394-395 and then shows how to construct a PDA which recognizes the intersection of EQUAL (CFL of strings with same number of a s as b s) and ENDA (RL of strings ending in a) Consider also DOUBLEWORD ( = ww where w = (a+b)* ) We know from Chapter 10 that it is non-regular We know from Chapter 16 that it is non-context-free (pumping lemma proof)

We can also prove it is non-regular by means of Theorem 41 and careful choice of a regular language with which to intersect it. Consider the intersection of DOUBLEWORD with aa*bb*aa*bb* The intersection is L = ww where w = a n b m where m,n > 0 i.e. a n b m a n b m But we know this language is non-context-free (see last week) so that means that DOUBLEWORD must be non-context free also otherwise it would contradict Theorem 41. Errata (Chapter 18) Chapter 18 p. 410, line 10, replace B A by B baa In this chapter we look at some decidability issues similar to those considered in Chapter 11 for RL. Paraphrasing Cohen, the first group of questions (p. 402) is: 1. Do two CFG define the same language? 2. Is a particular CFG ambiguous? 3. If a CFG is ambiguous, is there another CFG defining the same language that is not? 4. How can we tell if (CFL) is CF? 5. How can we tell if CFL 1 CFL 2 is CF? 6. Given CFG 1 defining CFL 1 and CFG 2 defining CFL 2 is CFL 1 CFL 2 empty? 7. Given CFG defining CFL, is CFL (a+b)* These questions are all undecidable no algorithm can exist for any of them. We will see more undecidable questions in Part III of the book. However, there are still some questions concerning CFG that we can answer: 1. (Emptiness) Does a particular CFG generate any words at all? 2. (Finiteness) Given a CFG, is the CFL it generates finite or infinite? 3. (Membership) Given a CFG, is a particular word w in the language it generates? We will see similar questions to these in Part III. Emptiness THEOREM 42 Given a CFG, there is an algorithm to determine if it generates any words at all

We can prove this is true by finding such an algorithm. We can tell if Λ is in the language (is S nullable?) Assume it is not and convert the grammar to CNF Find a rule of the form N t and back substitute If S is eliminated, we are done, CFG produces a string If we cannot eliminate S, CFG produces no strings. This is an algorithm because the back substitution must terminate in a finite number of steps there are a finite number of rules in the grammar. The example on page 405 is of a grammar that does produce strings. The example on page 406 is of a grammar that does not produce any strings. If you draw the derivation tree of the page 406 grammar, you will see that we can never get rid of all the nonterminals in a working string. The following Theorem is somewhat related THEOREM 43 Given a CFG with nonterminal X, there is an algorithm to determine if X is ever used in the generation of words. Again we can prove this Theorem by devising such an algorithm. We could break the problem down into two subproblems: (i) can we generate a string of terminals from X? (ii) Can we obtain from S, a working string containing X? (i) (ii) In a copy of the grammar (CFG 2 ) exchange S and X wherever they occur. Now apply the algorithm of Theorem 42 to CFG 2. If CFG 2 produces any words then X in CFG produces words. Back substitution of X to see if we can obtain a working string from S. In the example on page 408 X can be produced from S if A can be produced from S (second rule). A can be produced from S according to the first rule so X can be in a working string from S Finiteness THEOREM 44 There is an algorithm to decide if a given CFG generates a finite or an infinite language. Again the proof is to construct such an algorithm.

Note that if any word in the language is long enough to apply the pumping lemma to we can produce an infinite language. If the language is infinite there must be some words long enough to apply the pumping lemma to. So we need to determine if the pumping lemma can be applied need to see if there is a self-embedded nonterminal that is involved in the derivation of words in the language. Algorithm (1) eliminate useless nonterminals from the grammar (see Theorem 43) (2) back substitution similar to Theorem 43 to see if there is a self-embedded (directly or indirectly) nonterminal. Example on p. 410-411 Membership We would like to be able to determine if the language defined by a particular CFG contains a particular word w. THEOREM 45 Given a CFG and string x (over the same alphabet), we can determine if x can be generated by the CFG Once more the proof of the algorithm is the demonstration that an algorithm exists to answer the question. We assume the CFG is in CNF We wish to determine which from which substrings of x = x 1 x 2... x n are derivable nonterminals in the grammar S, N 1, N 2,... N M The substrings of length 1 are easy to identify because they are on the RHS of rules of the form N t. For each substring of length 1 we have a list of the nonterminals that can produce it. For a string of length 2 e.g. x i x j to be producable x i must be producable from N p, x j must be producable from N q and there is a rule N r N p N q. For each producable substring of length 2 we have a list of the nonterminals (N r ) that can produce it.

Similarly, we can determine which substrings of length 3 are producable, then which substrings of length 4 and so on. Eventually, we will consider the (sub)strings of length n (the length of the word of interest). If S is among the nonterminals that can produce it then x is in the language. Because the string x is finite in length, the algorithm is finite. Parsing Simple Arithmetic In chapter 3 we had a simple (recursive) grammar (AE) for arithmetic expressions. However, it did not reflect the different operator precedences. A better grammar PLUS- TIMES is given on Page 414. It distinguishes between a lower precedence operator (+) and a higher precedence operator (*). We can easily extend the grammar to include subtraction and division operators. We can also extend he grammar to include operators with precedence greater than * and operators with precedence lower than +. The derivation tree for an arithmetic expression generated using PLUS-MINUS will properly reflect operator precedence (see p 416) The parsing problem is how to determine whether a string x is in a language L. In this case whether a string is a valid arithmetic expression. Two approaches are: (1) Top-down start with S and see if we can generate x (2) Bottom-up start with x and see if we can reduce it to S (1) Top-down: On pages 416-420, Cohen gives an example of this approach showing how a derivation tree is grown and pruned and how, in this case, a tree can be constructed with w as its leaves. Note that we don t need to grow the tree in full; we can explore a branch then backtrack to the parent and try another if it doesn t work out. (2) Bottom-up: On pages 421-423. In this case the root of the tree is the string x and we try to construct a path to a leaf S. We know from Data Structures classes that a postfix (Reverse Polish) arithmetic expression can be evaluated using a stack of operands. A PDA has a stack so it seems reasonable to devise a PDA that can read a postfix expression and output its value. We need to add ADD, MPY and PRINT operators to our existing set of nodes. This PDA is given on page 424. We also know from Data Structures that Dijkstra s algorithm for converting infix to postfix also uses a stack. This time the stack contains operators and open parentheses. The PDA for this process is on Page 427. Input is an infix expression, output is the corresponding postfix.

Reading Assignment Read Chapters 17 and 18 Homework #5 Here is Homework #5 due 4/12. Each question is worth 20 points. Covers Chapters 16, 17 and 18. 1. Consider the grammar for { a n b n } S asb ab (a) Chomsky-ize this grammar. (b) Find all derivation trees that do not have self-embedded nonterminals. 2. (a) Find a CFG for the language L = (bb)*a (b) Find a CFG for L* (where L is the language in part (a)) 3. Show that Is context free. L = { a p b q a p b s } 4. For each of the following two grammars and target strings, determine whether the string can be generated from the grammar (use the CYK algorithm). (a) S XS X XX X a S b String baab (b) S AB A BB a B AB b

String bbaab 5. For each of the following two grammars, determine whether it generates any words (use the algorithm of Theorem 42) (a) (b) S XY X SY Y SX X a Y b S AB A BSB B AAS A CC B CC C SS A a b C b bb