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

Similar documents
Lecture 08: Feb. 08, 2019

CHAPTER 1 Regular Languages. Contents

1 From NFA to regular expression

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

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Theory of Computation Regular Languages

Fundamentals of Computer Science

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.

Harvard University Computer Science 121 Midterm October 23, 2012

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

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

CS:4330 Theory of Computation Spring Regular Languages. Equivalences between Finite automata and REs. Haniel Barbosa

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Let's start with an example:

Deterministic Finite Automata

Some Theory of Computation Exercises Week 1

Homework 3 Solutions

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

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

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY. FLAC (15-453) - Spring L. Blum

NFAs continued, Closure Properties of Regular Languages

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Non-deterministic Finite Automata

Finite Automata-cont d

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

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.

1 Nondeterministic Finite Automata

CISC 4090 Theory of Computation

Non-deterministic Finite Automata

Lecture 09: Myhill-Nerode Theorem

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

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

First Midterm Examination

Finite-State Automata: Recap

Chapter 2 Finite Automata

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

Languages & Automata

CMSC 330: Organization of Programming Languages

CS375: Logic and Theory of Computing

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

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Worked out examples Finite Automata

1.3 Regular Expressions

Minimal DFA. minimal DFA for L starting from any other

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

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

3 Regular expressions

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

NFAs continued, Closure Properties of Regular Languages

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

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

ɛ-closure, Kleene s Theorem,

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

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

More on automata. Michael George. March 24 April 7, 2014

Lecture 6 Regular Grammars

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

First Midterm Examination

Coalgebra, Lecture 15: Equations for Deterministic Automata

Lexical Analysis Finite Automate

Today s Topics Automata and Languages

Automata and Languages

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

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

GNFA GNFA GNFA GNFA GNFA

Closure Properties of Regular Languages

Formal languages, automata, and theory of computation

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

CS 330 Formal Methods and Models

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

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

CS375: Logic and Theory of Computing

Nondeterminism and Nodeterministic Automata

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

CS 330 Formal Methods and Models

Regular languages refresher

Convert the NFA into DFA

Parse trees, ambiguity, and Chomsky normal form

Designing finite automata II

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

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

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

CS103 Handout 32 Fall 2016 November 11, 2016 Problem Set 7

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

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

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.

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

FABER Formal Languages, Automata and Models of Computation

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers

Turing Machines Part One

Closure Properties of Regular Languages

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

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

Thoery of Automata CS402

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

Formal Language and Automata Theory (CS21004)

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Name Ima Sample ASU ID

In-depth introduction to main models, concepts of theory of computation:

Transcription:

CS 301 Lecture 04 Regulr Expressions Stephen Checkowy Jnury 29, 2018 1 / 35

Review from lst time NFA N = (Q, Σ, δ, q 0, F ) where δ Q Σ P (Q) mps stte nd n lphet symol (or ) to set of sttes We run n NFA on n input w y keeping trck of ll possile sttes the NFA could e in We cn convert n NFA to DFA y letting ech stte of the DFA represent set of sttes in the NFA 2 / 35

Building new lnguges using regulr opertion Use regulr opertions to uild new lnguges A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } D = C E = A (B C) F = (D C) (B E) 3 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } 4 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } C = {} {} {} 4 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } C = {} {} {} = {} ({} {}) ({} {} {}) 4 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } C = {} {} {} = {} ({} {}) ({} {} {}) B = {} {} {} 4 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } C = {} {} {} = {} ({} {}) ({} {} {}) B = {} {} {} A = {} {} ({} Σ {}) ({} Σ {}) 4 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } C = {} {} {} = {} ({} {}) ({} {} {}) B = {} {} {} A = {} {} ({} Σ {}) ({} Σ {}) = {} {} ({} ({} {}) {}) ({} ({} {}) {}) 4 / 35

Descriing complex lnguges using simpler ones Use regulr opertions to rek complex lnguges down into simpler ones A = {w w strts nd ends with the sme symols} B = { k k 1} C = {,, } C = {} {} {} = {} ({} {}) ({} {} {}) B = {} {} {} A = {} {} ({} Σ {}) ({} Σ {}) = {} {} ({} ({} {}) {}) ({} ({} {}) {}) We roke ech lnguge down into lnguges contining {}, {}, or {} nd comined them using the three regulr opertions,, nd 4 / 35

