CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Similar documents
Technische Universität München Winter term 2009/10 I7 Prof. J. Esparza / J. Křetínský / M. Luttenberger 11. Februar Solution

Minimal DFA. minimal DFA for L starting from any other

Nondeterministic Automata vs Deterministic Automata

= state, a = reading and q j

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER MACHINES AND THEIR LANGUAGES ANSWERS

NON-DETERMINISTIC FSA

State Minimization for DFAs

Regular languages refresher

Fundamentals of Computer Science

Finite State Automata and Determinisation

Nondeterministic Finite Automata

Regular expressions, Finite Automata, transition graphs are all the same!!

Myhill-Nerode Theorem

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

Let's start with an example:

CS 573 Automata Theory and Formal Languages

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem. Kleene s Theorem 2/16/15

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

Lecture 08: Feb. 08, 2019

CS375: Logic and Theory of Computing

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Running an NFA & the subset algorithm (NFA->DFA) CS 350 Fall 2018 gilray.org/classes/fall2018/cs350/

Lecture 6 Regular Grammars

Prefix-Free Regular-Expression Matching

Convert the NFA into DFA

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

First Midterm Examination

Finite Automata-cont d

Homework 3 Solutions

CS 310 (sec 20) - Winter Final Exam (solutions) SOLUTIONS

Formal Languages and Automata

Formal languages, automata, and theory of computation

2.4 Theoretical Foundations

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Harvard University Computer Science 121 Midterm October 23, 2012

CS 311 Homework 3 due 16:30, Thursday, 14 th October 2010

Non-deterministic Finite Automata

FABER Formal Languages, Automata and Models of Computation

Finite-State Automata: Recap

CMSC 330: Organization of Programming Languages

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

NFAs continued, Closure Properties of Regular Languages

Designing finite automata II

CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CSE 401 Compilers. Today s Agenda

Lecture 09: Myhill-Nerode Theorem

Nondeterminism and Nodeterministic Automata

First Midterm Examination

NFA and regex. the Boolean algebra of languages. non-deterministic machines. regular expressions

CS 330 Formal Methods and Models Dana Richards, George Mason University, Spring 2016 Quiz Solutions

Non-deterministic Finite Automata

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

CHAPTER 1 Regular Languages. Contents. definitions, examples, designing, regular operations. Non-deterministic Finite Automata (NFA)

Lexical Analysis Finite Automate

Worked out examples Finite Automata

Tutorial Automata and formal Languages

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

CS 330 Formal Methods and Models

Intermediate Math Circles Wednesday, November 14, 2018 Finite Automata II. Nickolas Rollick a b b. a b 4

NFAs continued, Closure Properties of Regular Languages

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

DFA minimisation using the Myhill-Nerode theorem

@#? Text Search ] { "!" Nondeterministic Finite Automata. Transformation NFA to DFA and Simulation of NFA. Text Search Using Automata

1 Nondeterministic Finite Automata

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

Project 6: Minigoals Towards Simplifying and Rewriting Expressions

GNFA GNFA GNFA GNFA GNFA

Chapter 2 Finite Automata

CS 275 Automata and Formal Language Theory

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

Some Theory of Computation Exercises Week 1

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

Lecture 6: Coding theory

Today s Topics Automata and Languages

Automata and Languages

input tape head moves current state

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2014

11.1 Finite Automata. CS125 Lecture 11 Fall Motivation: TMs without a tape: maybe we can at least fully understand such a simple model?

Java II Finite Automata I

Automata and Languages

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Thoery of Automata CS402

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

Deterministic Finite Automata

12.1 Nondeterminism Nondeterministic Finite Automata. a a b ε. CS125 Lecture 12 Fall 2016

1. For each of the following theorems, give a two or three sentence sketch of how the proof goes or why it is not true.

CSCI 340: Computational Models. Transition Graphs. Department of Computer Science

Theory of Computation Regular Languages

Formal Language and Automata Theory (CS21004)

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Transcription:

CS311 Computtionl Strutures Regulr Lnguges nd Regulr Grmmrs Leture 6 1

Wht we know so fr: RLs re losed under produt, union nd * Every RL n e written s RE, nd every RE represents RL Every RL n e reognized y NFA nd we know how to uild it NFAs nd DFA hve the sme power Every NFA n e turned in to DFA the suset onstrution 2

