Chapter 18: Decidability

Similar documents
Chapter 14: Pushdown Automata

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

Automata Theory TEST 1 Answers Max points: 156 Grade basis: 150 Median grade: 81%

CYK parsing. I love automata theory I love love automata automata theory I love automata love automata theory I love automata theory

ITEC2620 Introduction to Data Structures

Homework. Announcements. Before We Start. Languages. Plan for today. Chomsky Normal Form. Final Exam Dates have been announced

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Assignment No.4 solution. Pumping Lemma Version I and II. Where m = n! (n-factorial) and n = 1, 2, 3

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Context-Free Grammars

CMSC 330: Organization of Programming Languages

Parsing. Cocke Younger Kasami (CYK) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 35

CMSC 330: Organization of Programming Languages. Context Free Grammars

JNTUWORLD. Code No: R

CMSC 330: Organization of Programming Languages

Normal Forms. Suradet Jitprapaikulsarn First semester 2005

Decision Properties for Context-free Languages

Formal Languages. Grammar. Ryan Stansifer. Department of Computer Sciences Florida Institute of Technology Melbourne, Florida USA 32901

Section 1.7 Sequences, Summations Cardinality of Infinite Sets

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

CMSC 330: Organization of Programming Languages. Context Free Grammars

Section 2.4 Sequences and Summations

Lecture 8: Context Free Grammars

Ambiguous Grammars and Compactification

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

CT32 COMPUTER NETWORKS DEC 2015

Formal Languages and Automata

CMPSCI 250: Introduction to Computation. Lecture #28: Regular Expressions and Languages David Mix Barrington 2 April 2014

1. Which of the following regular expressions over {0, 1} denotes the set of all strings not containing 100 as a sub-string?

1. Provide two valid strings in the languages described by each of the following regular expressions, with alphabet Σ = {0,1,2}.

Normal Forms and Parsing. CS154 Chris Pollett Mar 14, 2007.

CHAPTER TWO LANGUAGES. Dr Zalmiyah Zakaria

Introduction to Syntax Analysis

Lecture 12: Cleaning up CFGs and Chomsky Normal

Compiler Construction

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Introduction to Syntax Analysis. The Second Phase of Front-End

The CYK Algorithm. We present now an algorithm to decide if w L(G), assuming G to be in Chomsky Normal Form.

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

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

UNIT I PART A PART B

QUESTION BANK. Unit 1. Introduction to Finite Automata

Glynda, the good witch of the North

Multiple Choice Questions

Chapter 4: Regular Expressions

CS375: Logic and Theory of Computing

The CYK Parsing Algorithm

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

CSCE 314 Programming Languages

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

Dr. D.M. Akbar Hussain

Exercise 1: Balanced Parentheses

Notes for Comp 454 Week 2

LING/C SC/PSYC 438/538. Lecture 22 Sandiway Fong

Specialized Grammars. Overview. Linear Grammars. Linear Grammars and Normal Forms. Yet Another Way to Specify Regular Languages

1.3 Functions and Equivalence Relations 1.4 Languages

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

CS402 - Theory of Automata FAQs By

Definition: two derivations are similar if one of them precedes the other.

KHALID PERVEZ (MBA+MCS) CHICHAWATNI

On the Parikh-de-Bruijn grid

Limits of Computation p.1/?? Limits of Computation p.2/??

CMSC 330 Practice Problem 4 Solutions

DHANALAKSHMI SRINIVASAN INSTITUTE OF RESEARCH AND TECHNOLOGY SIRUVACHUR, PERAMBALUR DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CS311 / MATH352 - AUTOMATA AND COMPLEXITY THEORY

Week 2: Syntax Specification, Grammars

Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

CSE 3302 Programming Languages Lecture 2: Syntax

CMSC 330, Fall 2009, Practice Problem 3 Solutions

Automata Theory CS S-FR Final Review

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CS210 THEORY OF COMPUTATION QUESTION BANK PART -A UNIT- I

Lec-5-HW-1, TM basics

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

CS402 - Theory of Automata Glossary By

Theory and Compiling COMP360

