from re to nfa and vise versa
TRANSCRIPT
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 1/47
RegularExpression NFA DFA Program
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 2/47
RE
NFA
DFA
Minimized DFA
Program
Thompson construction
Subset construction
DFA simulation Scanner generator
Minimization
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 3/47
Scanner
generator
Finite
automaton
Regular
expression
scanner
program
String stream
Tokens
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 4/47
Regular expressions
y Regular expressions give us a way to formally expressthe lexical structure of a Regular language.
y We can build regular expressions using the followingrules.
1) (standing for the empty language) and I
(standing for the language {}) are regularexpression
2) Let 7 be the input alphabet:- If a 7 then a is aregular expression (denoting the set containing a).
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 5/47
Regular expressions
3) If R1 and R2 are regular expressions, then:
( R) a parenthesized R is a regular expression.
R1 | R2 is a regular expression, and | signifies union(sometimes + is used)
ab is a regular expression, and this signifies concatenation(sometimes . is used)
a* is a regular expression, and * signifies closure
4) A string is a regular expression if and only if it can bederived from the primitive regular expression by a finitenumber of applications of the previous rules
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 6/47
From Regular Expression to NFAy Thompsons construction - an NFA from a regular
expression
y Input: a regular expression r over an alphabet 7.y Output: an NFA N accepting L(r )
Winter 2007 SEG2101 Chapter 8 6
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 7/47
Winter 2007 SEG2101 Chapter 8 7
Methody First parse r into its constituent subexpressions.
y Construct NFAs for each of the basic symbols in r .
y for I
y for a in 7
y For the parenthesized regular expression (s), use N( s )itself as the NFA.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 8/47
Methody Union : For the regular expression s|t,
Winter 2007 SEG2101 Chapter 8 8
Start
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 9/47
Methody Concatenation: For the regular expression st
Start
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 10/47
Methody Kleene Star (closure) :For the regular expression s*
Winter 2007 SEG2101 Chapter 8 10
Every time we construct a new state, we give it a distinct name.
Start
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 11/47
Precedence
y When we start parsing r into its constituentsubexpressions we must take care of Precedence
y ( ) Parentheses have the highest Precedence (do it
first)
y * closure has the second Precedence
y . concatenation has the third Precedence
y
| Union has the lowest Precedencey Example: a|ba* = (a|(b(a*)))
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 12/47
From Regular Expression to NFAy Example 1 : construct N(r ) for the expression
r= (( a b )|c )*
Step 1: parse r into its constituent sub-expressions,giving each sub-expression a unique name.
r1 = a
r2 = b
r3 = (r1.r2)r4 = c
r5 = r3|r4
r6 = (r5)*
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 13/47
Step 1: parse r into its constituent sub-
expressions, giving each sub-expression a unique
name.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 14/47
Step 2: construct NFA for r1.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 15/47
Step 3: construct NFA for r2.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 16/47
Step 4: construct NFA for r3.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 17/47
Step 5: construct NFA for r4.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 18/47
Step 6: construct NFA for r5.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 19/47
Step 7: construct NFA for r6.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 20/47
Example2 - construct N( r) for
r=(a|b )*abby r1 = a
y r2 = b
y r3 = r1 | r2
y r4 = (r3)y r5 = r4*
y r6 = a
y r7 = r5.r6
y
r8 = by r9 = r7.r8
y r10 =b
y r11 = r9.r10
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 21/47
Step 1: parse r into its constituent sub-expressions,
giving each sub-expression a unique name.
y r10
r9
b
r8
r7r6
r5r4
r3
r1 r2
b
a*
|
a b
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 22/47
a
b
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 23/47
a | b
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 24/47
(a | b) *
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 25/47
(a | b) *a
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 26/47
(a | b) *abb
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 27/47
Finite Automatay
A finite automaton is a r ecognizer for a language that takesas input a string x and answer yes if x is a sentence of thelanguage and no otherwise.
y A finite automaton can be
y
N ond eter ministic finite automata (NFA )
have nor estr ictions on the labels of their edges. A symbol can labelseveral edges out of the same state, and I , the empty string,is a possible label.
y Deter ministic finite automata ( DFA ) have, f or each state,
and f or each symbol of its input alphabet exactly one edge with that symbol leaving that state.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 28/47
Finite Automata
y Both deterministic and nondeterministic finiteautomata are capable of recognizing the samelanguages, called the r egular lan guages , that r egular
e xpr essions can describe.y We can represent either an NFA or DFA by a tr ansition
gr a ph, wher e the nodes are states and the labeled edgesrepresent the transition function. There is an edge
labeled a f r om state s to state t if and only if t is one o f the ne xt states for state s and input a
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 29/47
Nondeterministic FiniteAutomata (NFA)y A nondeterministic finite automaton is defined as
quintuple M = (Q, 7, H, q0, F)
y
A set of states Qy A set of input symbols 7
y A transition function H that maps state-symbol pairs tosets of states
y
A state q0 that is distinguished as the start ( initial )
state
y A set of states F distinguished as acce ptin g ( final ) states
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 30/47
30
Deterministic FiniteAutomata (DFA)y A d eter ministic finite automaton ( DFA ) is a s pecial
case o f an NFA wher e:
y
There are no moves on inputI , and
y For each state s and in put s ymbol a, ther e is e xactly oneed ge out o f s labeled a.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 31/47
Conversion of an NFA into DFA
yThe Subset ConstructionAlgorithm1.Create the start state of the DFA by taking the I -
closure of the start state of the NFA.
2-Perform the following for the new DFA state:For each possible input symbol:y Apply move to the newly-created state and the input symbol;
this will return a set of states.y If A, B and C are states, move({A,B,C},`a') = move ( A,`a')Union move (B,`a') Union move (C,`a').
y Apply the I-closure to this set of states, possibly resulting in anew set.
y This set of NFA states will be a single state in theDFA.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 32/47
The Subset Construction Algorithm3.Each time we generate a new DFA state, we must apply
step 2 to it. The pr ocess is complete when applying step 2
do
es not yield any new states.
4.The f inish states of the DFA are those which contain any
of the f inish states of the NFA.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 33/47
What is I -closure ?y The I -closure is a function takes a state and returns
the set of states reachable from this state based on
(zero or more)I
-transitions. Note that this will alwaysinclude the state itself. We should be able to get from astate to any state in its I -closure without consumingany input.
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 34/47
Examplesy Example 1
a
b
a
P0q 1q 2q
NFA
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 35/47
Example 1
y I -closure (q0) = {q0} First Statey move({q0} , a) ={q1}
y I -closure ({q1})={q1,q2} New State
y move({q0} , b) ={ }
y move({q1,q2} , a) ={q1}y I -closure ({q1})= we made it before !!!
y move({q1,q2} , b) ={q0}
y I -closure ({qo})= we made it before !!!
y There are no more new states ,so we finished
y Note that q1 is Final state in NFA, so any node contains q1 isFinal State in DFA
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 36/47
Solution of Example 1
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 37/47
Example 2
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 38/47
Example 2y I -closure (1) = {1,2,3} first state
y move({1,2,3} , a) ={1,2}
y
move({1,2,3} , b) ={1,3,4}y I -closure ({1,2})= {1,2,3}
y I -closure ({1,3,4}) = {1,2,3,4} new state
y move({1,2,3,4} , a) = {1,2,5}
y move({1,2,3,4} , b) ={1,3,4}y I -closure ({1,2,5}) ={1,2,3,5} new state
y I -closure ({1,3,4}) = {1,2,3,4}
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 39/47
Very Important notey The function move takes a state and a character, and
returns the set of states reachable by one transition onthis character.
y When you draw DFA dont draw move({1,2,3} , b) ={1,3,4}
But draw move({1,2,3} , b) ={1,2,3,4}
Such that I -closure ({1,3,4}) = {1,2,3,4}
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 40/47
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 41/47
Example 3: Convert this NFA to DFA using
Subset Construction Algorithm
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 42/47
y I -closure (0) = {0,1,3} first state
y
move({0,1,3} , a) ={1,2}y I -closure ({1,2})={1,2,3} new state
y move({0,1,3} , b) ={3}
y I -closure ({3})={1,3} new state
ymove({1,2,3} , a)= { }
y move({1,2,3} , b)= {3}
y I -closure ({3})= we made it before !!!
y move({1,3} , a)= { }
y move({1,3} , b)= {3}y I -closure ({3})= we made it before !!!
y There are not new states ,so we finished here
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 43/47
DFA
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 44/47
Solved Examples but without Detailed
Solution Example 1
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 45/47
Solution
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 46/47
Example 2
8/3/2019 From RE to NFA and Vise Versa
http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 47/47
Solution