Lesson 12 - DFSA
Lesson 12 - DFSA
2
Finite State Machines (aka Finite Automata)
letter | digit
letter
S A
3
Finite State Machines viewed as Graphs
• A state
• An accepting state
a
• A transition
4
Finite State Machines
• Transition
s1 a
> s2
• Is read
In state s1 on input “a” go to state s2
• If end of input
– If in accepting state => accept
– Otherwise => reject
• If no transition possible (got stuck) => reject
5
Language defined by FSM
6
Example: Integer Literals
digit
B
digit
digit
+
S A
-
7
Formal Definition
8
State-Transition Function
(S, +) = A
(S, –) = A
(S, digit) = B
(A, digit) = B
(B, digit) = B
9
FSM Examples
0 1 1
A B
0
Accepts strings
over alphabet
{0,1} that end in 1
10
FSM Examples
a 1 b
a b
2 3
b a a b Accepts strings
over alphabet {a,b}
that begin and end
4 with same symbol
b 5 a
11
FSM Examples
0
Accepts strings over
{0,1,2} such that sum of
1 digits is a multiple of 3
Start 2
1
0 0 1 2 2 0
1
12
FSM Examples
0 0
1
Even Odd
1
Accepts strings over {0,1}
that have an odd number
of ones
13
FSM Examples
1 0,1
0 0 1
1
0
Accepts strings over
{0,1} that contain
the substring 001
14
Examples
15
FSM Examples
0
1
0 1
1
1 0
1 0
16
TEST YOURSELF
17
TEST YOURSELF
1
q0 q2
1
0 0 0 0
1
q1 q3
1
18
Two kinds of Finite State Machines
Deterministic (DFSM):
– No state has more than one outgoing edge with the
same label. [All previous FSM were DFSM.]
Non-deterministic (NFSM):
– States may have more than one outgoing edge with
same label.
– Edges may be labeled with (epsilon), the empty
string. [Note that some books use the symbol .]
– The automaton can make an epsilon transition
without consuming the current input character.
19
Example of NFSM
• integer-literal example:
digit
B
digit
+
S A
-
20
Example of NFSM
0,1 0,1
0 0 1
21
Non–deterministic finite state machines (NFSM)
• Theorem:
– For each non-deterministic finite state machine N,
we can construct a deterministic finite state
machine D such that N and D accept the same
language.
– [proof omitted]
• Theorem:
– Every deterministic finite state machine can be
regarded as a non–deterministic finite state
machine that just doesn’t use the extra non–
deterministic capabilities.
23
How to Implement a FSM
A table-driven approach:
• Table:
– one row for each state in the machine, and
– one column for each possible character.
• Table[j][k]
– which state to go to from state j on input character k,
– an empty entry corresponds to the machine getting stuck.
24
The table-driven program for a DFSM
25
Finite State Machines
Control Circuits
26
Example: Vending Machine
27
Example: Vending Machine
28
Example: Vending Machine
25
25 50
001 010 25
25 ret
ret soda
0 soda 75
000 ret 011
100
ret 25
Inputs
25 = 00 100
100 = 01 100
soda = 10
29
ret = 11
Example: Vending Machine
0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1
0 1 0 0 0 0 1 1 0 1 0 1 0 0 1 0
0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0
1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 1
0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0
0 0 1 0 1 0 0 1 0 0 1 1 1 0 0 0
0 1 0 0 1 0 1 0 0 1 0 1 1 0 0 0
0 1 1 0 1 0 1 1 0 1 1 1 1 0 0 0
1 0 0 0 1 1 0 0 1 0 0 1 1 0 0 0
30
Example: Vending Machine
0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0
0 0 1 0 0 0 1 0 0 0 1 1 0 0 0 1
0 1 0 0 0 0 1 1 0 1 0 1 0 0 1 0
0 1 1 0 0 1 0 0 0 1 1 1 0 0 0 0
1 0 0 0 X 1 0 0 1 0 0 1 0 0 0 1
0 0 0 0 1 1 0 0 X X X 1 1 0 0 0
0 0 1 0 1 0 0 1
0 1 0 0 1 0 1 0
0 1 1 0 1 0 1 1
31
Example: Vending Machine
Clock
S0
S1
S2
I0
I1
32