Properties of Context-Free Languages. Decision Properties Closure Properties

Similar documents
Automata and Formal Languages

Automata and Computability. Solutions to Exercises

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

Chapter 7 Uncomputability

C H A P T E R Regular Expressions regular expression

The Halting Problem is Undecidable

SRM UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF SOFTWARE ENGINEERING COURSE PLAN

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

CS103B Handout 17 Winter 2007 February 26, 2007 Languages and Regular Expressions

Automata Theory. Şubat 2006 Tuğrul Yılmaz Ankara Üniversitesi

Reading 13 : Finite State Automata and Regular Expressions

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

Formal Grammars and Languages

Pushdown automata. Informatics 2A: Lecture 9. Alex Simpson. 3 October, School of Informatics University of Edinburgh als@inf.ed.ac.

Lecture summary for Theory of Computation

Automata on Infinite Words and Trees

Honors Class (Foundations of) Informatics. Tom Verhoeff. Department of Mathematics & Computer Science Software Engineering & Technology

CS 3719 (Theory of Computation and Algorithms) Lecture 4

Regular Expressions and Automata using Haskell

Compiler Construction

Computability Theory

Regular Languages and Finite Automata

Introduction to Automata Theory. Reading: Chapter 1

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Finite Automata. Reading: Chapter 2

MACM 101 Discrete Mathematics I

I. GROUPS: BASIC DEFINITIONS AND EXAMPLES

3515ICT Theory of Computation Turing Machines

CS5236 Advanced Automata Theory

Model 2.4 Faculty member + student

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

Lecture 2: Regular Languages [Fa 14]

Overview of E0222: Automata and Computability

Scanner. tokens scanner parser IR. source code. errors

Pushdown Automata. place the input head on the leftmost input symbol. while symbol read = b and pile contains discs advance head remove disc from pile

Philadelphia University Faculty of Information Technology Department of Computer Science First Semester, 2007/2008.

CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

Continued Fractions and the Euclidean Algorithm

1. Prove that the empty set is a subset of every set.

Properties of Real Numbers

Deterministic Finite Automata

Computational Models Lecture 8, Spring 2009

Computer Architecture Syllabus of Qualifying Examination

1 Homework 1. [p 0 q i+j p i 1 q j+1 ] + [p i q j ] + [p i+1 q j p i+j q 0 ]

Introduction to Turing Machines

CS 341: Foundations of Computer Science II elearning Section Syllabus, Spring 2015

CAs and Turing Machines. The Basis for Universal Computation

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

How To Compare A Markov Algorithm To A Turing Machine

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

CS154. Turing Machines. Turing Machine. Turing Machines versus DFAs FINITE STATE CONTROL AI N P U T INFINITE TAPE. read write move.

1 Definition of a Turing machine

Parsing Beyond Context-Free Grammars: Introduction

Proseminar on Semantic Theory Fall 2013 Ling 720. Problem Set on the Formal Preliminaries : Answers and Notes

Lights and Darks of the Star-Free Star

Boolean Algebra Part 1

Regular Languages and Finite Automata

Quotient Rings and Field Extensions

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

SOLUTIONS TO ASSIGNMENT 1 MATH 576

26 Integers: Multiplication, Division, and Order

Lecture 2: Universality

Section 4.2: The Division Algorithm and Greatest Common Divisors

An Introduction to Braid Theory

Using the ac Method to Factor

Regular Languages and Finite State Machines

Introduction to Theory of Computation

Turing Machines: An Introduction

Informatique Fondamentale IMA S8

Turing Machines, Part I

THE TURING DEGREES AND THEIR LACK OF LINEAR ORDER

Two Way F finite Automata and Three Ways to Solve Them

Session 7 Fractions and Decimals

Primes in Sequences. Lee 1. By: Jae Young Lee. Project for MA 341 (Number Theory) Boston University Summer Term I 2009 Instructor: Kalin Kostadinov

Answer Key for California State Standards: Algebra I

=

INCIDENCE-BETWEENNESS GEOMETRY

Mathematical Induction

Online EFFECTIVE AS OF JANUARY 2013

Finite Automata and Regular Languages

Solution to Homework 2

Solutions to TOPICS IN ALGEBRA I.N. HERSTEIN. Part II: Group Theory

Kevin James. MTHSC 412 Section 2.4 Prime Factors and Greatest Comm

THEORY of COMPUTATION

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Omega Automata: Minimization and Learning 1

Follow links for Class Use and other Permissions. For more information send to:

Today s Topics. Primes & Greatest Common Divisors

THE DEGREES OF BI-HYPERHYPERIMMUNE SETS

