Regular Expressions and Automata using Miranda

Similar documents
Fig.25: the Role of LEX

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

Dr. D.M. Akbar Hussain

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

Definition of Regular Expression

COMP 423 lecture 11 Jan. 28, 2008

Topic 2: Lexing and Flexing

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

Lexical Analysis: Constructing a Scanner from Regular Expressions

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

2014 Haskell January Test Regular Expressions and Finite Automata

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

CSCE 531, Spring 2017, Midterm Exam Answer Key

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Reducing a DFA to a Minimal DFA

CS201 Discussion 10 DRAWTREE + TRIES

CS 241 Week 4 Tutorial Solutions

2 Computing all Intersections of a Set of Segments Line Segment Intersection

What are suffix trees?

Suffix trees, suffix arrays, BWT

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

Lexical Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

TO REGULAR EXPRESSIONS

Section 3.1: Sequences and Series

Scanner Termination. Multi Character Lookahead

Slides for Data Mining by I. H. Witten and E. Frank

Compilers Spring 2013 PRACTICE Midterm Exam

Information Retrieval and Organisation

Lexical analysis, scanners. Construction of a scanner

CS 430 Spring Mike Lam, Professor. Parsing

ECE 468/573 Midterm 1 September 28, 2012

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

Algorithm Design (5) Text Search

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

ASTs, Regex, Parsing, and Pretty Printing

Compiler Construction D7011E

Theory of Computation CSE 105

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

CSE 401 Midterm Exam 11/5/10 Sample Solution

CMPSC 470: Compiler Construction

A Tautology Checker loosely related to Stålmarck s Algorithm by Martin Richards

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

Assignment 4. Due 09/18/17

MATH 25 CLASS 5 NOTES, SEP

CS481: Bioinformatics Algorithms

Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming

MTH 146 Conics Supplement

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

COMBINATORIAL PATTERN MATCHING

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

Intermediate Information Structures

Typing with Weird Keyboards Notes

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

EECS150 - Digital Design Lecture 23 - High-level Design and Optimization 3, Parallelism and Pipelining

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016

Principles of Programming Languages

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

Context-Free Grammars

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night

CS 321 Programming Languages and Compilers. Bottom Up Parsing

Context-Free Grammars

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

Midterm I Solutions CS164, Spring 2006

1.1. Interval Notation and Set Notation Essential Question When is it convenient to use set-builder notation to represent a set of numbers?

Before We Begin. Introduction to Spatial Domain Filtering. Introduction to Digital Image Processing. Overview (1): Administrative Details (1):

COS 333: Advanced Programming Techniques

Outline. Introduction Suffix Trees (ST) Building STs in linear time: Ukkonen s algorithm Applications of ST

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

10.5 Graphing Quadratic Functions

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Notes for Graph Theory

CMSC 331 First Midterm Exam

Graphs with at most two trees in a forest building process

Chapter44. Polygons and solids. Contents: A Polygons B Triangles C Quadrilaterals D Solids E Constructing solids

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup

1 Quad-Edge Construction Operators

An Efficient Divide and Conquer Algorithm for Exact Hazard Free Logic Minimization

The Greedy Method. The Greedy Method

INTRODUCTION TO SIMPLICIAL COMPLEXES

From Dependencies to Evaluation Strategies

Suffix Tries. Slides adapted from the course by Ben Langmead

A dual of the rectangle-segmentation problem for binary matrices

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits

Position Heaps: A Simple and Dynamic Text Indexing Data Structure

9 Graph Cutting Procedures

Functor (1A) Young Won Lim 10/5/17

Misrepresentation of Preferences

Lexical Analysis and Lexical Analyzer Generators

Transcription:

Regulr Expressions nd Automt using Mirnd Simon Thompson Computing Lortory Univerisity of Kent t Cnterury My 1995 Contents 1 Introduction ::::::::::::::::::::::::::::::::: 1 2 Regulr Expressions ::::::::::::::::::::::::::::: 2 Mtching regulr expressions :::::::::::::::::::::::: 4 4 Sets :::::::::::::::::::::::::::::::::::::: 6 5 Non-deterministic Finite Automt ::::::::::::::::::::: 11 6 Simulting n NFA :::::::::::::::::::::::::::::: 1 7 Implementing n exmple :::::::::::::::::::::::::: 16 8 Building NFAs from regulr expressions ::::::::::::::::::: 17 9 Deterministic mchines ::::::::::::::::::::::::::: 20 10 Trnsforming NFAs to DFAs ::::::::::::::::::::::::: 2 11 Minimising DFA :::::::::::::::::::::::::::::: 25 12 Regulr definitions :::::::::::::::::::::::::::::: 27 1. Introduction In these notes Mirnd is used s vehicle to introduce regulr expressions, pttern mtching, nd their implementtions y mens of non-deterministic nd deterministic c Simon Thompson, 1995 1

