Lecture 4-FSMs-NFA-DFA
Lecture 4-FSMs-NFA-DFA
Lecture 4
Finite State Automata (FSA)
1
Syllabus and Terminologies
Regular Languages .. Regular Sets
REs (Regular Expressions)
FSMs (or FSA/FA) … Finite State Machines/Automata
DFA vs. NFA … Deterministic vs. Non-deterministic FSA
Comparison and conversion
Examples & Closure Operations
Pumping Lemma
Context Free Languages
CFGs … Context Free Grammars
PDA … Push Down Automata
Parsing: CFG generating strings vs. PDA recognizing strings
Turing Machine
05/11/2024 FCI-CU-EG 2
Finite Automaton
Input
String
Output
“Accept”
Finite
or
Automaton
“Reject”
Finite Automata State Graphs
A state
• An accepting/final state
a
• A transition
4
Transition Graph
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
initial accepting
state state
transition
state
Regular Expressions to Finite Automata
High-level sketch
NFA
Regular
expressions DFA
Lexical Table-driven
Specification Implementation of DFA
6
Regular Expressions to NFA (1)
A
• For
• For input a
a
7
Regular Expressions to NFA (2)
For AB
A B
• For A | B
B
A
8
Regular Expressions to NFA (3)
For A*
A
9
EXAMPLE OF (RE → NFA) CONVERSION
10
Example of (RE → NFA) conversion
C 1 E
A B 1
0 F G H I J
D
11
NFA
NFA travels all possible paths, and so it remains in many states at once.
As long as at least one of the paths results in an accepting state, the
NFA accepts the input.
Alphabet ={a}
q1 a q2
a
q0
a
q3
12
NFA
Alphabet = {a}
Two choices
q1 a q2
a
q0
a
q3
13
NFA
14
NFA
OR
• The input cannot be consumed
15
NFA Acceptance Example 1
a a
q1 a q2
a
q0
a
q3
16
NFA
a a
q1 a q2
a
q0
a
q3
17
NFA
a a
q1 a q2 “accept”
a
q0
a
q3 “reject”
18
NFA
“accept”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
because this
computation this computation
acceptsaa is ignored
19
NFA
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 “reject” q3
“reject”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
Remember!
Bothe Lambda λ and Epsilon є can be used to represent an empty move
q0 a q1 q2 a q3
LAMBDA TRANSITIONS
Acceptance Example 2
a a
q0 a q1 q2 a q3
LAMBDA TRANSITIONS
a a
q0 a q1 q2 a q3
LAMBDA TRANSITIONS
input tape head does not move
a a
q0 a q1 q2 a q3
25
LAMBDA TRANSITIONS
all input is consumed
a a
“accept”
q0 a q1 q2 a q3
String aa is accepted
LAMBDA TRANSITIONS
Rejection Example 3
a a a
q0 a q1 q2 a q3
LAMBDA TRANSITIONS
a a a
q0 a q1 q2 a q3
LAMBDA TRANSITIONS
(read head doesn’t move)
a a a
q0 a q1 q2 a q3
LAMBDA TRANSITIONS
Input cannot be consumed
a a a
Automaton halts
“reject”
q0 a q1 q2 a q3
q0 a q1 q2 a q3
Example 4
q0 a q1 b q2 q3
a b
q0 a q1 b q2 q3
a b
q0 a q1 b q2 q3
a b
“accept”
q0 a q1 b q2 q3
Another String
a b a b
q0 a q1 b q2 q3
a b a b
q0 a q1 b q2 q3
a b a b
q0 a q1 b q2 q3
a b a b
q0 a q1 b q2 q3
a b a b
q0 a q1 b q2 q3
a b a b
q0 a q1 b q2 q3
a b a b
“accept”
q0 a q1 b q2 q3
Language accepted
q0 a q1 b q2 q3
43
Example 5
L( M ) ??
0
q0 q1 0, 1 q2
1
44
Language accepted
46
Another Example
47
Extended Transition *
Definition: The language accepted by an NFA M = (Q, , ,
q0, F) is the set of all strings on accepted by M.
Formally,
L(M) = {w * : *(q0, w) F }
* q0 , a ??
q1 q4 q5
a a
q0 a q1 b q2 q3
48
Extended Transition
* q0 , aa ??
q4 , q5
q4 q5
a a
q0 a q1 b q2 q3
49
Extended Transition
* q0 , ab ??
q2 , q3 , q0
q4 q5
a a
q0 a q1 b q2 q3
50
Extended Transition
If
F q0 ,q5 * q0 , aa ??
q4 , q5
q4 q5
a a
q0 a q1 b q2 q3
then
aa L(M )
51
Extended Transition
q4 q5
a a
q0 a q1 b q2 q3
ab LM
52
Extended Transition
F q0 ,q5 * q0 , abaa ??
q4 , q5
q4 q5
a a
q0 a q1 b q2 q3
abaa L(M )
53
Extended Transition
q4 q5
a a
q0 a q1 b q2 q3
aba LM
54
Extended Transition
q4 q5
a a
q0 a q1 b q2 q3
*
L(M ) = {ab }* È {ab} {aa}
55
NFA example: Multiple Paths
b a
b
q0 q1
a b
q2 b
b b a b b b b a b b
q0 q 0 q 1 q 1 q 2 q 1 q0 q 1 q 2 q 0 q 0 q 1
q0 q 1 q 2 q 0 q 0 q 0 q0 q 1 q 2 q 0 q 1 q 2
56
Nondeterministic Finite Automata
Definition: The language accepted by a nfa M = (Q, , ,
q0, F) is the set of all strings on accepted by M. Formally,
L(M) = {w * : *(q0, w) F }
qi
w
qk
q0 w
w qj
57
Deterministic Finite Automata (DFA)
Example:
b
a 1 a
0 3 a, b
a
b b
2
58
DFA
Example
M =({q0, q1, q2}, {0,1}, , q0, {q1}),
where is
q0 q1 q2
1 1
59
Deterministic finite automata (DFA)
b q2 q4 a
0 1 0,
1 0 1
q0 q1 q2
S = {0, 1}
62
Languages and Dfa’s
Example:
a a,b
q0 b q1 a, b q2
trap state
L = { anb: n >= 0}
63
Deterministic Finite Automata
Find a deterministic finite accepter that recognizes the set of all
strings on = {a, b} staring with a prefix ab.
a,b
q0 a q1 b q2
b a
q3 a,b
Find a dfa that accepts all strings on {0, 1}, except those containing
the substring 001.
1 0 0,1
1
⋀ 0 0 00 1 001
0
64
Ex: DFA
All words with even count of letters.
((a+b)(a+b))*
a, b
1± 2
a, b
65
Ex: DFA
All words that start with “a”
a(a+b)*
a,b b
b 2 2
1- 1-
a 3+ a 3+ a,b
a,b
Does not accept all inputs
66
Ex: DFA
All words that start with “a”
a(a+b)* a,b
b 2
1- a,b
a a,b
3+ 4+
67
Ex: DFA (not well defined)
All words that start with triple letter
(aaa+bbb)(a+b)*
a a a
2 3
1- 6+ a,b
4 5
b b b
68
NFA vs. DFA
69
NFA and DFA
Example:
The following are NFA and the DFA that describes the
same language:
L={(10)n : n≥0}
0
0, 1 q DFA 0,1
q0 q1 2 0
1
q0 q1 1 q2
NFA 1
0
70
NFA vs. DFA (2)
1 0
0 0
DFA
1
1
• DFA can be exponentially larger than NFA
71
NFA vs. DFA
Compactibility Readability Speed
NFA Good Good Slow
DFA Bad Bad Fast
• DFAs are widely used to build lexical analyzers.
NFA
DFA
The language that recognizes (a+b) * a b 76