Converting Regular Expressions to Discrete Finite Automata: A Tutorial

Similar documents
Java II Finite Automata I

Convert the NFA into DFA

Let's start with an example:

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

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Minimal DFA. minimal DFA for L starting from any other

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

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

1 Nondeterministic Finite Automata

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

CHAPTER 1 Regular Languages. Contents

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

CMSC 330: Organization of Programming Languages

Deterministic Finite Automata

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

Worked out examples Finite Automata

Fundamentals of Computer Science

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

1 From NFA to regular expression

Designing finite automata II

Chapter 2 Finite Automata

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

Homework 3 Solutions

Lexical Analysis Finite Automate

Formal languages, automata, and theory of computation

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

Lecture 08: Feb. 08, 2019

Formal Language and Automata Theory (CS21004)

Some Theory of Computation Exercises Week 1

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

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular 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

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

Finite Automata-cont d

Theory of Computation Regular Languages

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

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

Lecture 09: Myhill-Nerode Theorem

Nondeterminism and Nodeterministic Automata

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

Non-deterministic Finite Automata

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

CISC 4090 Theory of Computation

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

Non Deterministic Automata. Formal Languages and Automata - Yonsei CS 1

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

Languages & Automata

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

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

Non-deterministic Finite Automata

3 Regular expressions

80 CHAPTER 2. DFA S, NFA S, REGULAR LANGUAGES. 2.6 Finite State Automata With Output: Transducers

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

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

Harvard University Computer Science 121 Midterm October 23, 2012

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

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

Chapter 1, Part 1. Regular Languages. CSC527, Chapter 1, Part 1 c 2012 Mitsunori Ogihara 1

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

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

Formal Languages and Automata

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Lexical Analysis Part III

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.

ɛ-closure, Kleene s Theorem,

Name Ima Sample ASU ID

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

Non-Deterministic Finite Automata

Finite-State Automata: Recap

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.

Thoery of Automata CS402

First Midterm Examination

Myhill-Nerode Theorem

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

Tutorial Automata and formal Languages

CS 275 Automata and Formal Language Theory

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

First Midterm Examination

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

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

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

NFAs continued, Closure Properties of Regular Languages

Lecture 9: LTL and Büchi Automata

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.

GNFA GNFA GNFA GNFA GNFA

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

Context-Free Grammars and Languages

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

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

Regular languages refresher

Normal Forms for Context-free Grammars

1.3 Regular Expressions

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

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

1.4 Nonregular Languages

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

Transcription:

Converting Regulr Expressions to Discrete Finite Automt: A Tutoril Dvid Christinsen 2013-01-03 This is tutoril on how to convert regulr expressions to nondeterministic finite utomt (NFA) nd how to convert these to deterministic finite utomt. It s ment to e strightforwrd nd esy to follow, rther thn worrying out every technicl detil. Plese see Toren Mogensen s ook for the nitty-gritty. In this tutoril, we write concrete regulr expressions in typewriter font nd vriles rnging over regulr expressions with vrious forms of the letter r. The regulr expression mtching the empty string is written, pronounced epsilon. We use α nd β, pronounced lph nd et, to represent ritrry symols. Converting Regulr Expressions to NFAs A regulr expression cn consist of the following constructions: It cn mtch the empty string, represented y. It cn mtch symol α from the lphet of the lnguge to e mtched. It cn mtch either of two regulr expressions r 1 nd r 2, written r 1 r 2. It cn mtch seuence of two regulr expressions r 1 nd r 2, written r 1 r 2. It cn mtch zero or more instnces of regulr expression r, written r*. We convert regulr expression to nondeterministic finite utomton (NFA) y considering ech cse in the ove definition. By definition, every utomton (whether NFA or DFA) hs single stte. An utomton my hve more thn one ccepting stte, ut ecuse of the wy the rules elow re constructed, the resulting NFAs will hve exctly one ccepting stte. Even if this were not the cse, it would e esy to crete uniue ccepting stte y simply creting new uniue ccepting stte nd inserting -trnsitions from the previous ccepting sttes to the new one. 1

We egin with the se cses, tht is, regulr expressions α nd. The symol α is mtched y n utomton tht hs stte, n ccepting stte, nd trnsition on α from the stte to the ccepting stte: α Becuse we re creting n NFA, we cn use the sme construction to mtch the empty expression, just with n -trnsition to the ccepting stte rther thn symol from the lphet. In other words, the regulr expression gives rise to the following utomton: In the remining cses, tht is, the composite regulr expressions, we need to represent the result of recursive use of the conversion procedure. The result of converting some regulr expression r to n NFA will e shown s follows: r The leftmost stte in the ox represents the stte of r s utomton, nd the ccepting stte to the right represent the ccepting sttes of r s utomton. A choice etween r 1 nd r 2, written r 1 r 2, is constructed y creting new stte with -trnsitions to the sttes of r 1 s nd r 2 s utomt, nd n ccepting stte with -trnsitions from their ccepting sttes, s follows: r 1 A seuence of two regulr expressions r 1 nd r 2, written r 1 r 2, is converted to n NFA y simply ttching the ccepting sttes of r 1 s NFA to the initil stte of r 2 s NFA: r 2 r 1 r 2 The NFA of the repeting expression r* hs n -trnsition from its stte to r s stte, nd n -trnsition from r s ccepting stte to its ccepting stte. We llow unlimited repetitions of r y inserting n -trnsition from its ccepting stte to its stte, nd we llow r to e skipped y inserting n -trnsition from the new stte directly to the new ccepting stte. 2