Regulr expressions The rces ren t dding nything since ll of our sets re singletons; let s drop them Similrly, let s drop the much s how we drop multipliction symols Let s lso replce with (which we red s or ) This gives us regulr expressions (regex) A = {} {} ({} ({} {}) {}) ({} ({} {}) {}) = B = {} {} {} = C = {} ({} {}) ({} {} {}) = 5 / 35

Regulr expressions The rces ren t dding nything since ll of our sets re singletons; let s drop them Similrly, let s drop the much s how we drop multipliction symols Let s lso replce with (which we red s or ) This gives us regulr expressions (regex) A = {} {} ({} ({} {}) {}) ({} ({} {}) {}) = ( ) ( ) B = {} {} {} = C = {} ({} {}) ({} {} {}) = 5 / 35

Regulr expressions The rces ren t dding nything since ll of our sets re singletons; let s drop them Similrly, let s drop the much s how we drop multipliction symols Let s lso replce with (which we red s or ) This gives us regulr expressions (regex) A = {} {} ({} ({} {}) {}) ({} ({} {}) {}) = ( ) ( ) B = {} {} {} = C = {} ({} {}) ({} {} {}) = 5 / 35

Regulr expressions The rces ren t dding nything since ll of our sets re singletons; let s drop them Similrly, let s drop the much s how we drop multipliction symols Let s lso replce with (which we red s or ) This gives us regulr expressions (regex) A = {} {} ({} ({} {}) {}) ({} ({} {}) {}) = ( ) ( ) B = {} {} {} = C = {} ({} {}) ({} {} {}) = 5 / 35

Regulr expressions The rces ren t dding nything since ll of our sets re singletons; let s drop them Similrly, let s drop the much s how we drop multipliction symols Let s lso replce with (which we red s or ) This gives us regulr expressions (regex) A = {} {} ({} ({} {}) {}) ({} ({} {}) {}) = ( ) ( ) B = {} {} {} = C = {} ({} {}) ({} {} {}) = Order of opertion:,, Prentheses used for grouping We underline the expression to differentite the string from the regulr expression 5 / 35

Regulr expressions Six types of regulr expressions: three se types, three recursive types Regex Lnguge (very rrely used) {} t {t} for ech t Σ R 1 R 2 L(R 1 ) L(R 2 ) R 1 nd R 2 re regex R 1 R 2 L(R 1 ) L(R 2 ) R 1 nd R 2 re regex R L(R) R is regex As shorthnd, we ll use Σ to men (or similr for other lphets) A = Σ Σ 6 / 35

Techniclities Techniclly, regulr expression genertes or descries (regulr) lnguge, it is not lnguge itself Given regulr expression R, the lnguge L(R) is the set of strings generted y R E.g., R = genertes strings,,,... L(R) = { k k 0} A DFA M recognizes (regulr) lnguge L(M) ut we don t identify M with its lnguge Similrly, we shouldn t identify regulr expression R with its lnguge L(R); however it is customry to do so Still, even if we let {} =, tht doesn t men is the sme s! 7 / 35

Kleene str = { k k 0} 8 / 35

Kleene str = { k k 0} ( c) = {w w contins ny numer of,, or c in ny order} 8 / 35

Kleene str = { k k 0} ( c) = {w w contins ny numer of,, or c in ny order} ( ) = {w w is the conctention of 0 or more or } 8 / 35

Kleene str = { k k 0} ( c) = {w w contins ny numer of,, or c in ny order} ( ) = {w w is the conctention of 0 or more or } = { m n m, n 0} 8 / 35

Kleene str = { k k 0} ( c) = {w w contins ny numer of,, or c in ny order} ( ) = {w w is the conctention of 0 or more or } = { m n m, n 0} = {} = 8 / 35

Kleene str = { k k 0} ( c) = {w w contins ny numer of,, or c in ny order} ( ) = {w w is the conctention of 0 or more or } = { m n m, n 0} = {} = = {} = 8 / 35

Kleene str = { k k 0} ( c) = {w w contins ny numer of,, or c in ny order} ( ) = {w w is the conctention of 0 or more or } = { m n m, n 0} = {} = = {} = Σ = Σ = {w w is string over Σ} 8 / 35

Regulr expression exmples ΣΣ = {w w = 2} 9 / 35