Finite State Automata are Limited. Let us use (context-free) grammars!

CS143 Midterm Fall 2008

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

MA513: Formal Languages and Automata Theory Topic: Context-free Grammars (CFG) Lecture Number 18 Date: September 12, 2011

Formal Languages and Compilers Lecture VI: Lexical Analysis

Context-Free Grammars and Languages (2015/11)

Solving systems of regular expression equations


LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Turing Machine Languages

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

1. Provide two valid strings in the languages described by each of the following regular expressions, with alphabet Σ = {0,1,2}.

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

CS 44 Exam #2 February 14, 2001

Welcome! CSC445 Models Of Computation Dr. Lutz Hamel Tyler 251

Finite Math - J-term Homework. Section Inverse of a Square Matrix

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Transcription:

Chapter 18: Decidability Peter Cappello Department of Computer Science University of California, Santa Barbara Santa Barbara, CA 93106 cappello@cs.ucsb.edu Please read the corresponding chapter before attending this lecture. These notes are not intended to be complete. They are supplemented with figures, and material that arises during the lecture period in response to questions. Based on Theory of Computing, 2nd Ed., D. Cohen, John Wiley & Sons, Inc. 1

Emptiness & Uselessness Theorem Let G be a CFG. Is L(G) =? is decidable. Proof 1. We give an algorithm to answer this question. 2. Λ L(G) S Λ. 3. The proof of Theorem 23 of Chapter 13 shows how to decide if a nonterminal, N, is nullable: N Λ. 4. Therefore, we know how to decide if Λ L(G). 5. We henceforth assume without loss of generality that Λ / L(G). 2

