2. 2
Finite Automata
• Recognizer for a language
– takes an input string & determines whether it’s a
valid sentence of the language
• Non Deterministic
– Has more than one alternative action for the same
input symbol
• Deterministic
– Has at most one action for a given input symbol
4. 4
Non Deterministic Finite
Automata
• An NFA is a mathematical model that consists of
:
– S, a set of states
– , the symbols of the input alphabet
– move, a transition function.
• move(state, symbol) set of states
• move : S {} S
– A state, s0 S, the start state
– F S, a set of final or accepting states.
5. 5
Representation of NFAs
• Transition diagrams
– 2 edges on same input
– edges are permitted
• Transition tables
– Suitable for program implementations
– Takes up a lot of space when the input alphabet is
large
– Provides fast access
– Adjacency list representation – compact but slow
6. 6
Example (a/b)*abb
S = { 0, 1, 2, 3 }
s0 = 0
F = { 3 }
= { a, b }
start
0 3
b
2
1 b
a
a
b
s
t
a
t
e
i n p u t
0
1
2
a b
{ 0, 1 }
-- { 2 }
-- { 3 }
{ 0 }
(null) moves possible
j
i
Switch state but do not
use any input symbol
7. 7
NFA
• An NFA accepts a string x iff there is some
path in the transition graph from the start
state to some accepting state such that the
edge labels along the path spell out x.
• Can add dead state to handle all undefined
transitions
8. 8
Example (Contd.)
start
0 3
b
2
1 b
a
a
b Given an input string, we trace moves
If no more input & in final state, ACCEPT
EXAMPLE:
Input: ababb
move(0, a) = 1
move(1, b) = 2
move(2, a) = ? (undefined)
REJECT !
move(0, a) = 0
move(0, b) = 0
move(0, a) = 1
move(1, b) = 2
move(2, b) = 3
ACCEPT !
9. 9
Constructing an NFA from a RE
• Syntax directed construction procedure
• Each step of the construction process
introduces at most two new states
• Resulting NFA has at most 2 x No. of
symbols and operators- states
10. 10
Thompson’s Construction Algorithm
• Input: A regular expression r over an
alphabet
• Output: An NFA N accepting L(r)
– First r is parsed into its constituent sub-
expressions
– Then NFA is constructed for each of the basic
symbols
– If same symbol occurs repeatedly, separate
NFA is constructed for each occurrence
11. 11
Thompson’s Construction Algorithm
2. For a in the regular expression, construct NFA
a
start i f L(a)
1. For in the regular expression, construct NFA
L()
start i f
12. 12
Thompson’s Construction Algorithm
where i and f are new start / final states, and -moves
are introduced from i to the old start states of N(s) and
N(t) as well as from all of their final states to f.
3(a) If s, t are regular expressions, N(s), N(t) their NFAs
s|t has NFA:
i f
N(s)
N(t)
L(s) L(t)
13. 13
Thompson’s Construction Algorithm
3.(b) If s, t are regular expressions, N(s), N(t) their NFAs
st (concatenation) has NFA:
start
i f
N(s) N(t) L(s) L(t)
Alternative:
overlap
N(s)
start i f
N(t)
where i is the start state of N(s) (or new under the
alternative) and f is the final state of N(t) (or new).
Overlap maps final states of N(s) to start state of N(t).
14. 14
Thompson’s Construction Algorithm
f
N(s)
start i
where : i is new start state and f is new final state
-move i to f (to accept null string)
-moves i to old start, old final(s) to f
-move old final to old start
3.(c) If s is a regular expressions, N(s) its NFA, s*
(Kleene star) has NFA:
15. 15
Properties of Construction
Let r be a regular expression, with NFA N(r), then
• N(r) has #of states 2*(#symbols +
#operators) of r
• N(r) has exactly one start and one accepting
state
• Each state of N(r) has at most one outgoing
edge a or at most two outgoing ’s
• Unique names should be assigned to all states
18. 18
NFA Construction Example
r11: a
r7: b
r6: c
c
r9 : r7 | r8
b
c
r8:
c
r12 : r11 r10
b
a
r10 : r9
19. 19
NFA Construction Example
r13 : r5 | r12
b
a c
c
b
a
1
6
5
4
3
8
2
10
9 12 13 14
11
15
7
16
17
20. 20
Simulation of an NFA
• Given an NFA N constructed by Thompson’s
Algorithm and an input string x – determine whether
N accepts x
• Algorithm reads input one character at a time and
computes set of states N could be in after having
read each prefix
• Runs in time |N| x |x|
21. 21
Simulation of an NFA
ε-closure(T)
push all states in T onto stack
initialize ε-closure(T) to T
while stack is not empty
pop t from top of stack
for each state u with an ε-transition from t
if u is not in ε-closure(T) then
add u to ε-closure(T)
push u onto stack
22. 22
Simulation of an NFA
• Input: NFA and input string x
• Output: Yes/ No
• Similar to Subset construction procedure; Computes ε-
closure(move(S,a))
S -closure({s0})
c nextchar;
while c eof do
S -closure(move(S,c));
c nextchar;
end;
if SF then return “yes” else return “no”
23. 23
Simulation of an NFA
• Two Stacks and bit vector implementation
of states can be used
– Stack 1 – keeps track of current set of states
– Stack 2 – To compute next set
– Roles of both stacks can be interchanged