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

Similar documents
CS103B Handout 18 Winter 2007 February 28, 2007 Finite Automata

Homework 3 Solutions

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Worked out examples Finite Automata

Minimal DFA. minimal DFA for L starting from any other

Convert the NFA into DFA

1 Nondeterministic Finite 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.

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

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

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

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

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

Formal languages, automata, and theory of computation

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

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

Finite Automata-cont d

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

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

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

First Midterm Examination

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

Let's start with an example:

FABER Formal Languages, Automata and Models of Computation

CMSC 330: Organization of Programming Languages

Deterministic Finite Automata

Name Ima Sample ASU ID

Java II Finite Automata I

Formal Languages and Automata

Lecture 09: Myhill-Nerode Theorem

Lecture 08: Feb. 08, 2019

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

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

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

Coalgebra, Lecture 15: Equations for Deterministic Automata

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

Harvard University Computer Science 121 Midterm October 23, 2012

1 From NFA to regular expression

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

Thoery of Automata CS402

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.

Model Reduction of Finite State Machines by Contraction

First Midterm Examination

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

Converting Regular Expressions to Discrete Finite Automata: A Tutorial

CISC 4090 Theory of Computation

CHAPTER 1 Regular Languages. Contents

Lexical Analysis Finite Automate

Some Theory of Computation Exercises Week 1

GNFA GNFA GNFA GNFA GNFA

State Minimization for DFAs

3 Regular expressions

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

NFAs continued, Closure Properties of Regular Languages

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Homework Solution - Set 5 Due: Friday 10/03/08

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

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

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.

Non-deterministic Finite Automata

Fundamentals of Computer Science

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

Nondeterminism and Nodeterministic Automata

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

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

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

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

PART 2. REGULAR LANGUAGES, GRAMMARS AND AUTOMATA

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

Non-deterministic Finite Automata

Lexical Analysis Part III

NON-DETERMINISTIC FSA

Closure Properties of Regular Languages

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

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

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

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

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

Tutorial Automata and formal Languages

Chapter 2 Finite Automata

Review for the Midterm

CS 330 Formal Methods and Models

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

Normal Forms for Context-free Grammars

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

I1 = I2 I1 = I2 + I3 I1 + I2 = I3 + I4 I 3

Formal Language and Automata Theory (CS21004)

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

Context-Free Grammars and Languages

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

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

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

NFAs continued, Closure Properties of Regular Languages

Languages & Automata

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

DFA minimisation using the Myhill-Nerode theorem

Revision Sheet. (a) Give a regular expression for each of the following languages:

Transcription:

University of Southern Cliforni Computer Science Deprtment Compiler Design Fll Lexicl Anlysis Smple Exercises nd Solutions Prof. Pedro C. Diniz USC / Informtion Sciences Institute 4676 Admirlty Wy, Suite Mrin del Rey, Cliforni 99 pedro@isi.edu Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment Prolem : Considering the lphet Σ = {,} derive Non-Deterministic-Finite Automton (NFA) using the Thompson construction tht is le to recognize the sentences generted y the regulr expression *( )*. Does the sentence w = elong to the lnguge generted y this regulr expression? Justify. Solution: Considering simplifction of the comintion of the NFA during the Thompson construction (e.g., sequence of two -trnsitions cn e considered s single -trnsition) possile NFA is s shown elow nd where the strt stte is the stte leled. 4 6 8 7 9 The word w = elongs to the lnguge generted y this RE ecuse there is pth from the strt stte to the ccepting stte tht spells out the word, respectively the pth,,4,6,8,,,,,,. Prolem : Strting from the NFA you derived in prolem, convert it to DFA using the su-set construction descried in clss. Verify tht the DFA yields the sme output s the originl NFA for the sme input string w. Solution: Using the -closure nd DFAedge computtion s descried in clss, we hve the following mpping of set of sttes of the NFA to sets of sttes of the DFA: I = -closure () = {,,, 4, } I = DFAedge(I,) = -closure ({,,, 4, }, ) = {6} I = DFAedge(I,) = -closure ({,,, 4, }, ) = {,,, 4,, 7} I = DFAedge(I,) = -closure ({6}, ) = Ierr I4 = DFAedge(I,) = -closure ({6}, ) = {8,,, } I = DFAedge(I,) = -closure ({,,, 4,, 7}, ) = {6, 9,,, } I6 = DFAedge(I,) = -closure ({,,, 4,, 7}, ) = {,,, 4,, 7} = I I7 = DFAedge(I4,) = -closure ({8,,, }, ) = Ierr I8 = DFAedge(I4,) = -closure ({8,,, }, ) = {,, } I9 = DFAedge(I,) = -closure ({6, 9,,, }, ) = Ierr I = DFAedge(I,) = -closure ({6, 9,,, }, ) = {8,,,, } I = DFAedge(I8,) = -closure ({,, }, ) = Ierr I = DFAedge(I8,) = -closure ({,, }, ) = {,, } = I8 I = DFAedge(I,) = -closure ({8,,,, }, ) = Ierr I4 = DFAedge(I,) = -closure ({8,,,, }, ) = I8 Effectively there re only 8 sttes s shown in the DFA elow nd this is clerly not the miniml DFA tht cn recognize this regulr expression. Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment, Ierr I I I4 I8 I I I For the input sentence w = in his DFA we would rech the stte I8, through sttes I, I4 nd I8 nd thus ccepting this string. Prolem : Strting from the DFA you constructed in the previous prolem, convert it to miniml DFA using the minimiztion lgorithm descried in clss. Verify tht the DFA yields the sme output s the originl NFA for the sme input string w. Solution: The first prtition would hve the two sets of sttes P = {I, I, I, Ierr} nd = {I4, I, I8, I}. A second prtition would mintin nd prtition P into {I, I, Ierr} nd generte P = {I} since I goes to itself on nd to on. Next the lgorithm would generte = {I) nd next generte P = {Ierr} leving P = {I}. The finl DFA would hve therefore sttes hs shown elow., P P P Agin on the input string w = the DFA would trverse P,, nd loop in therefore ccepting the string. Importnt Note: If you try to pply the sme construction, i.e. prtitioning the set of sttes strting with n incomplete DFA where the trp stte is missing you might e surprised to find tht you rech t n incorrect miniml DFA. Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment Prolem 4: Considering the lphet Σ = {,} construct Non-Deterministic-Finite Automton (NFA) using the Thompson construction tht is le to recognize the sentences generted y the regulr expression (**)**. Does the sentence w = elong to the lnguge generted y this regulr expression? Justify. Solution: The NFA is s shown elow nd where the strt stte is stte leled. 4 6 7 8 9 S = -closure () = {,,, 4,,, } this is finl stte ecuse of = DFAedge(S,) = -closure (S, ) = {, 6, 8} S = DFAedge(S,) = -closure (S, ) = {,, 4,,, } finl stte DFAedge(S,) = -closure (S, ) = {, 6, 8} = DFAedge(S,) = -closure (S, ) = {,, 4,,, } = S S = DFAedge(,) = -closure (, ) = {,, 4, 9,,, } finl stte S4 = DFAedge(,) = -closure (, ) = {6, 7, 8} DFAedge(S4,) = -closure (S4, ) = {,, 4, 9,,, } = S DFAedge(S4,) = -closure (S4, ) = {6, 7, 8} = S4 DFAedge(S,) = -closure (S, ) = {, 6, 8} = DFAedge(S,) = -closure (S, ) = {,, 4,,, } = S This results in the DFA shown elow with strting stte S. S S S S S4 4 This DFA cn e further minimize y recognizing tht S, S nd S form self-contined prtition nd nd S4 nother prtition resulting in the DFA on the right-hnd-side. Lexicl Anlysis Smple Exercises 4 Fll