Regulr expression exmples ΣΣ = {w w = 2} (ΣΣ) = {w w is even} 9 / 35

Regulr expression exmples ΣΣ = {w w = 2} (ΣΣ) = {w w is even} ( ) = {w every in w is followed y t lest one } 9 / 35

Regulr expression exmples ΣΣ = {w w = 2} (ΣΣ) = {w w is even} ( ) = {w every in w is followed y t lest one } ( ) = 9 / 35

Regulr expression exmples ΣΣ = {w w = 2} (ΣΣ) = {w w is even} ( ) = {w every in w is followed y t lest one } ( ) = = {w w contins exctly one } 9 / 35

Question 1 Wht strings re in the lnguge given y the regulr expression ( )( )? 10 / 35

Question 1 Wht strings re in the lnguge given y the regulr expression ( )( )?,,, 10 / 35

Question 2 True or flse. If lnguges A nd B ech contin 2 strings, then A B contins 4 strings. 11 / 35

Question 2 True or flse. If lnguges A nd B ech contin 2 strings, then A B contins 4 strings. Flse. Counter exmple: A = B = {, }. A B = {,, } Another counter exmple A = {, } nd B = {, }. A B = {,, } 11 / 35

Question 3 Is in the lnguge given y ( )? 12 / 35

Question 3 Is in the lnguge given y ( )? Yes. = 12 / 35

Question 4 Write regex for the lnguge {w is sustring of w} 13 / 35

Question 4 Write regex for the lnguge {w is sustring of w} Σ Σ 13 / 35

Question 5 Write regex for the lnguge {w the second symol of w is or the third to lst symol of w is } 14 / 35

Question 5 Write regex for the lnguge {w the second symol of w is or the third to lst symol of w is } ΣΣ Σ ΣΣ 14 / 35

Question 6 Let Σ = {0, 1,..., 9, } nd D = 0 1 9. Wht strings re generted y the following regulr expression? ((1 )DDD )DDD DDD 15 / 35

Question 6 Let Σ = {0, 1,..., 9, } nd D = 0 1 9. Wht strings re generted y the following regulr expression? ((1 )DDD )DDD DDD U.S. phone numers. We cn rewrite this regex s 1 DDD DDD DDDD DDD DDD DDDD DDD DDDD 15 / 35

Question 7 If R is regulr expression, then the lnguge generted y R is either infinite or contins exctly one string. Under wht condition on R is R infinite? When R contins exctly one string, wht is the string nd wht is R? 16 / 35

Question 7 If R is regulr expression, then the lnguge generted y R is either infinite or contins exctly one string. Under wht condition on R is R infinite? When R contins exctly one string, wht is the string nd wht is R? R is infinite if R contins t lest one nonempty string R contins exctly one string, nmely, when R = or R = 16 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 17 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 R 1 = R 1 17 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 R 1 = R 1 (R 1 R 2 )R 3 = R 1 R 3 R 2 R 3 17 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 R 1 = R 1 (R 1 R 2 )R 3 = R 1 R 3 R 2 R 3 R 1 (R 2 R 3 ) = R 1 R 2 R 1 R 3 17 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 R 1 = R 1 (R 1 R 2 )R 3 = R 1 R 3 R 2 R 3 R 1 (R 2 R 3 ) = R 1 R 2 R 1 R 3 (R1 ) = R1 17 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 R 1 = R 1 (R 1 R 2 )R 3 = R 1 R 3 R 2 R 3 R 1 (R 2 R 3 ) = R 1 R 2 R 1 R 3 (R1 ) = R1 (R 1 R 2 ) = (R1 R2 ) 17 / 35

Regulr expression mnipultion Let R 1, R 2, nd R 3 e regulr expressions R 1 = R 1 R 1 = R 1 (R 1 R 2 )R 3 = R 1 R 3 R 2 R 3 R 1 (R 2 R 3 ) = R 1 R 2 R 1 R 3 (R1 ) = R1 (R 1 R 2 ) = (R1 R2 ) Theorem Every regulr expression R cn e rewritten s n equivlent regulr expression R 1 R 2 R k such tht none of the R i contin n or ( ) 17 / 35