2 utomt. As prt of the mteril, we give n implementtion of the ides, contined in set of files. References to this mteril re scttered through the text. The files cn e otined y following the instructions in http://www.ukc.c.uk/computer science/mirnd crft/regexp.html This mteril is sed on the tretment of the suject in [Aho et. l.], ut provides full implementtions rther thn their pseudo-code versions of the lgorithms. The mteril gives n illustrtion of mny of the fetures of Mirnd, including polymorphism (the sttes of n NFA cn e represented y ojects of ny type); modulristion (the system is split cross numer of modules); higher-order functions (used in finding limits of processes, for exmple) nd other fetures. A tutoril introduction to Mirnd cn e found in [Thompson]. The pper egins with definitions of regulr expressions, nd how strings re mtched to them; this lso gives our first Mirnd tretment lso. After descriing the strct dt type of sets we define non-deterministic finite utomt, nd their implementtion in Mirnd. We then show how to uild n NFA corresponding to ech regulr expression, nd how such mchine cn e optimised, first y trnsforming it into deterministic mchine, nd then y minimising the stte spce of the DFA. We conclude with discussion of regulr definitions, nd show how recognisers for strings mtching regulr definitions cn e uilt. 2. Regulr Expressions Regulr expressions re ptterns which cn e used to descrie sets of strings of chrcters of vrious kinds, such s the identifiers of progrmming lnguge strings of lphnumeric chrcters which egin with n lphetic chrcter; the numers integer or rel given in progrmming lnguge; nd so on. There re five sorts of pttern, or regulr expression: " This is the Greek chrcter epsilon, which mtches the empty string. x xis ny chrcter. This mtches the chrcter itself. (r 1 r 2 ) r 1 nd r 2 re regulr expressions. (r 1 r 2 ) r 1 nd r 2 re regulr expressions. (r)* r is regulr expression.

Exmples of regulr expressions include ( ()), (() (" ()*)) nd hello. In order to give more redle version of these, it is ssumed tht * inds more tightly thn juxtposition (i.e. (r 1 r 2 )), nd tht juxtposition inds more tightly thn (r 1 r 2 ). This mens tht r 1 r 2 * will men (r 1 (r 2 )*), not ((r 1 r 2 ))*, nd tht r 1 r 2 r will men r 1 (r 2 r ), not (r 1 r 2 )r. A Mirnd lgeric type representing regulr expressions is given y reg ::= Epsilon Literl chr Or reg reg Then reg reg Str reg This definition nd those which follow cn e found in the file regexp.m. Mirnd representtions of ( ()) nd (() (" ()*)) re The Or (Literl ) (Then (Literl ) (Literl )) Or (Then (Literl ) (Literl )) (Or Epsilon (Str (Literl ))) respectively. In order to shorten these definitions we will usully define constnt literls such s = Literl = Literl so tht the expressions ove ecome Or (Then ) Or (Then ) (Or Epsilon (Str )) If we use the infix forms of Or nd Then, $Or nd $Then, they red $Or ( $Then ) ( $Then ) $Or (Epsilon $Or (Str )) Functions over the type of regulr expressions re defined y recursion over the structure of the expression. Exmples include

4 literls :: reg -> [chr] literls Epsilon = [] literls (Literl ch) = [ch] literls (Or r1 r2) = literls r1 ++ literls r2 literls (Then r1 r2) = literls r1 ++ literls r2 literls (Str r) = literls r which prints list of the literls ppering in regulr expression, nd printre :: reg -> [chr] printre Epsilon = "@" printre (Literl ch) = [ch] printre (Or r1 r2) = "(" ++ printre r1 ++ " " ++ printre r2 ++ ")" printre (Then r1 r2) = "(" ++ printre r1 ++ printre r2 ++ ")" printre (Str r) = "(" ++ printre r ++")*" which gives printle form of regulr expression. Note tht @ is used to represent epsilon in ASCII. Exercises 1. Write more redle form of the expression (((( ) c)(()* ()*))(c d)). 2. Wht is the unrevited form of ((x?)*(y?)*)+?. Mtching regulr expressions Regulr expressions re ptterns. expression. We should sk which strings mtch ech regulr