University of Southern Cliforni Computer Science Deprtment Prolem : Consider the lphet Σ = {,}.. Construct Non-Deterministic-Finite Automton (NFA) using the Thompson construction tht is le to recognize the sentences generted y the regulr expression RE = ()*.()*.. Do the sentences w = nd w = elong to the lnguge generted y this regulr expression? Justify. c. Convert the NFA in prt ) to DFA using the suset construction. Show the mpping etween the sttes in the NFA nd the resulting DFA. d. Minimize the DFA using the itertive refinement lgorithm discussed in clss. Show your intermedite prtition results nd doule check the DFA using the sentences w nd w. Solution: ) A possile construction (lredy simplified to hve only single -trnsition etween sttes) would result in the NFA shown elow nd where the strt stte is leled. 4 6 ) Both words re recognized y this NFA. Regrding the word there is pth from stte to the ccepting stte 6, nmely:,,,,4,,4,,6. Regrding the word the utomton my rech stte 6 following the pth,,,4,,4,,4,,6. c) Using the suset construction we rrive t the following susets nd trnsitions. S = -closure () = {,,, 4,, 6} this is finl stte ecuse of stte 6 S = DFAedge(S,) = -closure (goto(s, )) = {, 4,, 6} finl stte SE = DFAedge(S,) = -closure (goto(s, )) = { } = DFAedge(S,) = -closure (goto(s, )) = {4,, 6} finl stte S = DFAedge(S,) = -closure (goto(s, )) = {, 4,, 6} finl stte DFAedge(,) = -closure (goto(, )) = {4,, 6} = DFAedge(,) = -closure (goto(, )) = { } = SE DFAedge(S,) = -closure (goto(s, )) = {, 4,, 6} = S DFAedge(S,) = -closure (goto(s, )) = { } = SE This results in the DFA shown elow with strting stte S. S SE S S, d) This DFA cn e further minimize y using the itertive refinement prtitioning yeilding the sequence of prtitions indicted elow. Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment S SE S S S P SE S S P, P, Initil prtitioning on finl sttes Prtitioning on S P SE S S S SE S P,, Prtitioning on finl miniml DFA Lexicl Anlysis Smple Exercises 6 Fll

University of Southern Cliforni Computer Science Deprtment Prolem 6: Minimize the FA shown elow over the lphet {,}. Mke sure to convert it first into DFA nd them pply the itertive refinement lgorithm discussed in clss. strt 4 Solution: The first oservtion is tht there re some sttes for which one of the trnsitions on the lphet symols is missing. This is the cse with sttes "" nd "". As such s need to ugment this FA with "trp-stte" tht is non-ccepting s shown elow in ). The reminder DFA result from the ppliction of the itertive prtitioning lgorithm descried in clss. strt strt P 4, 4 P, ) Augmented DFA strt ) Prtion sed on ccepting nd non-ccepting sttes strt P P 4 P, 4 P, P c) Prtion sed on '' trnsistions d) Prtion sed on '' trnsistions strt strt P 4 P e) Prtion sed on '' trnsistions P, f) Minimized DFA, Lexicl Anlysis Smple Exercises 7 Fll

University of Southern Cliforni Computer Science Deprtment Prolem 7: Consider the DFA elow with strting stte nd ccepting stte :, 4, ) Using the Kleene construction lgorithm derive regulr expression. ) Symplify the expression found in ) ove. Solution: Before engging in very long sequence of conctention nd symolic opertions we mke the oservtion tht stte 4 is trp-stte. As such we cn sfely ignore it since it is not n ccepting stte. Wht this mens is tht we cn use the lgorithm simply with k = nd need to use it to compute R s there re pths from the strt stte to the finl stte vi stte. Expressions for k = R = () R = ( ) R = () R = () R = ( ) Expressions for k = R = R (R )*R R = () R = R (R )*R R = ( ) R = R (R )*R R = () R = R (R )*R R = () R = R (R )*R R = ( ) Expressions for k = R = R (R )*R R = R = () R = R (R )*R R = ( ) ( )* ( ) ( ) = ( ) * R = R (R )*R R = ( ) ( )* () () = *() R = R (R )*R R = () ( )* ( ) () = () * R = R (R )*R R = ( ) ( )* ( ) ( ) = * R = R (R )*R R = ( ) ( )* () = ( ) * () R = R (R )*R R = () ( )* () = () *() Expressions for k = R = R (R )*R R = R = () R = R (R )*R R = ( ) * () (() *())* () * ( )* R = R (R )*R R = *() (() *())* () *() * () R = R (R )*R R = () *() (() *())* () * () * R = R (R )*R R = *() (() *())* () * * R = R (R )*R R = ( ) * () (() *())* () *() ( ) * () R = R (R )*R R = () *() (() *())* () *() () *() Lexicl Anlysis Smple Exercises 8 Fll