6. Assume without loss of generality that G is in CNF. 7. Execute the following procedure: (a) For each nonterminal N with a production of the form N t, for t T +, for terminal set T : i. replace occurrences of N on the RHS of all productions with t ii. remove N and all N-productions from the grammar. (b) Repeat the step above until either: S is eliminated, or there are no nonterminals with a production of the form N t, for t T +, for terminal set T. 8. The algorithm terminates after finitely many steps (no more than the number of nonterminals in the G. 9. If S is eliminated by the procedure, then L(G). 3

(When S was eliminated, there was a production of the form S t, for t T. That t L(G).) 10. If L(G), then S is eliminated by the procedure. (a) If L(G), then there is some w L(G). (b) Consider a parse tree for w. (c) Its parse tree shows how the above procedure eliminates S. Illustrate. 4

Example Consider this CFG: S XY X AX AA Y BY BB A a B b Step 1 yields: S XY X ax aa Y by bb Applying Step 1 to the resulting set of productions yields: 5

S aabb S is eliminated during the next iteration. Thus, this grammar generates a non-empty language. In particular, it generates aabb. 6

Example Consider this CFG: S XY X AX Y BY BB A a B b Step 1 yields: S XY X ax Y by bb Applying step 1 again yields: 7

S Xbb X ax At this point, no nonterminal has a production of the form N t, for some terminal t T +. The algorithm terminates with the answer No. 8

Theorem Let G be a CFG, and N be a nonterminal in G. Is N used to generate any w L(G)? is decidable. Proof 1. A nonterminal that cannot generate a string of terminals is unproductive. 2. Given a nonterminal, N, decide if it is unproductive as follows: (a) Apply the algorithm of the previous theorem. (b) If N is eliminated by this algorithm, then N is productive. 3. Use the following algorithm to decide if a N is used to generate some w L(G): (a) Find all unproductive nonterminals. 9

(b) Eliminate all productions involving unproductive nonterminals (i.e., on either the RHS or the LHS). (c) Paint all N s blue. (d) If any nonterminal, M, on the LHS has a production that has any blue on the RHS, paint all occurrences of M blue. Explain any. (e) Repeat the above step until nothing new is painted. (f) Return Yes if S is blue; else return No. 4. The algorithm terminates after finitely many steps, since each iteration paints at least 1 nonterminal blue. 5. If N is used in the generation of some w L(G), then S is painted blue: (a) If N is used in the generation of some w L(G), then let S w be the derivation. 10

(b) In the parse tree for this derivation, there is a path from the root (S) to a N node. (c) Going up from the N node, we have a sequence productions in which a nonterminal is painted blue. (d) This sequence of productions culminates in S being painted blue. 6. If S is painted blue, then N is used in the generation of some w L(G): (a) Since S is blue, there is a sequence of j productions that made it blue: N 1 (V + T ) N(V + T ) N 2 (V + T ) N 1 (V + T ) and so on until S (V + T ) N J (V + T ) 11

(b) Thus, there is a derivation S (V + T ) N J (V + T ) (V + T ) N J 1 (V + T ) N 1 (V + T ) N(V + T ). (c) Since N is productive, S w, for some w L(G). If a nonterminal is not useful, it is useless. A corollary of the previous theorem is that the question Is nonterminal N useless? is decidable. 12

Example Is X useful in the CFG below? S ABa baz b A Xb bza B baa X aza aaa Z ZAbA Z is unproductive. After removing unproductive productions, we have: S ABa b A Xb B baa 13

X aaa Paint A blue. Therefore, paint S blue. Therefore, answer Yes. 14

Theorem Let G be a CFG. The question Is L(G) finite? is decidable. Proof 1. L(G) is infinite there is a w L(G) that can be pumped (producing infinitely many other words in L(G)). 2. There is a w L(G) that can be pumped, if there is a self-embedded nonterminal that is not useless. 3. Use the algorithm of the previous theorem to identify all useless nonterminals in G. 4. Remove all productions that involve useless nonterminals from G. 5. Use the following algorithm to decide if any remaining nonterminal is self-embedded: 15

For each nonterminal N, do: (a) Change all occurrences of N on the LHS of productions to the letter ℵ. (b) Paint all occurrences of N on the RHS of productions blue. (c) If Y is the LHS of a production with some blue on the RHS, paint all occurrences of Y blue. (d) Repeat the above step until nothing new is painted blue. (e) If ℵ is blue, N is self-embedded. 6. A nonterminal is self-embedded L(G) is infinite. The algorithm is finite and works for the same reasons that the algorithm of the previous theorem is finite and works. 16

Example Does the grammar below generate an infinite language? S ABa baz b A Xb bza B baa X aza ba aaa Z ZAbA Z is unproductive, hence useless. After removing productions involving the useless Z, we have: S ABa b A Xb B baa 17

X ba aaa We search for a self-embedded nonterminal, starting with X. Change X on the LHS to ℵ: S ABa b A Xb B baa ℵ ba aaa Paint X blue. Therefore, paint A blue. Therefore, paint ℵ blue. Therefore, X is self-embedded. Therefore, L(G) is infinite. 18

Theorem Let G be a CFG and w a string of G s terminals. w L(G)? is decidable. The question Is Proof 1. Let w = w 1 w 2 w n. 2. WLOG, assume G is in CNF. 3. For each w i, create a list of nonterminals that can generate it. That is, N is on w i s list, if there is a production of the form N w i. 19

4. For 2 i n do: (a) For each substring of length i do: i. For 1 j < i do: A. break the substring into 2 pieces, the 1st of length j, the 2nd of length i j; B. Check to see if there is a production of the form N k N l N m such that: N l generates the 1st subsubstring N m generates the 2nd subsubstring C. If so, add N k to the list of nonterminals that can generate this substring; 5. If S is on the list of nonterminals that can generate w 1 w 2 w n, then w L(G). 20

Example Is baaba L(G), where G is given by the following set of productions: S AB BC A BA a B CC b C AB a 21

The following table is the result of applying the algorithm. Row i concerns substrings of length i. Column j concerns substrings that with w j. The entry in row i, column j is the set of nonterminals that can generate the substring of length i, that starts with w j. 1 2 3 4 5 5 {S, A, C} 4 - {S, A, C} 3 - {B} {B} 2 {S, A} {B} {S, C} {S, A} 1 {B} {A, C} {A, C} {B} {A, C} b a a b a Since S is in row 5 column 1, baaba L(G). 22