5 " The empty string mtches epsilon. x The chrcter x mtches the pttern x, for ny chrcter x. (r 1 r 2 ) The string st will mtch (r 1 r 2 ) if st mtches either r 1 or r 2 (or oth). (r 1 r 2 ) The string st will mtch (r 1 r 2 ) if st cn e split into two sustrings st 1 nd st 2, st = st 1 ++st 2, so tht st 1 mtches r 1 nd st 2 mtches r 2. (r)* The string st will mtch (r)* if st cn e split into zero or more sustrings, st = st 1 ++st 2 ++...++st n, ech of which mtches r. The zero cse implies tht the empty string will mtch (r)* for ny regulr expression r. This cn e implemented in Mirnd, in the file mtches.m. The first three cses re simple trnslitertion of the definitions ove. mtches :: reg -> string -> ool mtches Epsilon st = (st = "") mtches (Literl ch) st = (st = [ch]) mtches (Or r1 r2) st = mtches r1 st \/ mtches r2 st In the cse of juxtposition, we need n uxiliry function which gives the list contining ll the possile wys of splitting up list. splits :: [*] -> [ ([*],[*]) ] splits st = [ (tke n st,drop n st) n <- [0..#st] ] For exmple, splits [2,] is [([],[2,]),([2],[]),([2,],[])]. A string will mtch (Then r1 r2) if t lest one of the splits gives strings which mtch r1 nd r2. mtches (Then r1 r2) st = or [mtches r1 s1 & mtches r2 s2 (s1,s2)<-splits st] The finl cse is tht of Str. We cn explin * s either " or s followed y *.

6 We cn use this to implement the check for the mtch, ut it is prolemtic when cn e mtched y ". When this hppens, the mtch is tested recursively on the sme string, giving n infinite loop. This is voided y disllowing n epsilon mtch on the first mtch on hs to e non-trivil. mtches (Str r) st = mtches Epsilon st \/ or [ mtches r s1 & mtches (Str r) s2 (s1,s2) <- frontsplits st ] frontsplits is defined like splits ut so s to exclude the split ([],st). Exercises. Argue tht the string " mtches ( (c)*)* nd tht the string mtches (( )*()*). 4. Why does the string not mtch (( )*()*)? 5. Give informl descriptions of the sets of strings mtching the following regulr expressions. ( )*( )*( ) ( )*( )( ) "?()+? 6. Give regulr expressions descriing the following sets of strings All strings of s nd s contining t most two s. All strings of s nd s contining exctly two s. All strings of s nd s of length t most three. All strings of s nd s which contin no repeted djcent chrcters, tht is no sustring of the form or. 4. Sets A set is collection of elements of prticulr type, which is oth like nd unlike list. Lists re fmilir from Mirnd, nd exmples include

7 [Joe,Sue,Ben] [Ben,Sue,Joe] [Joe,Sue,Sue,Ben] [Joe,Sue,Ben,Sue] Ech of these lists is different not only do the elements of list mtter, ut lso the order in which they occur, nd their multiplicity (the numer of times ech element occurs). In mny situtions, order nd multiplicity re irrelevnt. If we wnt to tlk out the collection of people coming to our irthdy prty, we just wnt the nmes we cnnot invite someone more thn once, so multiplicity is not importnt; the order we might list them in is lso of no interest. In other words, ll we wnt to know is the set of people coming. In the exmple ove, this is the set contining Joe, Sue nd Ben. Sets cn e implemented in numer of wys in Mirnd, nd the precise form is not importnt for the user. It is sensile to declre the type s n strct dt type, or stype, illustrted in Figure 1. The stype nd its implementtion re found in the file sets.m. The implementtion we hve given represents set s n ordered list of elements without repetitions. The individul functions re descried nd implemented s follows. sing is the singleton set, consisting of the single element sing = [] union,inter,diff give the union, intersection nd difference of two sets. The union consists of the elements occurring in either set (or oth), the intersection of those elements in oth sets nd the difference of those elements in the first ut not the second set; their definitions re given in Figure 2. The empty set is the empty list empty = [] memset x tests whether is memer of the set x. Note tht this is n optimistion of the function memer over lists; since the list is ordered, we need look no further once we hve found n element greter thn the one we seek. memset [] = Flse memset (:x) = memset x, if < = True, if = = Flse, otherwise