University of Southern Cliforni Computer Science Deprtment Clerly these expressions hve lot of redundnt terms. In fct we re only interested in R = ( ) * () (() *())* () * ( )* since the strting stte is stte nd the only ccepting stte is stte. ) To simplify the expression found ove is very hrd, s there re lot of prtil terms in this expression. Insted you follow the edges in the DFA nd try to compose the regulr expressions (in some sense ignoring wht you hve done in the previous step). The simplest wy to look t this prolem is to see tht there is prefix ( ) nd then you return to stte either y (*) or (), so compct regulr expressions would e R = ( )(() ())* much more compct regulr expressions tht R ove. Prolem 8: Consider the lphet Σ = {; }. Define short, possily the shortest, regulr expression tht genertes strings over Σ tht contin exctly one nd t lest one. Solution: The string must either strt with one or n, so we cn use * preffix to ccount for ny possile numer of leding s efore the first. The single must hve t lest one either preceeding it or just efore it. After this single there cn e ny numer of triling s. As possile (lthough not necessrily unique) regulr expression denoting the sought lnguge is R = *( )*. Prolem 9: Given n NFA with -trnsitions how do you derive n equivlent NFA without those - trnsitions? Justify. Solution: For those sttes with -trnsitions merge the edges of the sttes t the end of those trnsitions into the current stte. Iterte until there re no -trnsitions left. You still hve NFA ut there will e t more multiple edges with the lel lel out of given stte. Prolem : Given n NFA with severl ccept sttes, show how to convert it onto NFA with exctly one strt stte nd one ccepting stte. Solution: The initl stte is the sme s in the originl NFA. As to the ccepting sttes just dd - trnsitions from the orignl ccepting sttes to new ccepting stte nd lel tht ccepting stte s the only ccepting stte of the new NFA. Lexicl Anlysis Smple Exercises 9 Fll