Converting regulr expressions to NFAs Theorem Every regulr expression R cn e converted to n equivlent NFA N. I.e., L(N) = L(R) Proof ide Induction on the structure of the regex We need to construct NFAs directly for the three se cses,, nd t for t Σ Then, we hndle the three inductive cses, R 1 R 2, R 1 R 2, nd R 1 For the inductive cses, we ssume there exist NFAs for R 1 nd R 2 nd use them to uild NFAs for the three inductive cses 18 / 35

Converting regulr expressions to NFAs Proof. Bse cses. 1 R = 19 / 35

Converting regulr expressions to NFAs Proof. Bse cses. 1 R = 2 R = 19 / 35

Converting regulr expressions to NFAs Proof. Bse cses. 1 R = 2 R = 3 R = t t for t Σ 19 / 35

Converting regulr expressions to NFAs Proof. Bse cses. 1 R = 2 R = 3 R = t Inductive cses. 4 R = R 1 R 2 5 R = R 1 R 2 6 R = R 1 t for t Σ 19 / 35

Converting regulr expressions to NFAs Proof. Bse cses. 1 R = 2 R = 3 R = t Inductive cses. 4 R = R 1 R 2 5 R = R 1 R 2 6 R = R 1 t for t Σ By the inductive hypothesis, there exist NFAs N 1 nd N 2 such tht L(N 1 ) = L(R 1 ) nd L(N 2 ) = L(R 2 ). 19 / 35

Converting regulr expressions to NFAs Proof. Bse cses. 1 R = 2 R = 3 R = t Inductive cses. 4 R = R 1 R 2 5 R = R 1 R 2 6 R = R 1 t for t Σ By the inductive hypothesis, there exist NFAs N 1 nd N 2 such tht L(N 1 ) = L(R 1 ) nd L(N 2 ) = L(R 2 ). Since regulr lnguges re closed under union, conctention, nd Kleene str, L(R) is regulr so there exists some NFA N such tht L(N) = L(R). 19 / 35

Converting regulr expressions to NFAs The proof of the inductive cses pplied previous theorems to show tht some NFA exists But we know how to perform the constructions explicitly: N 1 N 1 N 2 N 2 N 1 20 / 35

Regulr expressions descrie regulr lnguges The lnguge of regulr expression is regulr This follow directly from the previous theorem: Regulr expression NFA DFA regulr lnguge 21 / 35

Regulr expression to NFA: R = () () 1 22 / 35

Regulr expression to NFA: R = () () 1 2 22 / 35

Regulr expression to NFA: R = () () 1 2 3 22 / 35

Regulr expression to NFA: R = () () 1 2 3 4 () 22 / 35

Regulr expression to NFA: R = () () 1 2 3 4 () 5 () 22 / 35

Regulr expression to NFA: R = () () 1 2 3 4 () 5 () 6 () 22 / 35

Regulr expression to NFA: R = () () 1 2 3 4 () 5 () 6 () 7 R 22 / 35

Not the smllest possile NFA 23 / 35

Not the smllest possile NFA 23 / 35

Not the smllest possile NFA 23 / 35

Not the smllest possile NFA 23 / 35

Not the smllest possile NFA 23 / 35

Not the smllest possile NFA "Accepted 23 / 35

Not the smllest possile NFA "Accepted 24 / 35

Not the smllest possile NFA "Accepted 24 / 35

Not the smllest possile NFA "Accepted 24 / 35

Not the smllest possile NFA "Accepted 24 / 35

Not the smllest possile NFA "Accepted $Rejected 24 / 35

Not the smllest possile NFA "Accepted $Rejected 25 / 35

Not the smllest possile NFA "Accepted $Rejected 25 / 35

Not the smllest possile NFA "Accepted $Rejected 25 / 35

Not the smllest possile NFA "Accepted $Rejected $Rejected 25 / 35

Converting from NFAs to regex Theorem Every NFA (nd thus every DFA) cn e converted to n equivlent regulr expression. Proof ide 1 Convert the NFA to new type of finite utomton whose edges re leled with regulr expressions 2 Remove sttes nd updte trnsitions one t time from the new utomton to produce n equivlent utomton 3 When only the strt nd (single) ccept stte remin, the trnsition etween them is the regulr expression 26 / 35

Generlized NFA (GNFA) A GNFA is finite utomton with single ccept stte, no trnsitions to the strt stte, no trnsitions from the ccept stte, nd ech trnsition is leled with regulr expression q 1 q 0 q q 2 27 / 35

