Compilers
WELCOME TO A JOURNEY TO
CS419 Lecture 6
Scanning using Non-deterministic Finite automata (NFA) cont.
Cairo UniversityFCI
Dr. Hussien SharafComputer Science [email protected]
2
NON DETERMINISTIC FINITE AUTOMATA NFA There is a fixed number of states but we can be in multiple
states at one time.
NFA = “a 5-tuple “ (Q, Σ, , q0, F)Q A finite set of statesΣ A finite input alphabetq0 The initial/starting state, q0 is in QF A set of final/accepting states, which is a subset of Qδ A transition function, which is a total function from Q x Σ to 2Q , this function:
Takes a state and input symbol as arguments. Returns a set of states instead a single state as in
DFA.δ: (Q x Σ) –> 2Q -2Q is the power set of Q, the set of
all subsets of Q δ(q,s) is a function from Q x S to 2Q (but not to Q)
Dr. Hussien M. Sharaf
NFA
A finite automaton is deterministic if It has no edges/transitions labeled with
epsilon/lamda. For each state and for each symbol in
the alphabet, there is exactly one edge labeled with that symbol.
4
1q 2q
3q
a
a
a
0q
}{aAlphabet =
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.
Dr. Hussien M. Sharaf
5
1q 2q
3q
a
a
a
0q
Two choices
}{aAlphabet =
NFA
Dr. Hussien M. Sharaf
6
No transition
1q 2q
3q
a
a
a
0q
Two choicesNo transition
}{aAlphabet =
NFA
Dr. Hussien M. Sharaf
7
An NFA accepts a string:if there is a computation of the NFAthat accepts the string
i.e., all the input string is processed and the automaton is in an accepting state
NFA
Dr. Hussien M. Sharaf
8
a a
0q
1q 2q
3q
a
a
Acceptance Example 1
a
NFA
Dr. Hussien M. Sharaf
9
a a
0q
1q 2q
3q
a
a
a
First ChoiceNFA
Dr. Hussien M. Sharaf
10
a a
0q
1q 2q
3q
a
a
a “accept”
First Choice
All input is consumed
NFA
Dr. Hussien M. Sharaf
11
a a
0q
1q 2q
3q
a
a
Second Choice
a
NFA
Dr. Hussien M. Sharaf
12
a a
0q
1q 2qa
a
a
3q
Second Choice
“reject”
Input cannot be consumed
Automaton Halts
NFA
Dr. Hussien M. Sharaf
13
aa is accepted by the NFA:
0q
1q 2q
3q
a
a
a
“accept”
0q
1q 2qa
a
a
3q “reject”
because this computationaccepts aa
this computationis ignored
NFA
Dr. Hussien M. Sharaf
14
Dr. Hussien M. Sharaf
An NFA rejects a string:if there is no computation of the NFAthat accepts the string.
• All the input is consumed and the automaton is in a non final state
• The input cannot be consumed
OR
For each computation:
NFA
15
a is rejected by the NFA:
0q
1q 2qa
a
a
3q “reject”
0q
1q 2qa
a
a
3q
“reject”
All possible computations lead to rejection
NFA
Dr. Hussien M. Sharaf
16
aaais rejected by the NFA:
0q
1q 2q
3q
a
a
a
“reject”
0q
1q 2qa
a
a
3q “reject”
All possible computations lead to rejection
NFA
Dr. Hussien M. Sharaf
17
1q 3qa0q
2q a
Dr. Hussien M. Sharaf
LAMBDA TRANSITIONS
18
a a
1q 3qa0q
2q a
Dr. Hussien M. SharafLAMBDA TRANSITIONS
Acceptance Example 2
19
a a
1q 3qa0q
2q a
Dr. Hussien M. SharafLAMBDA TRANSITIONS
20
a a
1q 3qa0q
2q a
input tape head does not move
Dr. Hussien M. SharafLAMBDA TRANSITIONS
21
a a
1q 3qa0q
2q a
“accept”
String is acceptedaa
all input is consumed
Dr. Hussien M. SharafLAMBDA TRANSITIONS
22
a a
1q 3qa0q
2q a
Rejection Example 3
a
Dr. Hussien M. SharafDr. Hussien M. SharafLAMBDA TRANSITIONS
23
a a
1q 3qa0q
2q a
a
Dr. Hussien M. SharafLAMBDA TRANSITIONS
24
a a
1q 3qa0q
2q a
(read head doesn’t move)
a
Dr. Hussien M. SharafLAMBDA TRANSITIONS
25
a a
1q 3qa0q
2q a
“reject”
String is rejectedaaa
a
Input cannot be consumed
Automaton halts
Dr. Hussien M. SharafLAMBDA TRANSITIONS
26
Language accepted: }{aaL
1q 3qa0q
2q a
Dr. Hussien M. SharafLAMBDA TRANSITIONS
27
Example 4
0q 1q 2qa b
3q
Dr. Hussien M. Sharaf
28
a b
0q 1q 2qa b
3q
Dr. Hussien M. Sharaf
29
0q 2qa b
3q
a b
1q
Dr. Hussien M. Sharaf
30
a b
0q 1qa b
3q2q
“accept”
Dr. Hussien M. Sharaf
31
0qa b
a b
Another String
a b
1q 2q 3q
Dr. Hussien M. Sharaf
32
0qa b
a b a b
1q 2q 3q
Dr. Hussien M. Sharaf
33
0qa b
a b a b
1q 2q 3q
Dr. Hussien M. Sharaf
34
0qa b
a b a b
1q 2q 3q
Dr. Hussien M. Sharaf
35
0qa b
a b a b
1q 2q 3q
Dr. Hussien M. Sharaf
36
0qa b
a b a b
1q 2q 3q
Dr. Hussien M. Sharaf
37
a b a b
0qa b
1q 2q 3q
“accept”
Dr. Hussien M. Sharaf
38
ab
ababababababL ...,,,
Language accepted
0q 1q 2qa b
3q
Dr. Hussien M. Sharaf
39
EXAMPLE 5
0q 1q 2q0
11,0
Dr. Hussien M. Sharaf
40
{ }{ }*10=
...,101010,1010,10,λ=)(ML
0q 1q 2q0
11,0
Language accepted
(redundant state)
Dr. Hussien M. Sharaf
41
DETERMINISTIC AND NONDETERMINISTIC AUTOMATA
Deterministic Finite Automata (DFA) One transition per input per state No -moves
Nondeterministic Finite Automata (NFA) Can have multiple transitions for one input
in a given state Can have -moves
THANK YOU
Dr. Hussien M. Sharaf 42
Top Related