8 stype set * with sing :: * -> set * union,inter,diff :: set * -> set * -> set * empty :: set * memset :: set * -> * -> ool suset :: set * -> set * -> ool eqset :: set * -> set * -> ool mpset :: (* -> **) -> set * -> set ** filterset,seprte :: (*->ool) -> set * -> set * foldset :: (* -> * -> *) -> * -> set * -> * mkeset :: [*] -> set * showset :: (*->[chr]) -> set * -> [chr] crd :: set * -> num fltten :: set * -> [*] setlimit :: (set * -> set *) -> set * -> set * Figure 1: The set stype union [] y = y union x [] = x union (:x) (:y) = : union x (:y) inter [] y = [] inter x [] = [] inter (:x) (:y) = inter x (:y), if < = : union x y, if = = : union (:x) y, otherwise, if < = : inter x y, if = = inter (:x) y, otherwise diff [] y = [] diff x [] = x diff (:x) (:y) = : diff x (:y), if < = diff x y, if = = diff (:x) y, otherwise Figure 2: Set opertions

9 suset x y tests whether x is suset of y; tht is whether every element of x is n element of y. suset [] y = True suset x [] = Flse suset (:x) (:y) = Flse, if < = suset x y, if = = suset (:x) y, if > eqset x y tests whether two sets re equl. eqset = (=) mpset, filterset nd foldset ehve like mp, filter nd foldr except tht they operte over sets. seprte is synonym for filterset. mpset f = mkeset. (mp f) filterset = filter seprte = filterset foldset = foldr mkeset turns list into set mkeset = remdups. sort where remdups [] = [] remdups [] = [] remdups (:x) = : remdups x, if < = remdups x, otherwise where = hd x showset f gives printle version of set, one item per line, using the function f to give printle version of ech element. showset f = conct. (mp ((++"\n"). f))