University of Southern Cliforni Computer Science Deprtment Prolem : Given the Finite Automton elow with initil stte nd lphet {,} nswer the following questions: 4, () Why is this FA Non-Deterministic Finite Automton (NFA)? () Convert this NFA to DFA using the closure computtion. (c) Minimize the resulting DFA. (d) Wht is the Regulr Expression mtched y this NFA? You re dvised not to use the utomtic Kleene construction or try to look t the input NFA ut rther the correctly minimized DFA. Solution: () This FA is lredy DFA, s the only trnsitions tht re missing ll go to the trp stte. () The suset construction, which in this cse yields the sme originl DFA on the left where we hve noted the suset ech stte stnds for. {} {} {} {4} {},, (c) Using the itertive prtition refinement we hve the following prtitions (elow left) nd the resulting minimized DFA on the right gin with the sttes releled. Notice tht this is specil cse where the refinement lgorithm cnnot refine ny su-sets of sttes given tht the originl DFA ws lredy miniml. {} P {} {} P {4} {} P,, (d) Bsed on the lst DFA the regulr expression identified y this FA must hve exctly two consecutive symol possily preceded y single symol. For instnce, the string is in the lnguge ut the string or re not. The regulr expression cn e denoted y *. Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment Prolem : Drw the smllest DFA tht ccepts the lnguge derived y the regulr expression (** **). Solution: A possile trnsltion guided y the ide of the Thompson construction is shown elow. 6, 4 Using the itertive refinements lgorithm for DFA minimiztion indeed confirms tht this is the smllest possile DFA. The sequence of refinements is shown elow., 4 6, 4 6 4, 6, 4 6 4, 6 4, 6 Prolem : Given the regulr expression RE = ( *)*(*) over the lphet Σ = {,} do the following trnsformtions:. Derive NFA using the Thompson construction cple of identifying the strings generted y this regulr expression.. Convert the NFA otined in ) to DFA. c. Minimize the resulting DFA using the itertive refinement lgorithm discussed in clss. d. Determine in how mny steps is the sequence processed. Justify. Solution: Regrding ) the NFA is given elow. Here we mde some very simple simplifiction tht two or more sequences of -trnsitions cn e converted to single -trnsitions. This sustntilly reduces the numer of sttes in the NFA nd thius fcilittes the construction of the equivlent DFA. Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment 4 6 7 8 9 4 Applying the suset construction we hve the following sttes S = -closure () = {,, 4, 7, 8,,,,, 4, } this is finl stte ecuse of S = DFAedge(S,) = -closure (goto(s, )) = {} = DFAedge(S,) = -closure (goto(s, )) = {,,, 4, 7, 8, 9,,, 4, } finl S = DFAedge(S,) = -closure (goto(s, )) = {} S4 = DFAedge(S,) = -closure (goto(s, )) = {,,, 4, 6, 7, 8,,, 4, } finl S = DFAedge(,) = -closure (goto(, )) = {} = S S6 = DFAedge(,) = -closure (goto(, )) = {,,, 4, 7, 8, 9,,,, 4, } finl S7 = DFAedge(S,) = -closure (goto(s, )) = {} = S S8 = DFAedge(S,) = -closure (goto(s, )) = {} = S S9 = DFAedge(S4,) = -closure (goto(s4, )) = {} = S S = DFAedge(S4,) = -closure (goto(s4, )) = {,,, 4,, 7, 8, 8,,,, 4, } = S6 S = DFAedge(S6,) = -closure (goto(s6, )) = {,,, 4, } finl = DFAedge(S6,) = -closure (goto(s6, )) = {,,, 4, 7, 8, 9,,, 4, } = S = DFAedge(S,) = -closure (goto(s, )) = {} = S S4 = DFAedge(S,) = -closure (goto(s, )) = {,,, 4, 6, 7, 8,,, 4, }= S4 So there re totl of 7 sttes, S, S,, S, S4, S6, S nd the corresponding DFA is s shown y the tle elow (old sttes re finl sttes) nd then in the figure elow the tle. Next Stte Current Stte S S S S S4 S S6 S S S S4 S S6 S6 S S S S4, 4 6 Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment, P 4 6 initil refinement on cceptnce criteri 6, 4 refinement of on P P, 4 6 P refinement of on P, P 4 6 refinement of P on P P P6 P P, 4 6 refinement of on P P6 P P, 4 6 finl refinement with 6 sttes P Prolem 4: Given the DFA elow descrie in English the set of strings ccepted y it., Solution: Any string over the lphet tht contins ny numer of consecutive s, including none with preffix of zero or more consecutive s. Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment Prolem : Given regulr lnguge L. i.e., lnguge descried y regulr expression, prove tht the reverse of L is lso regulr lnguge (Note: the reverse of lnguge L is L R where for ech word w in L, w R is in L R. Given word w over the given lphet, w R is constructed y spelling w ckwrds). Solution: If L is regulr lnguge then there exists DFA M tht recognizes it. Now given M we cn construct M tht recognizes the reverse of L with respect to the input lphet. We now descrie how to construct M. M is replic of M ut reversing ll the edges. The finl stte of M is the stte tht used to e the strt stte of M. The strt stte of M is new stte with -trnsitions to the sttes in M tht used to e the finl sttes of M. Now ecuse M might hve multiple finl sttes M is y construction NFA. Given the equivlence of NFA nd regulr expressions we hve shown tht if L is regulr so is L R. Prolem 6: Drw the DFA cple of recognizing the set of ll strings eginning with which interpreted s the inry representtion of n integer (ssuming the lst digit to e processed is the lest significnt) is congruent to zero modulo i.e., the numeric vlue of this inry representtion is multiple of. Solution: The hrd prt out this prolem is tht you need to keep trck with the lredy oserved its wht the reminder of the division y is. Given tht you hve reminder you would need no more tht sttes, one for ech of the reminder vlues through eing the stte tht represents reminder of zero the ccepting stte, in this cse stte S. The DFA elow ccomplishes this. You cn verify this DFA y trying the numer in inry or in inry. Notice tht in the lst stte S ny dditionl mens you re shifting the its y one it, i.e., multiplying y, hence stying in the sme stte. S S S Prolem 7: Consider the lphet Σ = {,}.. Consider the Non-Deterministic Finite Automton (NFA) depicted elow. Why is this utomton non-deterministic? Explin the vrious source on indetermincy.. Do the sentences w = nd w = elong to the lnguge recognized y this FA? Justify. c. Convert the NFA in prt ) to DFA using the suset construction. Show the mpping etween the sttes in the NFA nd the resulting DFA. d. Minimize the DFA using the itertive refinement lgorithm descried in clss. Show your intermedite prtition results nd doule check the DFA using the sentences w nd w. Lexicl Anlysis Smple Exercises 4 Fll

