Lexical Analysis Finite Automate

Similar documents
AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Convert the NFA into DFA

CMSC 330: Organization of Programming 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

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

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

Minimal DFA. minimal DFA for L starting from any other

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

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

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

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

Java II Finite Automata I

Lecture 08: Feb. 08, 2019

Worked out examples Finite Automata

Finite Automata-cont d

Deterministic Finite Automata

Lexical Analysis Part III

Harvard University Computer Science 121 Midterm October 23, 2012

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

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

Nondeterminism and Nodeterministic Automata

CHAPTER 1 Regular Languages. Contents

Designing finite automata II

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

FABER Formal Languages, Automata and Models of Computation

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

CS375: Logic and Theory of Computing

Chapter 2 Finite Automata

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

First Midterm Examination

Formal Language and Automata Theory (CS21004)

NFAs continued, Closure Properties of Regular Languages

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

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

CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Theory of Computation Regular Languages

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

Formal Languages and Automata

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

Closure Properties of Regular Languages

GNFA GNFA GNFA GNFA GNFA

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

Non-deterministic Finite Automata

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

Non-deterministic Finite Automata

Formal languages, automata, and theory of computation

Normal Forms for Context-free Grammars

Today s Topics Automata and Languages

Automata and Languages

First Midterm Examination

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

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

1 From NFA to regular expression

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

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

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

Finite-State Automata: Recap

Homework 3 Solutions

Thoery of Automata CS402

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

NFAs continued, Closure Properties of Regular Languages

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

1 Nondeterministic Finite Automata

CS375: Logic and Theory of Computing

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

Let's start with an example:

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

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

input tape head moves current state

Nondeterministic Automata vs Deterministic Automata

State Minimization for DFAs

Lecture 6 Regular Grammars

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

2. Lexical Analysis. Oscar Nierstrasz

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

Non-Deterministic Finite Automata

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

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

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

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

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.

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

3 Regular expressions

This lecture covers Chapter 8 of HMU: Properties of CFLs

1.4 Nonregular Languages

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers. Mehryar Mohri Courant Institute and Google Research

On Determinisation of History-Deterministic Automata.

CISC 4090 Theory of Computation

Closure Properties of Regular Languages

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

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

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

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

ɛ-closure, Kleene s Theorem,

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

Transcription:

Lexicl Anlysis Finite Automte CMPSC 470 Lecture 04 Topics: Deterministic Finite Automt (DFA) Nondeterministic Finite Automt (NFA) Regulr Expression NFA DFA A. Finite Automt (FA) FA re grph, like trnsition digrm, with few differences. 1. FA re recognizers; they sy yes or no out possile input string. 2. There re two types of FA. Non-deterministic Finite Automt (NFA): node/stte cn hve severl edge outs for symol. lel of edge cn hve.. Deterministic Finite Automt (DFA): node/stte hs unique edge out for symol. Edges don t hve. Both NFA nd DFA cn recognize the sme lnguge clled, regulr lnguge.

B. Non-deterministic Finite Automt (NFA) B.) NFA definition NFA consists of: 1. A finite set of sttes 2. Set of input symol 3. Trnsition function 4. Strt stte (or initil stte) 5. Accepting sttes (or finl sttes) B.) NFA Trnsition Tle One of the wys to represent NFA (trnsition function) NFA Accepts input string xx if nd only if there is some pth in trnsition grph from strt (ss 0 ) to one of ccepting sttes (FF). Is ccepted y NFA? Is ccepted y NFA? Is ccepted y NFA?

Exmple) Given the following NFA, strt 0 1 2 3 How mny pths re there for? Does NFA ccept? Does NFA ccept? C. Deterministic Finite Automt (DFA) DFA is specil cse of NFA 1. There re no move on input 2. For ech stte ss nd input symol, there is only one edge out of ss leled. Drw symol tle for DFA. strt 0 1 2 Exmple) Drw DFA ccepting ( ). Does this DFA ccept? How mny pths does it hs?

Does this DFA ccept? How mny pths does this hs? Given n input string, DFA is esier to nswer yes or no thn NFA, ecuse DFA provides. D. Convert NFA to DFA Regulr expression is used in lexicl nlyzer. Regulr expression cn e esily converted to NFA. However, implementing lexicl nlyzer requires DFA implementtion. First, we will discuss: 1. how to convert NFA to DFA, nd then 2. how to convert regulr expression to NFA. The generl ide is tht ech stte of DFA corresponds to set of NFA. D.) Opertions -closure(ss): Set of NFA sttes rechle from stte ss on -trnsition lone -closure(tt): Set of NFA sttes rechle from set of sttes TT on -closure(ss) move(tt, ): Set of NFA sttes rechle from set of sttes TT on input symol