Whtʼs Next? How to turn FSA into regulr grmmr nd vie-vers Miniml-stte DFAs Myhill-Nerode Theorem Lnguge indistinguishility 3

Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4

Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4

Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T Vriles We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4

Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4

Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4

Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T Terminl symols We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4

Regulr Grmmrs Hein Setion 11.4.1 Whtʼs Regulr Grmmr? A prtiulr kind of grmmr in whih ll the produtions hve one of these forms: S ε S w S T S wt w is sequene of terminl symols t most one vrile n pper on the rhs, nd it must e on the right. Exmples: S Y Y Z S AB 5

Exmples 6

Exmples 6

Exmples * 6

Exmples * S ε S 6

Exmples * S ε S + S 6

Exmples * S ε S + S 6

Exmples * S ε S + (+)* S S ε S S 6

Exmples * S ε S + (+)* S S ε S S 6

Exmples * S ε S + (+)* S S ε S S * + * S A B A ε A B ε B 6

Exmples * S ε S + (+)* S S ε S S * + * S A B A ε A B ε B 6

Lnguges nd Grmmrs Any regulr lnguge hs regulr grmmr Any regulr grmmr genertes regulr lnguge 7

From NFA to Regulr Grmmr Hein Algorithm 11.11 1. Renme the sttes Q to set of upper-se letters 2. The strt symol of the grmmr is the nme of the strt stte q0. 3. For eh trnsition I J, rete the prodution I J. 4. For eh trnsition I ε J, rete the prodution I J. 5. For eh finl stte K, rete the prodution K ε. 8

Exmple A C W X 9

Exmple A C W X 10

Exmple A A A C W X 10

Exmple A A A C A C W X 10

Exmple A A A C A C A W W X 10

Exmple A A A C A C A W C C W X 10

Exmple A A A C A C A W C C C ε W X 10

Exmple A A A C A C A W C C C ε W X W X 10

Exmple A A A C A C A W C C C ε W X W X X ε 10

Exmple A C W X 11

Exmple A C A A C W W X 11

Exmple A C A A C W C C ε W X 11

Exmple A C A A C W C C ε W W X 11

From Regulr Grmmr to FSM Hein Algorithm 11.12 1. Trnsform the grmmr so tht ll produtions re of the form A x or A xb, where x is either single letter or ε. 2. The strt stte of the NFA is the grmmrʼs strt symol. 3. Crete stte QF nd dd it to the set F of finl sttes. 4. For eh prodution I J, rete the trnsition xxxxxxxxxxx 5. For eh prodution I J, rete the trnsition xxxxxxxxxxxx I J 6. For eh prodution K ε, dd K to the set of finl sttes F 7. For eh prodution I, rete the trnsition I I ε J Q F 12

Exmple S S B B ε B B 13

Exmple S S B S B ε B B 13

Exmple S S B S QF B ε B B 13

Exmple B S S B S QF B ε B B 13

Exmple B S S B S QF B ε B B 13

Exmple ε B S S B S QF B ε B B 13

Exmple ε B S S B S QF B ε B B 13

Exmple ε B S S B S QF B ε B B 13

Exmple ε B S S B S QF B ε B B Whtʼs the lnguge? 13

Exmple ε B S S B S QF B ε B B Whtʼs the lnguge? + * 13

Lnguge Indistinguishility Consider lnguge L over n lphet A. Two strings x, y A* re L- indistinguishle if for ll z A*, xz L whenever yz L. We write x L y L is n equivlene reltion The index of L is the numer of equivlene lsses indued y L 14

Exmple: L = + * + * = { ε,,,,,, } L? ε L? L? L? 15

Exmple: L = + * + * = { ε,,,,,, } L? ε L? L? L? Wht re the equivlene lsses of L? 1. {} 2. {,,,, } 3. {ε} 4. everything else 15

Myhill-Nerode Theorem The equivlene reltion L hrterizes extly wht the stte of n utomton tht epts L needs to rememer out the red portion of the input: if the red portion of the input is x, then the stte needs to rememer the equivlene lss [x]. This is suffiient, euse if x L y, then it does not mtter if the red portion of the input ws x or y; ll tht mtters (for deiding whether to ept or rejet) is the future portion of the input, sy z, euse xz L iff yz L. It is lso neessry, euse if x L y, then there is some possile future portion z of the input suh tht xz needs to e epted nd yz rejeted (or vie vers). 16

Theorem Sttement (Prt A) If the index of lnguge A is k, then there is k-stte DFA MA suh tht L(MA) = A 17