University of Southern Cliforni Computer Science Deprtment 4 6 strt Solution: ) This is indeed NFA for two resons. First, it includes -trnsitions. Second, in stte there re two trnsistions on the sme terminl or lphet symol, "". ) Regrding the word there is pth from stte to the ccepting stte 6, nmely:,4,,6. Regrding the word the utomton will never e le to rech the stte 6 s in order to spell out the chrcter will necessrily e in stte nd to rech tht stte we cnnot hve two consecutive "" chrcters we we would e either in stte or stte 4. c) Using the suset construction we rrive t the following susets nd trnsitions. S = -closure ({}) = {,, 4} this is not finl stte. = DFAedge(S,) = -closure (goto(s, )) = {,,, 6} finl stte S = DFAedge(S,) = -closure (goto(s, )) = {} S9 = DFAedge(,) = -closure (goto(, )) = {,, 6} finl stte S4 = DFAedge(,) = -closure (goto(, )) = {,, 6} finl stte. S = DFAedge(S,) = -closure (goto(s, )) = {,, 6} finl stte. DFAedge(S,) = -closure (goto(s, )) = {} = S DFAedge(S4,) = -closure (goto(s4, )) = {,, 6} = S finl stte. DFAedge(S4,) = -closure (goto(s4, )) = {,, 6} = S4 finl stte. S6 = DFAedge(S,) = -closure (goto(s, )) = {, 6} finl stte. S7 = DFAedge(S,) = -closure (goto(s, )) = {, 6} finl stte. DFAedge(S6,) = -closure (goto(s6, )) = {, 6} = S6 finl stte. S8 = DFAedge(S6,) = -closure (goto(s6, )) = { } DFAedge(S7,) = -closure (goto(s7, )) = { } = S8 DFAedge(S7,) = -closure (goto(s7, )) = {, 6} = S7 finl stte. DFAedge(S8,) = -closure (goto(s8, )) = { } = S8 DFAedge(S8,) = -closure (goto(s8, )) = { } = S8 DFAedge(S9,) = -closure (goto(s9, )) = {,, 6} = S9 DFAedge(S9,) = -closure (goto(s9, )) = {} = S This results in the DFA shown elow with strting stte S. strt S S9 S S4 S S7 S6 S8, Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment d) We cn try to minimize this DFA using the itertive refinement lgorithm descried in clss. The figure elow depicts possile sequence of refinements. For ech step we indicte the criteri used to discriminte etween sttes in the previous prtition. As cn e seen, the lgorithm leds to no refinement, so this is prticulr cse where the su-set lgorithm tht help us derive the DFA from its originl NFA does yield miniml DFA. P P P P strt S S S9 S4 S S7 S6 S8, strt S S9 S S4 S S7 S6, S8 strt S S S4 S9 S S7 S6, S8 P P P () Prtition etween ccepting nd non-ccepting sttes () Discrimintion sed on "" trnsitions (c) Discrimintion sed on "" trnsitions strt P S P S P9 S9 S4 S S7 S6 P P S8, strt P S P S S P6 S4 P9 S9 S7 S6 P P S8, strt P S P7 P S S P6 S4 P9 S9 S7 S6 P P S8, (d) Discrimintion sed on "" trnsitions (e) Discrimintion sed on "" trnsitions (f) Discriminion sed on "" trnsitions Lexicl Anlysis Smple Exercises 6 Fll