Generte DFA Given NFA, the following sequence of steps uilds DFA sttes (Dsttes), which re sets of NFA sttes, nd trnsition tle (Dtrn). strt 0 1 2 4 3 5 6 7 8 9 10 Trnsition tle: NFA sttes DFA stte A B C D E 1. Strt stte: AA = -closure(s 0 ) 2. Dtrn[AA, ] = -closure move(aa, ) 3. Dtrn[A, ] = -closure move(aa, )

4. Dtrn[BB, ] = -closure move(bb, ) 5. Dtrn[BB, ] = -closure move(bb, ) 6. Dtrn[CC, ] = -closure move(cc, ) 7. Dtrn[CC, ] = -closure move(cc, ) 8. Dtrn[DD, ] = -closure move(dd, ) 9. Dtrn[DD, ] = -closure move(dd, ) 10. Dtrn[EE, ] = -closure move(ee, ) 11. Dtrn[EE, ] = -closure move(ee, ) 12. Since there is no more new DFA stte, stop. Note tht, mong possile DFA sttes, we hve only 5 sttes.

E. Convert regulr expression to NFA The McNughton-Ymd-Thompson lgorithm. NFA is constructed, recursively, initilly from ech tomic expression in regulr expression. BASIS: NAF for regulr expression : NFA for regulr expression in Σ: INDUCTION: Let ss nd tt re prts of regulr expression, nd NN(ss) nd NN(tt) re their corresponding NFAs, recursively. ) Suppose rr = ss tt, then NN(rr) = NN(ss) NN(tt), tht is ) Suppose rr = ssss, then NN(rr) = NN(ss)NN(tt), tht is c) Suppose rr = ss, then NN(rr), tht is d) Suppose rr = (ss), then LL(rr) = LL(ss)

Exmple) Convert the following regulr expression to NFA. ( ) ( ) 1. Build prse tree from the regulr expression 2. Recursively uild NFA from prse tree.

F. Minimize numer of sttes of DFA Is the following two DFAs sme? strt C A B D E strt 0 1 2 3 Two utomt re the sme up to stte nmes if one cn e trnsformed into nother y doing nothing more thn chnging the nme of sttes. Two sttes AA nd CC re equivlent ecuse: String xx distinguishes stte ss from stte tt if exctly one of the sttes reched from ss nd tt y following the pth with lel ss is n ccepting stte. Stte ss is distinguishle from stte tt if there exists string xx tht distinguishes them. Q: Are AA nd BB distinguishle? If then why?

Minimize the numer of sttes of DFA. There is lwys unique minimum stte DFA for ny regulr expression. The following steps show how to determine the minimum stte DFA. 1. Strt with initil prtition Π with two groups: FF for ccepting sttes, nd SS FF for non-ccepting sttes. 2. Π new Π 3. Updte ΠΠ: For ech group GG Π, prtition GG into sugroups such tht two sttes ss nd tt re in the sme group if nd only if, Σ, sttes ss nd tt hve trnsition on to sttes in the sme groups of Π. 4. Replce GG in Π new y the set of ll sugroups formed. 5. Repet 2-4, until Π new = Π. Then Π finl = Π 6. Choose one stte in ech group of Π finl s the representtive for tht group. the new stte is the minimum-stte DFA.

G. How lexicl nlyzer works The min question in this prt is how lexicl-nlyzer generted y tools, such s Lex, work recognize lexeme nd tokens from regulr expression. ) Pttern mtching sed on NFA Let you hve three ptterns:,, +. Its NFA is: strt 1 0 3 2 4 5 6 7 8 You hve the following input nd lexemebegin t 0 th loction How generted nlyzer works?

) Pttern mtching sed on DFA Given NFA, its DFA cn e determined s follows: strt 0,1,3,7 2,4,7 7 strt 1 0 3 7 2 4 8 5 6 8 6,8 5,8 In DFA, 247, 8, 68, 58 re ccepting sttes ecuse they hve ccepting sttes 2, 6, 8 in NFA. 247 ccepts pttern: 8 ccepts pttern: 58 ccepts pttern: 68 ccepts pttern: You hve the following input nd lexemebegin t 0 th loction. How generted nlyzer works?