Mimimum-stte DFA For ny lnguge L, there is unique mimimum-stte DFA tht reognizes L unique mens unique up to n isomorphism, tht is, renming of the sttes. Any DFA n e trnsformed into minimum-stte DFA 18

Equivlent Sttes Two sttes p nd q in DFA m={q, Σ, q0, δ, F} re equivlent if, for ll z Σ*, ˆδ(p, z) δ*(p, w) is finl stte extly when ˆδ(q, z) δ*(q, w) is finl stte, i.e., p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) Is this n equivlene reltion? 19

How to Clulte Stte Equivlene, Exmple: 3 nd 4 re not equivlent (why)? First guess: 1 2 3 5 6 4 7 1, 2, 3, 5, 6 4, 7, This works for strings w of length 0 20

E0: 1, 2, 3, 5, 6 4, 7 2 3, 4 Tke sttes 1 nd 2 for ll single-hrter inputs, do we end up in 1 5 6 7 equivlent sttes? δ(1, ) = 2; δ(2, ) = 2, δ(1, ) = 5; δ(2, ) = 3. 5 3 in E0 So the pir 1, 2 stys in the sme equivlene lss in the next guess, E1 21

E0: 1, 2, 3, 5, 6 4, 7 2 3, 4 Wht out sttes 3 nd 5? 1 for ll single-hrter 5 6 7 inputs, do we end up in equivlent sttes?, δ(3, ) = 3; δ(5, ) = 6 δ(3, ) = 4; δ(5, ) = 5. 5 4 in E0 So the sttes 3 nd 5 re not in the sme equivlene lss in the next guess, E1 22 5

E1: 1, 2, 5 4, 7, 2 3 4 Wht out sttes 1 nd 5? 1 for ll single-hrter 5 6 7 inputs, do we end up in equivlent sttes?, δ(1, ) = 2; δ(5, ) = 6 δ(1, ) = 5; δ(5, ) = 5. 2 6 in E1 So the sttes 1 nd 5 re not in the sme equivlene lss in the next guess, E2 23 65

Then we repet to get the next guess E2 The equivlene reltion En represents sttes tht t in the sme wy fter reding input strings of length n Rememer, reltion is nothing more thn set of pirs. So we uild E0 E1 E2 When do we stop? 24

Minimizing DFA How n we esily ompute whether or not two sttes p nd q in DFA re equivlent? Suppose tht they re not equivlent: Then some (finite) string z will e epted when the mhine strts in p, nd rejeted when the mhine strts in q. p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) 25

Minimizing DFA How n we esily ompute whether or not two sttes p nd q in DFA re equivlent? Suppose tht they re not equivlent: Then some (finite) string z will e epted when the mhine strts in p, nd rejeted when the mhine strts in q. p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) 25

Computing sets of equivlent sttes E 0 p, q where p F q F E 1 = E 0 \{ p, q Σ. δ(p, ), δ(q, ) E 0 }... E n+1 = E n \{ p, q Σ. δ(p, ), δ(q, ) E n } 26

Construting Miniml DFA Hein Constrution 11.10 Algorithm to Construt Minimum-Stte DFA (11.10) Given: Output: A DFA with set of sttes S nd trnsition tle T. Assume tht ll sttes tht nnot e rehed from the strt stte hve lredy een thrown wy. A minimum-stte DFA reognizing the sme regulr lnguge s the input DFA. 1. Construt the equivlent pirs of sttes y lulting the desending sequene of sets of pirs E 0! E 1!... defined s follows: E 0 = {{s, t} s nd t re distint nd either oth sttes re finl or oth sttes re nonfinl}. E i+1 = {{s, t} {s, t} " E i nd for every " A either T(s, ) = T(t, ) or or {T(s, ), T(t, )} " E i }. The omputtion stops when E k = E k+1 for some index k. E k is the desired set of equivlent pirs.

2. Use the equivlene reltion generted y the pirs in E k to prtition S into set of equivlene lsses. These equivlene lsses re the sttes of the new DFA. 3. The strt stte is the equivlene lss ontining the strt stte of the input DFA. 4. A finl stte is ny equivlene lss ontining finl stte of the input DFA. 5. The trnsition tle T min for the minimum-stte DFA is defined s follows, where [s] denotes the equivlene lss ontining s nd is ny letter: T min ([s], ) = [T(s, )].