MATH10212 Linear Algebra. Systems of Linear Equations. Definition. An n-dimensional vector is a row or a column of n numbers (or letters): a 1.

8 Divisibility and prime numbers

4.6 The Primitive Recursive Functions

Fundamentele Informatica II

Implementation of Recursively Enumerable Languages using Universal Turing Machine in JFLAP

Extension of measure

Transcription:

Properties of Context-Free Languages Decision Properties Closure Properties 1

Summary of Decision Properties As usual, when we talk about a CFL we really mean a representation for the CFL, e.g., a CFG or a PDA accepting by final state or empty stack. There are algorithms to decide if: 1. String w is in CFL L. 2. CFL L is empty. 3. CFL L is infinite. 2

Non-Decision Properties Many questions that can be decided for regular sets cannot be decided for CFL s. Example: Are two CFL s the same? Example: Are two CFL s disjoint? How would you do that for regular languages? Need theory of Turing machines and decidability to prove no algorithm exists. 3

Testing Emptiness We already did this. We learned to eliminate variables that generate no terminal string. If the start symbol is one of these, then the CFL is empty; otherwise not. 4

Testing Membership Want to know if string w is in L(G). Assume G is in CNF. Or convert the given grammar to CNF. w = ε is a special case, solved by testing if the start symbol is nullable. Algorithm (CYK ) is a good example of dynamic programming and runs in time O(n 3 ), where n = w. 5

CYK Algorithm Let w = a 1 a n. We construct an n-by-n triangular array of sets of variables. X ij = {variables A A =>* a i a j }. Induction on j i+1. The length of the derived string. Finally, ask if S is in X 1n. 6