University of Southern Cliforni Computer Science Deprtment Prolem 8: Consider the DFA elow with strting stte nd ccepting stte : strt ) Descrie in English the set of strings ccepted y this DFA. ) Using the Kleene construction lgorithm derive the regulr expression recognized y this utomton symplifying it s much s possile. Solution: ) This utomton recognizes ll non-null strings over the {,} lphet tht end with "". ) The derivtions re shown elow with the ovious symplifiction. Expressions for k = R = ( ) R = () R = () R = ( ) Expressions for k = R = R (R )*R R = ( ). ( )*. ( ) ( ) = * R = R (R )*R R = ( ). ( )*. () () = * * R = R (R )*R R = (). ( )*. ( ) () = + + R = R (R )*R R = (). ( )*. ( ) ( ) = (* ) Expressions for k = R = R (R )*R R = ( * * ). (* )*. ( + + ) ( * ) R = R (R )*R R = ( * * ). (* )*. ( + + ) ( * * ) R = R (R )*R R = ( * * ). (* )*. ( + + ) ( + + ) R = R (R )*R R = ( * * ). (* )*. ( + + ) (* ) L = R = ( * * ). (* )*. ( + + ) ( * * ) As cn e seen the simplifiction of ny of these regulr expressions eyond the expresions for k= is firly complicted. This method, lthough correct y design leds to regulr expressions tht re fr from eing miniml or even the most compct representtion of the regulr lnguge the DFA recognizes. Lexicl Anlysis Smple Exercises 7 Fll

University of Southern Cliforni Computer Science Deprtment Prolem 9: For the regulr expression depicted elow nswer the following questions: RE = ( - +)?. (e E). (-9)+ ) Using the Thompson construction (or simplifiction thereof) derive NFA tht recognizes the strings specified y this RE. ) Convert the NFA in ) to n equivlent DFA using the suset construction. c) Mke sure the DFA M found in ) ove is miniml (you do not need to show tht it is in fct miniml) nd construct the DFA M tht ccepts the complement of the regulr lnguge the first DFA ccepts. Show tht M does ccept the word w = "e" which is not ccepted y the originl DFA. Solution: ) The figure elow depicts the NFA with -trnsitions nd where we hve simplified the trnsition on numeric digits. 'e' '' strt '-' 4 6 'E' 7 '9' 8 9 '+' ) We use the suset construction y trcing for every set of sttes the other possile sttes the NFA could e in if it were to trverse -edges only. The list elow depicts the results of this construction where we lso descrie the computtion of edge DFA edge using the function DFA_edge nd then computing the e-closure of the resulting intermedite set of sttes. -closure({}) DFA_edge(S,'-') DFA_edge(S,'+') DFA_edge(S,'e') DFA_edge(S,'E') DFA_edge(S,'<digit>') DFA_edge(S,'-') DFA_edge(S,'+') DFA_edge(S,'e') DFA_edge(S,'E') DFA_edge(S,'<digit>') = {,,,, 4} = S = -closure({}) = {,, 4} = S = -closure({}) = {,, 4} = S = -closure({}) = {, 6, 7} = = -closure({}) = {, 6, 7} = = -closure( {} ) = SE; error stte = -closure( {} ) = SE; error stte = -closure( {} ) = SE; error stte = -closure({}) = {, 6, 7 } = = -closure({}) = {, 6,7 } = = -closure( {} ) = SE; error stte DFA_edge(,'-') = -closure( {} ) = SE; error stte DFA_edge(,'+') = -closure( {} ) = SE; error stte DFA_edge(,'e') = -closure( {} ) = SE; error stte DFA_edge(,'E') = -closure( {} ) = SE; error stte DFA_edge(,'<digit>') = -closure( {8} ) = { 6, 7, 8, 9, } = S; DFA_edge(S,'-') = -closure( {} ) = SE; error stte DFA_edge(S,'+') = -closure( {} ) = SE; error stte DFA_edge(S,'e') = -closure( {} ) = SE; error stte DFA_edge(S,'E') = -closure( {} ) = SE; error stte DFA_edge(S,'<digit>') = -closure( {8} ) = { 6, 7, 8, 9, } = S; The figure elow depicts the DFA resulting from the ppliction of the suset construction. Notice tht this is fully specified DFA where ech stte includes trnsition for ll the chrcters in the considered lphet. Lexicl Anlysis Smple Exercises 8 Fll

