from re to nfa and vise versa

47
Regular Expression NF A DFA Program

Upload: mahmoud-nawwar

Post on 06-Apr-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: From RE to NFA and Vise Versa

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

Page 2: From RE to NFA and Vise Versa

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

Page 3: From RE to NFA and Vise Versa

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

Page 4: From RE to NFA and Vise Versa

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).

Page 5: From RE to NFA and Vise Versa

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

Page 6: From RE to NFA and Vise Versa

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

Page 7: From RE to NFA and Vise Versa

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.

Page 8: From RE to NFA and Vise Versa

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

Page 9: From RE to NFA and Vise Versa

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

Page 10: From RE to NFA and Vise Versa

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

Page 11: From RE to NFA and Vise Versa

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*)))

Page 12: From RE to NFA and Vise Versa

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)*

Page 13: From RE to NFA and Vise Versa

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.

Page 14: From RE to NFA and Vise Versa

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.

Page 15: From RE to NFA and Vise Versa

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.

Page 16: From RE to NFA and Vise Versa

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.

Page 17: From RE to NFA and Vise Versa

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.

Page 18: From RE to NFA and Vise Versa

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.

Page 19: From RE to NFA and Vise Versa

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.

Page 20: From RE to NFA and Vise Versa

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

Page 21: From RE to NFA and Vise Versa

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

Page 22: From RE to NFA and Vise Versa

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

Page 23: From RE to NFA and Vise Versa

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

Page 24: From RE to NFA and Vise Versa

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) *

Page 25: From RE to NFA and Vise Versa

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

Page 26: From RE to NFA and Vise Versa

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

Page 27: From RE to NFA and Vise Versa

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.

Page 28: From RE to NFA and Vise Versa

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

Page 29: From RE to NFA and Vise Versa

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

Page 30: From RE to NFA and Vise Versa

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.

Page 31: From RE to NFA and Vise Versa

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.

Page 32: From RE to NFA and Vise Versa

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.

Page 33: From RE to NFA and Vise Versa

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.

Page 34: From RE to NFA and Vise Versa

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

Page 35: From RE to NFA and Vise Versa

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 

Page 36: From RE to NFA and Vise Versa

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

Page 37: From RE to NFA and Vise Versa

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

Page 38: From RE to NFA and Vise Versa

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}

Page 39: From RE to NFA and Vise Versa

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}

Page 40: From RE to NFA and Vise Versa

8/3/2019 From RE to NFA and Vise Versa

http://slidepdf.com/reader/full/from-re-to-nfa-and-vise-versa 40/47

Page 41: From RE to NFA and Vise Versa

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

Page 42: From RE to NFA and Vise Versa

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

Page 43: From RE to NFA and Vise Versa

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

Page 44: From RE to NFA and Vise Versa

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

Page 45: From RE to NFA and Vise Versa

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

Page 46: From RE to NFA and Vise Versa

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

Page 47: From RE to NFA and Vise Versa

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