CYK Algorithm (2) Basis: X ii = {A A -> a i is a production}. Induction: X ij = {A there is a production A -> BC and an integer k, with i < k < j, such that B is in X ik and C is in X k+1,j. 7

Example: CYK Algorithm Grammar: S -> AB, A -> BC a, B -> AC b, C -> a b String w = ababa X 12 ={B,S} X 23 ={A} X 34 ={B,S} X 45 ={A} X 11 ={A,C} X 22 ={B,C} X 33 ={A,C} X 44 ={B,C} X 55 ={A,C} 8

Example: CYK Algorithm Grammar: S -> AB, A -> BC a, B -> AC b, C -> a b String w = ababa X 13 ={} Yields nothing X 12 ={B,S} X 23 ={A} X 34 ={B,S} X 45 ={A} X 11 ={A,C} X 22 ={B,C} X 33 ={A,C} X 44 ={B,C} X 55 ={A,C} 9

Example: CYK Algorithm Grammar: S -> AB, A -> BC a, B -> AC b, C -> a b String w = ababa X 13 ={A} X 24 ={B,S} X 35 ={A} X 12 ={B,S} X 23 ={A} X 34 ={B,S} X 45 ={A} X 11 ={A,C} X 22 ={B,C} X 33 ={A,C} X 44 ={B,C} X 55 ={A,C} 10

Example: CYK Algorithm Grammar: S -> AB, A -> BC a, B -> AC b, C -> a b String w = ababa X 14 ={B,S} X 13 ={A} X 24 ={B,S} X 35 ={A} X 12 ={B,S} X 23 ={A} X 34 ={B,S} X 45 ={A} X 11 ={A,C} X 22 ={B,C} X 33 ={A,C} X 44 ={B,C} X 55 ={A,C} 11

Example: CYK Algorithm Grammar: S -> AB, A -> BC a, B -> AC b, C -> a b X String w = ababa 15 ={A} X 14 ={B,S} X 13 ={A} X 25 ={A} X 24 ={B,S} X 35 ={A} X 12 ={B,S} X 23 ={A} X 34 ={B,S} X 45 ={A} X 11 ={A,C} X 22 ={B,C} X 33 ={A,C} X 44 ={B,C} X 55 ={A,C} 12

Testing Infiniteness The idea is essentially the same as for regular languages. Use the pumping lemma constant n. If there is a string in the language of length between n and 2n-1, then the language is infinite; otherwise not. Let s work this out in class. 13

Closure Properties of CFL s CFL s are closed under union, concatenation, and Kleene closure. Also, under reversal, homomorphisms and inverse homomorphisms. But not under intersection or difference. 14

Closure of CFL s Under Union Let L and M be CFL s with grammars G and H, respectively. Assume G and H have no variables in common. Names of variables do not affect the language. Let S 1 and S 2 be the start symbols of G and H. 15

Closure Under Union (2) Form a new grammar for L M by combining all the symbols and productions of G and H. Then, add a new start symbol S. Add productions S -> S 1 S 2. 16

Closure Under Union (3) In the new grammar, all derivations start with S. The first step replaces S by either S 1 or S 2. In the first case, the result must be a string in L(G) = L, and in the second case a string in L(H) = M. 17

Closure of CFL s Under Concatenation Let L and M be CFL s with grammars G and H, respectively. Assume G and H have no variables in common. Let S 1 and S 2 be the start symbols of G and H. 18

Closure Under Concatenation (2) Form a new grammar for LM by starting with all symbols and productions of G and H. Add a new start symbol S. Add production S -> S 1 S 2. Every derivation from S results in a string in L followed by one in M. 19

Closure Under Star Let L have grammar G, with start symbol S 1. Form a new grammar for L* by introducing to G a new start symbol S and the productions S -> S 1 S ε. A rightmost derivation from S generates a sequence of zero or more S 1 s, each of which generates some string in L. 20

Closure of CFL s Under Reversal If L is a CFL with grammar G, form a grammar for L R by reversing the right side of every production. Example: Let G have S -> 0S1 01. The reversal of L(G) has grammar S -> 1S0 10. 21

Closure of CFL s Under Homomorphism Let L be a CFL with grammar G. Let h be a homomorphism on the terminal symbols of G. Construct a grammar for h(l) by replacing each terminal symbol a by h(a). 22

Example: Closure Under Homomorphism G has productions S -> 0S1 01. h is defined by h(0) = ab, h(1) = ε. h(l(g)) has the grammar with productions S -> abs ab. 23

Closure of CFL s Under Inverse Homomorphism Here, grammars don t help us. But a PDA construction serves nicely. Intuition: Let L = L(P) for some PDA P. Construct PDA P to accept h -1 (L). P simulates P, but keeps, as one component of a two-component state a buffer that holds the result of applying h to one input symbol. 24

Architecture of P Input: 0 0 1 1 h(0) Buffer State of P Read first remaining symbol in buffer as if it were input to P. Stack of P 25

Formal Construction of P States are pairs [q, b], where: 1. q is a state of P. 2. b is a suffix of h(a) for some symbol a. Thus, only a finite number of possible values for b. Stack symbols of P are those of P. Start state of P is [q 0,ε]. 26

Construction of P (2) Input symbols of P are the symbols to which h applies. Final states of P are the states [q, ε] such that q is a final state of P. 27

Transitions of P 1. δ ([q, ε], a, X) = {([q, h(a)], X)} for any input symbol a of P and any stack symbol X. When the buffer is empty, P can reload it. 2. δ ([q, bw], ε, X) contains ([p, w], ) if δ(q, b, X) contains (p, ), where b is either an input symbol of P or ε. Simulate P from the buffer. 28

Proving Correctness of P We need to show that L(P ) = h -1 (L(P)). Key argument: P makes the transition ([q 0, ε], w, Z 0 ) *([q, x], ε, ) if and only if P makes transition (q 0, y, Z 0 ) *(q, ε, ), h(w) = yx, and x is a suffix of the last symbol of w. Proof in both directions is an induction on the number of moves made. 29

Nonclosure Under Intersection Unlike the regular languages, the class of CFL s is not closed under. We know that L 1 = {0 n 1 n 2 n n > 1} is not a CFL (use the pumping lemma). However, L 2 = {0 n 1 n 2 i n > 1, i > 1} is. CFG: S -> AB, A -> 0A1 01, B -> 2B 2. So is L 3 = {0 i 1 n 2 n n > 1, i > 1}. But L 1 = L 2 L 3. 30

Nonclosure Under Difference We can prove something more general: Any class of languages that is closed under difference is closed under intersection. Proof: L M = L (L M). Thus, if CFL s were closed under difference, they would be closed under intersection, but they are not. 31

Intersection with a Regular Language Intersection of two CFL s need not be context free. But the intersection of a CFL with a regular language is always a CFL. Proof involves running a DFA in parallel with a PDA, and noting that the combination is a PDA. PDA s accept by final state. 32

DFA and PDA in Parallel Input DFA PDA Accept if both accept S t a c k Looks like the state of one PDA 33

Formal Construction Let the DFA A have transition function δ A. Let the PDA P have transition function δ P. States of combined PDA are [q,p], where q is a state of A and p a state of P. δ([q,p], a, X) contains ([δ A (q,a),r], ) if δ P (p, a, X) contains (r, ). Note a could be, in which case δ A (q,a) = q. 34

Formal Construction (2) Accepting states of combined PDA are those [q,p] such that q is an accepting state of A and p is an accepting state of P. Easy induction: ([q 0,p 0 ], w, Z 0 ) * ([q,p],, ) if and only if δ A (q 0,w) = q and in P: (p 0, w, Z 0 ) *(p,, ). 35