In picture form, the NFA corresponding to r* is s follows: r Converting NFAs to DFAs To convert n NFA to DFA, we must find wy to remove ll -trnsitions nd to ensure tht there is one trnsition per symol in ech stte. We do this y constructing DFA in which ech stte corresponds to set of some sttes from the NFA. In the DFA, trnsitions from stte y some symol α go to the stte tht consists of ll the possile NFA-sttes tht could e reched y α from some NFA stte contined in the present DFA stte. The resulting DFA simultes the given NFA in the sense tht single DFA-trnsition represents mny simultneous NFA-trnsitions. The first concept we need is the -closure, pronounced epsilon closure. The -closure of n NFA stte is the set contining long with ll sttes in the utomton tht re rechle y ny numer of -trnsitions from. In the following utomton, the -closures re given in the tle to the right: α 0 1 β 2 3 Stte -closure 0 { 0, 2, 3 } 1 { 1 } 2 { 2, 3 } 3 { 3 } Likewise, we cn define the -closure of set of sttes to e the sttes rechle y -trnsitions from its memers. In other words, this is the union of the -closures of its elements. To convert our NFA to its DFA counterprt, we egin y tking the -closure of the stte 0 of our NFA nd constructing new stte in our DFA corresponding to tht -closure. Next, for ech symol α in our lphet, we record the set of NFA sttes tht we cn rech from on tht symol. For ech such set, we mke DFA stte corresponding to its -closure, tking cre to do this only once for ech set. In the cse two sets re eul, we simply reuse the existing DFA stte tht we lredy constructed. This process is then repeted for ech of the new DFA sttes (tht is, set of NFA sttes) until we run out of DFA sttes to process. Finlly, every DFA stte whose corresponding set of NFA sttes contins n ccepting stte is itself mrked s n ccepting stte. Exmple For this exmple, we ll convert the regulr expression * into DFA. We eing with simple utomt mtching nd : 3

Next, we construct the utomton mtching * using the utomton for : Finlly, we ttch our -utomton to ech end using the rule for seuencing: To convert this NFA to DFA, we egin y leling the sttes so we cn refer to them during the process: 0 1 6 7 2 5 3 4 4

We egin y tking the -closure of the stte, 0. As there re no epsilon trnsitions, we simply hve the singleton set { 0 }. Our DFA is s follows: Stte NFA Sttes?? { 0 } The NFA stte 0 hs no trnsitions on. Therefore, we mrk this tle entry s n error. On, it hs trnsition to 1. The -closure of 1 is { 1, 2, 3, 5, 6 }. We ssign this set to new DFA stte, yielding the following utomton: Stte NFA Sttes Err { 0 }?? { 1, 2, 3, 5, 6 } The NFA stte 3 hs n -trnsition to 4, which is the only -trnsition in s NFA sttes. The -closure of 4 is { 2, 3, 4, 5, 6 }. The NFA stte 6 hs the only -trnsition in s NFA sttes, nd it leds to 7, whose -closure is simply { 7 }. Stte NFA Sttes Err { 0 } { 1, 2, 3, 5, 6 }?? { 2, 3, 4, 5, 6 }?? { 7 } Anlyzing, we find n -trnsition from the NFA stte 3 to the NFA stte 4. However, we lredy hve DFA stte corresponding to 4 s -closure; nmely, itself. Likewise, we hve -trnsition to 7, whose -closure is represented y the DFA stte. Stte NFA Sttes Err { 0 } { 1, 2, 3, 5, 6 } { 2, 3, 4, 5, 6 }?? { 7 } Finlly, we exmine, corresponding to { 7 }. There re no outgoing trnsitions from 7, so we mrk these s errors: Stte NFA Sttes Err { 0 } { 1, 2, 3, 5, 6 } { 2, 3, 4, 5, 6 } Err Err { 7 } 5

Finlly, we mrk ech DFA stte (set of NFA sttes) s ccepting if t lest one of its memer NFA sttes re ccepting. In this cse, only 7 is ccepting, which mens tht only is ccepting. Stte Accepting Err No No No Err Err Yes (from 7 ) The DFA is now fully constructed. Acknowledgments I would like to thnk Jnus Vrmrken for correcting n error in the first version of this tutoril. 6