You are on page 1of 15

CONSTRUCTION OF NFA AND

DFA FROM R.E

By…
Sukanta behera
Reg. No.
07SBSCA048
OUTLINE
INTRODUCTION
DFA
NFA WITH ε- MOVES
REGULAR EXPRESSION
DFA FROM R.E
NFA FROM R.E
INTRODUCTION
A Finite automaton is a mathematical
model of a with discrete inputs and
outputs
E.g.: A switch circuit
1
q0 q
1

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 5-
tuple 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 ε-MOVES
This 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 EXPRESSION
The 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 Expressions
All 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 order

Cons → (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
Construction
RE→ NFA(Thompson’s construction)
Build an NFA for each term
Combine them with ε-moves
NFA→ DFA (subset construction)
Build the simulation The Cycle of Constructions
DFA→ Minimal DFA
Hopcroft’s algorithm
minimal
DFA→RE (Not part of the scanner construction)
RE NFA DFA
DFA

All pairs, all paths problem
Take the union of all paths from s0 to an
accepting state
using Thompson’s
RE →NFA

Construction
Key idea
 NFA pattern for each symbol & each operator
 Join them with ε moves in precedence order
a
S0 S1 a ε b
S0 S1 S3 S4
NFA for
a NFA for ab

S1
a
S2 ε
ε ε
S0 S5 S0 ε S1
a
S3 ε S4
ε b ε ε
S3 S4
NFA for a*
NFA for a | b
Example of Construction
Let’s try a ( b | c )*
1. a, b, & c
a b c
S0 S1 S0 S1 S0 S1

2. b | c S1
b
S2
ε ε
S0 S5
ε c ε
S3 S4

ε

3. ( b | c )* ε S2
b
S3 ε
S0 ε S1 S6 ε S7
ε c ε
S4 S5
ε
Example of Construction
(con’t)

4. a ( b | c )* ε
b
ε S4 S5 ε
S0
a
S1 ε ε ε
S2 S3 S8 S9
ε c ε
S6 S7
ε

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

a
S0 S1
with Subset
NFA →DFA

Construction
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 )* :
b q5
ε q4 ε
q0 a q1 ε q2 ε q3 q8 ε q9
ε q6 c q7 ε
ε

Applying the subset construction:
ε-closure(move(s,*))
NFA states a b c
s0 q0 q1, q2, q3, none none
q4, q6, q9
s1 q1, q2, q3, none q5, q8, q9, q7, q8, q9,
q4, q6, q9 q3, q4, q6 q3, q4, q6
s2 q5, q8, q9, none s2 s3
q3, q4, q6
s3 q7, q8, q9, none s2 s3
q3, q4, q6

Final states
with Subset
NFA →DFA

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

c
s3 - s2 s3

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