GNFA cceptnce A GNFA trnsitions from one stte to the next y reding lock of input symols generted y the regex q 1 q 0 q q 2 28 / 35

GNFA cceptnce A GNFA trnsitions from one stte to the next y reding lock of input symols generted y the regex q 1 q 0 q q 2 28 / 35

GNFA cceptnce A GNFA trnsitions from one stte to the next y reding lock of input symols generted y the regex q 1 q 0 q q 2 28 / 35

GNFA cceptnce A GNFA trnsitions from one stte to the next y reding lock of input symols generted y the regex q 1 q 0 q q 2 28 / 35

GNFA cceptnce A GNFA trnsitions from one stte to the next y reding lock of input symols generted y the regex q 1 q 0 q q 2 "Accepted 28 / 35

Removing sttes in GNFA 1 Select stte to remove r other thn the strt or ccept sttes (r Q {q 0, q }) 2 For ech q, s Q {r} we hve R 2 R 1 R 3 q r s R 4 If trnsition is missing from the GNFA, then the corresponding regex is Remove stte r nd replce regex R 4 with R 1 R 2 R3 R 4 q R 1 R 2 R 3 R 4 s 29 / 35

Removing sttes in GNFA 1 Select stte to remove r other thn the strt or ccept sttes (r Q {q 0, q }) 2 For ech q, s Q {r} we hve R 2 R 4 R 2 R 1 R 3 R 1 q r s q r R 4 If trnsition is missing from the GNFA, then the corresponding regex is Remove stte r nd replce regex R 4 with R 1 R 2 R3 R 4 R 3 q R 1 R2 R 3 R 4 s q R 1 R2 R 3 R 4 29 / 35

Remove stte q 1 q 1 q 0 q q 2 q 0 q q 2 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = q 2 q 0 q q 2 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q q 2 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q q 2 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q ( )( ) q 2 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q ( )( ) q 2 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q ( )( ) q 2 ( ) 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q ( )( ) q 2 ( ) 30 / 35

Remove stte q 1 R 1 = q 1 R 2 = R 3 = q 0 q R 4 = ( )( ) q 2 q 0 q ( )( ) q 2 ( ) ( ) 30 / 35

Remove stte q 2 ( )( ) q 0 q ( )( ) q 2 ( ) ( ) (( )( ) )(( ) ) (( ) ) (( )( ) ) q 0 q 31 / 35

Converting GNFA to regulr expression Remove sttes one t time until only the strt nd ccept remin The one remining trnsition is n equivlent regex G: q 1 q 0 q q 2 L(G) = (( )( ) )(( ) ) (( ) ) (( )( ) ) 32 / 35

Converting n NFA (or DFA) to GNFA 1 Add new strt stte with n epsilon trnsition to the originl strt stte 2 Add new ccept stte with epsilon trnsitions from the originl ccept sttes 3 Convert multiple trnsitions etween pir of nodes to single regex using to seprte them, 33 / 35

Converting n NFA (or DFA) to regulr expression Theorem Every NFA (nd thus every DFA) cn e converted to n equivlent regulr expression. Proof. Given n NFA N, convert it to n equivlent GNFA G. Convert G to n equivlent regulr expression. (Some detils missing, ut see the ook.) 34 / 35

Exmple, q 1 q 2 First, convert to GNFA. q 0 q 1 q 2 q 35 / 35

Exmple, q 1 q 2 First, convert to GNFA. q 0 q 1 q 2 q ( ) Next, remove q 1 q 0 ( ) q 2 q 35 / 35

Exmple, q 1 q 2 First, convert to GNFA. q 0 q 1 q 2 q ( ) Next, remove q 1 q 0 ( ) q 2 q ( )(( )) Next, remove q 2 q 0 q 35 / 35

Exmple, q 1 q 2 First, convert to GNFA. q 0 q 1 q 2 q ( ) Next, remove q 1 q 0 ( ) q 2 q ( )(( )) Next, remove q 2 q 0 q Equivlent regulr expression ( )(( )) 35 / 35

Exmple, q 1 q 2 First, convert to GNFA. q 0 q 1 q 2 q ( ) Next, remove q 1 q 0 ( ) q 2 q ( )(( )) Next, remove q 2 q 0 q Equivlent regulr expression ( )(( )) = Σ(Σ) 35 / 35