University of Southern Cliforni Computer Science Deprtment strt '-' '+' S 'e' 'E' '' '9' S '' '9' '' '9' 'e' 'E' '-' '+' 'e' 'E' '' S 'e' 'E' '-' '+' SE '9' 'e' 'E' '-' '+' '' '9' c) Let M e the DFA found in ). This DGA is in fct miniml. Informl proof: All sttes re required. S is the strt stte. S cptures the fct tht we hve oserved sign.. Stte cptures to fct we need to oserve t lest one digit. All the digits re then cptured y stte. Stte SE is the trp error stte. Let M e this first DFA. We construct M y mking ll ccepting sttes of M non-ccepting sttes nd ll non-ccepting stte s of M s ccepting sttes of M. The stte strt is unchnged. This is DFA s we only chnge the stte of non-ccepting sttes. The strt stte is still singleton nd ll trnsitions re clerly identified in the newly creted DFA s shown elow. '-' '+' S S 'e' 'E' 'e' 'E' '' S '' '9' strt '' '9' '' '9' '' '9' 'e' 'E' '-' '+' 'e' 'E' '-' '+' SE '9' 'e' 'E' '-' '+' In this newly creted DFA the input string w = "e" tkes the DFA to stte SE which is n ccepting stte n this "complement" mchine. Lexicl Anlysis Smple Exercises 9 Fll

University of Southern Cliforni Computer Science Deprtment Prolem Consider the lphet Σ = {,, c}.. Consider the Non-Deterministic Finite Automton (NFA) depicted elow. Why is this utomton non-deterministic? Explin the vrious source on indetermincy.. Do the sentences w = nd w = elong to the lnguge generted y this FA? Justify. c. Convert the NFA in prt ) to DFA using the suset construction. Show the mpping etween the sttes in the NFA nd the resulting DFA. d. By inspection cn you derive regulr expression (RE) this NFA detects? c c, c,c 4,, c strt 6, c Solution: ) This FA is non-deterministic s for t lest one of its sttes there re two trnsitions on the sme lel to two distinct sttes. This is the cse with stte where on '' the FA goes to stte nd 6. Similrly, sttes nd exhiit the sme ehvior. ) The sentence w = elongs to the lnguge ccepted y this FA s there is pth through sttes, nd tht speels out this ord nd stte is n ccepting stte. As to the word w = ny word tht egins with '' leds to stte 4 which is trp nd non-ccepting stte. As such w does not elong to the lnguge recognized y this FA. c) A simplified su-set construction is s shown elow: S = -closure ({}) = {} this is not finl stte. DFAedge(S, ) = -closure (goto(s, )) = {,6} = DFAedge(S, ) = -closure (goto(s, )) = {4} DFAedge(S, c) = -closure (goto(s, c)) = {4} DFAedge(, ) = -closure (goto(, )) = {4} = S = S = S DFAedge(, ) = -closure (goto(, )) = {,4} = S4 DFAedge(, c) = -closure (goto(, c)) = {4,} = S DFAedge(S, ) = -closure (goto(s, )) = {4} DFAedge(S, ) = -closure (goto(s, )) = {4} DFAedge(S, c) = -closure (goto(s, c)) = {4} = S = S = S DFAedge(S4, ) = -closure (goto(s4, )) = {4} = S DFAedge(S4, ) = -closure (goto(s4, )) = {4} = S DFAedge(S4, c) = -closure (goto(s4, c)) = {,4} = S4 DFAedge(S, ) = -closure (goto(s, )) = {4} = S DFAedge(S, ) = -closure (goto(s, )) = {4,} = S DFAedge(S, c) = -closure (goto(s, c)) = {4} = S Lexicl Anlysis Smple Exercises Fll

University of Southern Cliforni Computer Science Deprtment In this construction we hve simplified the resulting DFA y mking the trnsition etween stte {,6} on '' direclty move to stte 4 which is trp or error stte. A more mechnicl procedure would hve seen the cretion of n dditionl trp stte tht is equivlent to stte 4. c,4,,6 c,c 4,, c strt 4,,c d) By inspection of the two pths from stte to either stte or stte we cn immeditely see tht the lnguge ccepted y this FA cn e descried y the regulr expressions RE = c* c*. Lexicl Anlysis Smple Exercises Fll