10 crd x gives the numer of elements in x crd = (#) fltten x turns set x into n ordered list of the elements of the set fltten = id setlimit f x gives the limit of the sequence x, f x, f (f x), f (f (f x)),... tht is the first element in the sequence whose successor is equl, s set, to the element itself. In other words, keep pplying f until fixed point or limit is reched. setlimit f x = x, if eqset x next = setlimit f next, otherwise where next = f x Exercises 7. How is the function powerset :: set * -> set (set *) which returns the set of ll susets of set defined? 8. How would you define the functions setunion :: set (set *) -> set * setinter :: set (set *) -> set * which return the union nd intersection of set of sets? 9. Cn infinite sets (of numers, for instnce) e dequtely represented y ordered lists? Cn you tell if two infinite lists re equl, for instnce? 10. The stype set * cn e represented in numer of different wys. Alterntives include: ritrry lists (rther thn ordered lists without repetitions), nd oolen vlued functions, tht is elements of the type * -> ool. Give implementtions of the type using these two representtions.

11 5. Non-deterministic Finite Automt A Non-deterministic Finite Automton or NFA is simple mchine which cn e used to recognise regulr expressions. It consists of four components A finite set of sttes, S. A finite set of moves. A strt stte (in S). A set of terminl or finl sttes ( suset of S). In Mirnd (see file nf types.m) this is written nf * ::= NFA (set *) (set (move *)) * (set *) This hs een represented y n lgeric type rther thn 4-tuple simply for redility. The type of sttes cn e different in different pplictions, nd indeed in the following we use oth numers nd sets of numers s sttes. A move is etween two sttes, nd is either given y chrcter, or n ". move * ::= Move * chr * Emove * * The first exmple of n NFA, clled M, follows. 0 1 2 The sttes re 0,1,2,, with the strt stte 0 indicted y n incoming rrow, nd the finl sttes indicted y shded circles. In this cse there is single finl stte,. The moves re indicted y the rrows, mrked with chrcters nd in this cse. From

12 stte 0 there re two possile moves on symol, to1 nd to remin t 0. This is one source of the non-determinism in the mchine. The Mirnd representtion of the mchine is NFA (mkeset [0..]) (mkeset [ Move 0 0, Move 0 1, Move 0 0, Move 1 2, Move 2 ]) 0 (sing ) A second exmple, clled N, is illustrted elow. 1 2 0 4 5 The Mirnd representtion of this mchine is NFA (mkeset [0..5]) (mkeset [ Move 0 1, Move 1 2, Move 0, Move 4, Emove 4, Move 4 5 ]) 0 (mkeset [2,5]) This mchine contins two kinds of non-determinism. The first is t stte 0, from which

1 it is possile to move to either 1 or on reding. The second occurs t stte : itis possile to move invisily from stte to stte 4 on the epsilon move, Emove 4. The Mirnd code for these mchines together with function print nf to print n nf whose sttes re numered cn e found in the file nf misc.m. How do these mchines recognise strings? A move cn e mde from one stte s to nother t either if the mchine contins Emove s t or if the next symol to e red is, sy, nd the mchine contins move Move s t. A string will e ccepted y mchine if there is sequence of moves through sttes of the mchine strting t the strt stte nd terminting t one of the terminl sttes this is clled n ccepting pth. For instnce, the pth 0 ;! 1 ;! 2 ;! is n ccepting pth through M for the string. This mens tht the mchine M ccepts this string. Note tht other pths through the mchine re possile for this string, n exmple eing 0 ;! 0 ;! 0 ;! 0 All tht is needed for the mchine to ccept is one ccepting pth; it does not ffect cceptnce if there re other non-ccepting (or indeed ccepting) pths. More thn one ccepting pth cn exist. Mchine N ccepts the string y oth 0 ;! 1 ;! 2 nd 0 ;! ;! " 4 ;! 5 A mchine will reject string only when there is no ccepting pth. Mchine N rejects the string, since the two pths through the mchine lelled y fil to terminte in finl stte: 0 ;! 1 0 ;! Mchine N rejects the string since there is no pth through the mchine lelled y : fter reding the mchine cn e in stte 1, or 4, from none of these cn n move e mde. 6. Simulting n NFA As ws explined in the lst section, string st is ccepted y mchine M when there is t lest one ccepting pth lelled y st through M, nd is rejected y M when no such pth exists.

14 The key to implementtion is to explore simultneously ll possile pths through the mchine lelled y prticulr string. Tke s n informl exmple the string nd the mchine N. After reding no input, the mchine cn only e in stte 0. On reding n there re moves to sttes 1 nd ; however this is not the whole story. From stte it is possile to mke n "-move to stte 4, so fter reding the mchine cn e in ny of the sttes f1,,4g. On reding, we hve to look for ll the possile moves from ech of the sttes f1,,4g. From 1 we cn move to 2, from to 4 nd from 4 to 5 no"-moves re possile from the sttes f2,4,5g, nd so the sttes ccessile fter reding the string re f2,4,5g. Is this string to e ccepted y N? We ccept it exctly if the set contins finl stte it contins oth 2 nd 5, so it is ccepted. Note tht the sttes ccessile fter reding re f1,,4g; this set contins no finl stte, nd so the mchine N rejects the string. There is generl pttern to this process, which consists of repetition of Tke set of sttes, such s f1,,4g, nd find the set of sttes ccessile y move on prticulr symol, e.g.. In this cse it is the set f2,4,5g. This is clled onemove in nf li.m. Tke set of sttes, like f1,g, nd find the set of sttes ccessile from the sttes y zero or more "-moves. In this exmple, it is the set f1,,4g. This is the "-closure of the originl set, nd is clled closure in nf li.m. The functions onemove nd closure re composed in the function onetrns, nd this function is iterted long the string y the trns function of implement nf.m. Implementtion in Mirnd We discuss the development of the function trns :: nf * -> string -> set * top-down. Itertion long string is given y foldl foldl :: (set * -> chr -> set *) -> set * -> string -> set * foldl f r [] = r foldl f r (c:cs) = foldl f (f r c) cs

15 The first rgument, f, is the step function, tking set nd chrcter to the sttes ccessile from the set on the chrcter. The second rgument, r, is the strting stte, nd the finl rgument is the string long which to iterte. How does the function operte? If given n empty string, the strt stte is the result. If given string (c:cs), the function is clled gin, with the til of the string, cs, nd with new strting stte, (f r c), which is the result of pplying the step function to the strting set of sttes nd the first chrcter of the string. Now to develop trns. trns mch str = foldl step strtset str where step set ch = onetrns mch ch set strtset = closure mch (sing (strtstte mch)) step is derived from onetrns simply y suppling its mchine rgument mch, similrly strtset is derived from the mchine mch, using the functions closure nd strtstte. All these functions re defined in nf li.m. We discuss their definitions now. onetrns :: nf * -> chr -> set * -> set * onetrns mch c x = closure mch (onemove mch c x) Next, we exmine onemove, onemove :: nf * -> chr -> set * -> set * onemove (NFA sttes moves strt term) c x = mkeset [ s t <- fltten x ; Move z d s <- fltten moves ; z=t ; c=d ] The essentil ide here is to run through the elements t of the set x nd the set of moves, moves looking for ll c-moves originting t t. For ech of these, the result of the move, s, goes into the resulting set. The definition uses list comprehensions, so it is necessry first to fltten the sets

16 x nd moves into lists, nd then to convert the list comprehension into set y mens of mkeset. closure :: nf * -> set * -> set * closure (NFA sttes moves strt term) = setlimit dd where dd stteset = union stteset (mkeset ccessile) where ccessile = [ s x <- fltten stteset ; Emove y s <- fltten moves ; y=x ] The essence of closure is to tke the limit of the function which dds to set of sttes ll those sttes which re ccessile y single "-move; in the limit we get set to which no further sttes cn e dded y "-trnsitions. Adding the sttes got y single "-moves is ccomplished y the function dd nd the uxiliry definition ccessile which resemles the construction of onemove. 7. Implementing n exmple The mchine P is illustrted y Mchine P 0 1 2 6 7 8 4 5 Exercise 11. Give the Mirnd definition of the mchine P.

17 The "-closure of the set f0g is the set f0,1,2,4g. Looking t the definition of closure ove, the first ppliction of the function dd to f0g gives the set f0,1g; pplying dd to this gives f0,1,2,4g. Applying dd to this set gives the sme set, hence this is the vlue of setlimit here. The set of sttes with which we strt the simultion is therefore f0,1,2,4g. Suppose the first input is ; pplying onemove revels only one move, from 2 to. Tking the closure of the set fg gives the set f1,2,,4,6,7g. A move from here is only from 4 to 5; closing under "-moves gives f1,2,4,5,6,7g. An move from here is possile in two wys: from 2 to nd from 7 to 8; closing up f,8g gives f1,2,,4,6,7,8g. Is the string therefore ccepted y P? Yes, ecuse 8 is memer of f1,2,,4,6,7,8g. This sequence cn e illustrted thus 0 0 1 2 4 1 2 4 6 7 5 1 2 4 5 6 7 8 1 2 4 5 6 7 8 Exercise 12. Show tht the string is not ccepted y the mchine P. 8. Building NFAs from regulr expressions For ech regulr expression it is possile to uild n NFA which ccepts exctly those strings mtching the expression. The mchines re illustrted in Figure. The construction is y induction over the structure of the regulr expression: the mchines for n chrcter nd for " re given outright, nd for complex expressions, the mchines re uilt from the mchines representing the prts. It is strightforwrd to justify the construction. (e f) Any pth through M(e f) must e either pth through M(e) or pth through M(f) (with " t the strt nd end. ef Any pth through M(ef) will e pth through M(e) followed y pth through M(f).

18 M( ) M() M(e f) M(e) M(f) M(ef) M(e) M(f) M(e*) M(e) Figure : Building NFAs for regulr expressions

19 e* Pths through M(e*) re of two sorts; the first is simply n ", others egin with pth through M(e), nd continue with pth through M(e*). In other words, pths through M(e*) go through M(e) zero or more times. The mchine for the pttern ( )* is given y M(( )*) The Mirnd description of the construction is given in uild nf.m. At the top level the function uild :: reg -> nf num does the recursion. For the se cse, uild (Literl c) = NFA (mkeset [0..1]) (sing (Move 0 c 1)) 0 (sing 1) The definition of uild Epsilon is similr. In the other cses we define uild (Or r1 r2) = m or (uild r1) (uild r2) uild (Then r1 r2) = m then (uild r1) (uild r2) uild (Str r) = m str (uild r) in which the functions m or nd so on uild the mchines from their components s illustrted.

20 We mke certin ssumptions out the NFAs we uild. We tke it tht the sttes re numered from 0, with the finl stte hving the highest numer. Putting the mchines together will involve dding vrious new sttes nd trnsitions, nd renumering the sttes nd moves in the constituent mchines. An exmple progrm is m or :: nf num -> nf num -> nf num m or (NFA sttes1 moves1 strt1 finish1) (NFA sttes2 moves2 strt2 finish2) = NFA (sttes1 $union sttes2 $union newsttes) (moves1 $union moves2 $union newmoves) 0 (sing (m1+m2+1)) where m1 = crd sttes1 m2 = crd sttes2 sttes1 = mpset (renumer 1) sttes1 sttes2 = mpset (renumer (m1+1)) sttes2 newsttes = mkeset [0,(m1+m2+1)] moves1 = mpset (renumer move 1) moves1 moves2 = mpset (renumer move (m1+1)) moves2 newmoves = mkeset [ Emove 0 1, Emove 0 (m1+1), Emove m1 (m1+m2+1), Emove (m1+m2) (m1+m2+1) ] The function renumer renumers sttes nd renumer move moves. 9. Deterministic mchines A deterministic finite utomton is n NFA which contins no "-moves, nd hs t most one rrow lelled with prticulr symol leving ny given stte. The effect of this is to mke opertion of the mchine deterministic t ny stge there is t most one possile move to mke, nd so fter reding sequence of chrcters, the

21 mchine cn e in one stte t most. Implementing mchine of this sort is much simpler thn for n generl NFA: we only hve to keep trck of single position. Is there generl mechnism for finding DFA corresponding to regulr expression? In fct, there is generl technique for trnsforming n ritrry NFA into DFA, nd this we exmine now. The conversion of n NFA into DFA is sed on the implementtion given in Section 6. The min ide there is to keep trck of set of sttes, representing ll the possile positions fter reding certin mount of input. This set itself cn e thought of s stte of nother mchine, which will e deterministic: the moves from one set to nother re completely deterministic. We show how the conversion works with the mchine P. The strt stte of the mchine will e the closure of the set f0g, tht is A = f0,1,2,4g Now, the construction proceeds y finding the sets ccessile from A y moves on nd on ll the chrcters in the lphet of the mchine P. These sets re sttes of the new mchine; we then repet the construction with these new sttes, until no more sttes re produced y the construction. From A on the symol we cn move to from 2. Closing under "-moves we hve the set f1,2,,4,6,7g, which we cll B B = f1,2,,4,6,7g A ;! B In similr wy, from A on we hve C = f1,2,4,5,6,7g A ;! C Our new mchine so fr looks like B A C We now hve to see wht is ccessile from B nd C. First B. D = f1,2,,4,6,7,8g B ;! D

22 which is nother new stte. The process of generting new sttes must stop, s there is only finite numer of sets of sttes to choose from f0,1,2,,4,5,6,7,8g. Wht hppens with move from B? B ;! C This gives the prtil mchine B A D C Similrly, C ;! D C ;! C D ;! D D ;! C which completes the construction of the DFA A B D C Which of the new sttes is finl? One of these sets represents n ccepting stte exctly when it contins finl stte of the originl mchine. For P this is 8, which is contined in the set D only. In generl there cn e more thn one ccepting stte for mchine. (This need not e true for NFAs, since we cn lwys dd new finl stte to which ech of the originls is linked y n "-move.)

2 10. Trnsforming NFAs to DFAs The Mirnd code to covert n NFA to DFA is found in the file nf to df.m, nd the min function is mke deterministic :: nf num -> nf num mke deterministic = numer. mke deter A deterministic version of n NFA with numeric sttes is defined in two stges, using mke deter :: nf num -> nf (set num) numer :: nf (set num) -> nf num mke deter does the conversion to the deterministic utomton with sets of numers s sttes, numer replces sets of numers y numers (rther thn cpitl letters, s ws done ove). Sttes re replced y their position in list of sttes see the file for more detils. mke deter is specil cse of the function deterministic :: nf num -> [chr] -> nf (set num) mke deter mch = deterministic mch (lphet mch) The process of dding stte sets is repeted until no more sets re dded. This is version of tking limit, given y the nf limit function, which cts s the usul limit function, except tht it checks for equlity of NFAs s collections of sets. deterministic mch lph = nf limit (ddstep mch lph) strtmch where strtmch = NFA (sing strter) empty strter finish

24 strter = closure mch (sing strt) finish = empty, if eqset (term $inter strter) empty = sing strter, otherwise (NFA sts mvs strt term) = mch The strt mchine, strtmch, consists of single stte, the "-closure of the strt stte of the originl mchine. ddstep mch lph tkes prtilly uilt DFA nd dds the stte sets of mch ccessile y single move on ny of the chrcters in lph, the lphet of mch. ddstep :: nf num -> [chr] -> nf (set num) -> nf (set num) ddstep mch lph df = dd ux mch lph df (fltten sttes) where (NFA sttes m s f) = df dd ux mch lph df [] = df dd ux mch lph df (st:rest) = dd ux mch lph (ddmoves mch st lph df) rest This involves iterting over the stte sets in the prtilly uilt DFA, which is done using ddmoves. ddmoves mch x lph df will dd to df ll the moves from stte set x over the lphet lph. ddmoves :: nf num -> set num -> [chr] -> nf (set num) -> nf (set num) ddmoves mch x [] df = df ddmoves mch x (c:r) df = ddmoves mch x r (ddmove mch x c df) In turn, ddmoves itertes long the lphet, using ddmove. ddmove mch x c df will dd to df the moves from stte set x on chrcter c. ddmove :: nf num -> set num -> chr

25 -> nf (set num) -> nf (set num) ddmove mch x c (NFA sttes moves strt finish) = NFA sttes moves strt finish where sttes = sttes $union (sing new) moves = moves $union (sing (Move x c new)) finish = finish $union (sing new), if (eqset empty (term $inter new)) = finish, otherwise new = onetrns mch c x (NFA s m q term) = mch The new stte set dded y ddmove is defined using the onetrns function first defined in the simultion of the NFA. 11. Minimising DFA In uilding DFA, we hve produced mchine which cm e implemented more efficiently. We might, however, hve more sttes in the DFA thn necessry. This section shows how we cn optimise DFA so tht it contins the minimum numer of sttes to perform its function of recognising the strings mtching prticulr regulr expression. Two sttes m nd n in DFA re distinguishle if we cn find string st which reches n ccepting stte from n ut not from n (or vice vers). Otherwise, they cn e treted s the sme, ecuse no string mkes them ehve differently putting it different wy, no experiment mkes the two different. How cn we tell when two sttes re different? We strt y dividing the sttes into two prtitions: one contins the ccepting sttes, nd the other the reminder, or non-ccepting sttes. For our exmple, we get the prtition I: D II: A,B,C Now, for ech set in the prtition, we check whether the elements in the set cn e further divided. We look t how ech of the sttes in the set ehves reltive to the previous prtition. In pictures,

26 B (II) D (I) D (I) A B C C (II) C (II) C (II) This mens tht we cn re-prtition thus: I: D II: A III: B,C We now repet the process, nd exmine the only set which might e further sudivided, giving D (I) D (I) B C C (III) C (III) This shows tht we don t hve to re-prtition ny further, nd so tht we cn stop now, nd collpse the two sttes B nd C into one, thus: A The Mirnd implementtion of this process is in the file minimise df.m. Exercises 1. For the regulr expression ( )*, find the corresponding NFA. 14. For the NFA of question 1, find the corresponding (non-optimised) DFA. 15. For the DFA of question 2, find the optimised DFA. BC D

27 12. Regulr definitions A regulr definition consists of numer of nmed regulr expressions. We re llowed to use the defined nmes on the right-hnd sides of definitions fter the definition of the nme. For exmple, lph -> [-za-z] digit -> [0-9] lphnum -> lph digit ident -> lph lphnum* digits -> digit+ frct -> (.digits)? num -> digits frct Becuse of the stipultion tht definition precedes the use of nme, we cn expnd ech right-hnd side to regulr expression involving no nmes. We cn uild mchines to recognise strings from numer of regulr expressions. Suppose we hve the ptterns p1: p2: p: ** We cn uild the three NFAs thus: 1 2 4 5 6 7 8 nd then they cn e joined into single mchine, thus

28 1 2 p1 0 4 5 6 p2 7 8 p In using the mchine we look for the longest mtch ginst ny of the ptterns: 0,1,,7,8(p) 2(p1),4,7,8(p) 7,8(p) 8(p) - In the exmple, the segment of mtches the pttern p. Exercises 16. Fully expnd the nmes digits nd num given ove. 17. Build Mirnd progrm to recognise strings ccording to set of regulr definitions, s outlined in this section. Biliogrphy [Aho et. l.] Aho, A.V., Sethi, R. nd Ullmn, J.D., Compilers: Principles, Techniques nd Tools, Addison-Wesley, Reding, MA, USA, 1986. [Thompson] Thompson, S., Mirnd The Crft of Functionl Progrmming, Addison- Wesley, Wokinghm, Berks, UK, 1995.