Construction of Nfa and Dfa From r

15
By… Sukanta behera Reg. No. 07SBSCA048 CONSTRUCTION OF NFA AND DFA FROM R.E

Transcript of Construction of Nfa and Dfa From r

CONSTRUCTION OF NFA AND DFA FROM R.E

By Sukanta behera Reg. No. 07SBSCA048

OUTLINEINTRODUCTION DFA NFA WITH - MOVES REGULAR EXPRESSION DFA FROM R.E NFA FROM R.E

INTRODUCTIONA Finite automaton is a mathematical model of a with discrete inputs and outputs E.g.: A switch circuit 1

q0

q1

0 A finite automaton(FA) consists of a finite set of states and a set of transistors from one state to another state that occur on an input symbol from alphabet Z.

Deterministic Finite Automata(DFA)For each input symbol there is exactly one transition out of each state such a finite automaton is otherwise called Deterministic Finite Automata(DFA). We formally define a finite automata by a 5tuple represented as M=[Q,,,q0,F] where Q is a finite set of states is a finite input alphabet. q0 in Q is the initial state F C Q : where F is a set of final states; and is the transition function that maps QX to Q.

NONDETERMINISTIC FINITE AUTOMATON WITH -MOVESThis NFA is modeled to include transitions on the empty input . Nondeterministic finite automaton with moves is defined to be a quintuple(Q,,,q0,F) where Q is a set of states is a set of input symbols q0 is the initial state F is the Final state The difference lies only in 1 the transition function that maps QX(U{}) to 2^Q i.e; (q,a) will consists of a set of states P such that there is a transition labeled a from q to p where a is either or a symbol in .

REGULAR EXPRESSIONThe language accepted by finite automata can be simple expressions called regular expressions The union of two languages L and M , denoted LUM, is the set of strings that are in either L or M or both. The concatenation of languages L and M is the set of strings that can be formed by taking any string in L and concatenating it with any string in M. The closure (or star, or Kleene closure) of a language L is denoted L* and represent the set of those strings that can be formed by taking any number of strings

More Regular ExpressionsAll strings of 1s and 0s ending in a 1( 0 | 1 )* 1

All strings over lowercase letters where the vowels (a,e,i,o, & u) occur exactly once, in ascending orderCons (b|c|d|f|g|h|j|k|l|m|n|p|q|r|s|t|v|w|x|y| z) Cons* a Cons* e Cons* i Cons* o Cons* u Cons*

All strings of 1s and 0s that do not contain three 0s in a row:

Overview: Direct construction of a nondeterministic

finite automaton (NFA) to recognize a given RE Requires -transitions to combine regular

subexpressions

Construct a deterministic finite automaton

(DFA) to simulate the NFA

Use a set-of-states construction

Minimize the number of states Hopcroft state minimization algorithm Generate the scanner code Additional specifications needed for details

Automating Scanner ConstructionRE NFA

(Thompsons construction) Build an NFA for each term Combine them with -moves DFA (subset construction) Build the simulation Minimal DFA Hopcrofts algorithm

NFA

The Cycle of Constructions

DFA

DFA

RE (Not part of the scanner construction) All pairs, all paths problem Take the union of all paths from s0 to an accepting stateRE NFA DFA

minimal DFA

using Thompsons ConstructionRE NFA

Key idea NFA pattern for each symbol & each operator Join them with moves in precedence orderS0 a aNFA for

S1

S0

a

S1

S3

b

S4

NFA for ab

S0

S1

a

S2

S5 S0

S1 a S3

S4

S3

b

S4

NFA for a*

NFA for a | b

Example of ConstructionLets try a ( b | c )* 1. a, b, & cS0 a S1 S0 b S1 S0 c S1

2. b | cS0

S1

b

S2

S5

S3

c

S4

3. ( b | c )*S0

S1

S2

b

S3

S6

S7

S4

c

S5

Example of Construction(cont)

4. a ( b | c )*S0 a S1

S3 S4 b S5

S8

S2

S9

S6

c

S7

Of course, a human would design something simpler ...b|c S0 a S1

with Subset ConstructionNFA DFA

Need to build a simulation of the NFA Two key functions Move(si , a) is set of states reachable from si by a -closure(si) is set of states reachable from si by The algorithm: Start state derived from s0 of the NFA Take its -closure S0 = -closure(s0) Take the image of S0, Move(S0, ) for each , and take its -closure Iterate until no more states are added

NFA DFA

with Subset Construction

a ( b | c )* :q0 a q1

q2

q4q3

b c

q5 q7

q8

q9

q6

Applying the subset construction: -closure(move(s,*))NFA states

a q1, q2, q3, q4, q6, q9 none none none

b none q5, q8, q9, q3, q4, q6 s2 s2

c none q7, q8, q9, q3, q4, q6 s3 s3

s0 s1 s2 s3

q0 q1, q2, q3, q4, q6, q9 q5, q8, q9, q3, q4, q6 q7, q8, q9, q3, q4, q6

Final states

with Subset ConstructionNFA DFA

The DFA for a ( b | c )*b b s0 a s1 c b s3 c s2 c

s0 s1 s2 s3

a s1 -

b s2 s2 s2

c s3 s3 s3

Ends up smaller than the NFA All transitions are deterministic Use same code skeleton as before