Download presentation
Presentation is loading. Please wait.
Published byKerry Perkins Modified over 9 years ago
1
CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130 The Chinese University of Hong Kong DFA to regular expression conversion Fall 2009
2
Road map NFA regular expression NFA without DFA
3
Road map NFA regular expression NFA without DFA 2-state GNFA GNFA
4
Generalized NFAs A generalized NFA is an NFA whose transitions are labeled by regular expressions, like moreover –It has exactly one accept state, different from its start state –No arrows come into the start state –No arrows go out of the accept state +10* 0*11 0*1 qq qq qq 01
5
Converting a DFA to a GNFA NFA regular expression NFA without DFA 2-state GNFA GNFA qq qq qq qq qfqf
6
Conversion example q1q1 q2q2 0 1 1 0 –It has exactly one accept state, different from its start state –No arrows come into the start state –No arrows go out of the accept state q0q0 q3q3
7
GNFA state reduction NFA regular expression NFA without DFA 2-state GNFA GNFA From any GNFA, we can eliminate every state but the start and accept states
8
State elimination +10* 0*11 0*1 qq qq qq 01 qq qq +10*)(0*1)*0*11 qq qq +10*)(0*1)*0*11 + 01
9
State elimination – general method To eliminate state q k, for every pair of states (q i, q j ) remember to do this even when q i = q j ! Replace by R1R1 R3R3 R2R2 qiqi qkqk qjqj R4R4 R 1 R 2 *R 3 + R 4 qiqi qjqj
10
Road map NFA regular expression NFA without DFA 2-state GNFA GNFA qq qq R A 2-state GNFA is the same as a regular expression R !
11
Conversion example q1q1 q2q2 0 1 1 0 q0q0 q3q3 Eliminate q 1 : Eliminate q 2 : q2q2 q0q0 q3q3 0*1 00*1+1 q0q0 q3q3 0*1(00*1+1)* Check: 0*1(00*1+1)* = q1q1 q2q2 0 1 1 0
12
What you need to know DFA NFA regular expression regular languages DesignAnalyzeConvert
13
Some pitfalls NFA for all strings that end in 10 : NFA for all strings that do not contain 01 : 1 0 0, 1 qq qq qq 1 0 qq qq qq
14
How to do it right 1 0 0, 1 qq qq qq 1 qq {q 0, q 1 } 1 {q 0, q 2 } 1 0 0 0 1 qq {q 0, q 1 } 1 {q 0, q 2 } 1 0 0 0 NFA for ends in 10 DFA for ends in 10 DFA for does not end in 10
15
Another example NFA for even number of 0 s or odd number of 1 s RE for even number of 0 s or odd number of 1 s r0r0 r1r1 1 1 0 0 even number of 0 s s0s0 s1s1 0 0 1 1 odd number of 1 s q0q0 (1*01*01*)* + (0*10*10*)*(0*10*)
16
Another example Even number of 0 s and odd number of 1 s? We’ll see another way of doing this next week q ee q eo 1 1 1 1 q oe q oo 0 0 0 0
17
Text search
18
The program grep grep regexp file Searches for the occurrence of patterns matching a regular expression in a file [abc] { a, b, c } [a-z] { a, b, c,..., z } [ab][12] { a1, a2, b1, b2 } concatenation cat|12 { cat, 12 } union (ab)* { , ab, abab,...} star [ab]? { , a, b} zero or one (cat)+ { cat, catcat,...} one or more [ab]{2} { aa, ab, ba, bb } { n } copies
19
Regular expressions in grep Say we have a file w.txt Want to know all sunny days > grep ‘sunny’ w.txt Any cloudy days in April ‘09? > grep ‘[0-9]*/4/09 ([0-9]|C| )* cloudy’ w.txt Any consecutive string of 7 sunny days? > grep ‘(([0-9]|/|C| )* sunny,){7}’ w.txt 1/1/09 14C rain, 2/1/09 17C sunny, 3/1/09 18C sunny, …
20
Implementation of grep regular expression NFA NFA without DFA text file input [ab]?, a+, (cat){3} not allowedallowed input handling matches wholelooks for pattern outputaccept/reject finds position of pattern in text differencesour RERE in grep
21
Implementation of grep How do you handle expressions like: [ab]? zero or one (cat)+ one or more a{3} { n } copies → ()|[ab] R ? → |R → () |R → (cat)(cat)* R + → RR * → aaa R { n } → RR...R n times
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.