Unit 1.2
Unit 1.2
Unit 1: Part 2
2
Nondeterministic Finite Automata:
o Generalize FAs by adding nondeterminism, allowing several alternative computations
on the same input string.
o Ordinary deterministic FAs follow one path on each input.
3
Nondeterministic Finite Automata:
By continuing to experiment in this way, you will see that N1 accepts all
strings that contain either 101 or 11 as a substring.
4
Nondeterministic Finite Automata:
5
Formal Definition Nondeterministic Finite Automata:
6
Nondeterministic Finite Automata:
7
Nondeterministic Finite Automata:
NFA Example
8
Nondeterministic Finite Automata:
NFA Example
9
Nondeterministic Finite Automata:
Draw NFA which accept 00 or 11 at the end of a string where symbols are 0 and 1.
10
Nondeterministic Finite Automata:
Draw a NFA which accept a string containing “for” anywhere in a string of {a-z}
11
Concatenation of two Regular Languages:
Theorem 3: The class of regular languages are closed under concatenation.
In other words, If L1 and L2 are regular languages then L1o L2 is also a regular language
12
Star operation of Regular Language:
Theorem 3: The class of regular languages are closed under star operation.
In other words, If L is a regular language then L* is also a regular language
13
DFA VS
NFA
14
Converting NFA to
DFA
15
Converting NFA to
DFA
16
Converting NFA to
DFA
17
Converting NFA to
DFA
18
Converting NFA to
DFA
19
Converting NFA to
DFA
20
Converting NFA to
DFA
21
Converting NFA to
DFA
22
Converting NFA to
DFA
23
24
25
Converting NFA to
DFA
26
REGULAR EXPRESSIONS
27
REGULAR EXPRESSIONS
• Regular expressions
• describe regular languages
• Example:
28
REGULAR EXPRESSIONS
Recursive Definition
29
REGULAR EXPRESSIONS
A regular expression:
30
REGULAR EXPRESSIONS
Example
31
REGULAR EXPRESSIONS
32
REGULAR EXPRESSIONS
• Regular expression:
33
REGULAR EXPRESSIONS
• Regular expression
• Regular expression
34
35
REGULAR EXPRESSIONS
36
REGULAR EXPRESSIONS
37
REGULAR EXPRESSIONS to NFA
38
REGULAR EXPRESSIONS to NFA Thompson’s construction
39
REGULAR EXPRESSIONS to NFA
40
41
42
Constructing RE for given DFA using Arden's theorem
In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem
along with the properties of regular expressions.
Statement −
Let P and Q be two regular expressions.
If P does not contain null string, then R = Q + RP has a unique solution that is R = QP*
Proof −
R = Q + (Q + RP)P [After putting the value R = Q + RP]
= Q + QP + RPP
When we put the value of R recursively again and again, we get the following equation −
R = Q + QP + QP2 + QP3…..
R = Q (ε + P + P2 + P3 + …. )
R = QP* [As P* represents (ε + P + P2 + P3 + ….) ]
43
The equations for the three states q1, q2, and q3 are as follows −
q1 = q1a + q3a + ε (ε move is because q1 is the initial state0
q2 = q 1 b + q 2 b + q 3 b
q3 = q 2 a
45
State Elimination Method
Step-01:
Step-02:
46
State Elimination Method
Problem-01:
47
State Elimination Method
Problem-02:
48
State Elimination Method
Problem-03:
49
State Elimination Method
Problem-04:
Problem-05:
50
https://www.youtube.com/watch?v=mpyCeSvGh-M
https://www.guru99.com/linux-regular-expressions.html
51
Minimization of Finite Automata
1. State minimization problem
2. There can be more than one DFA that accepts the same language
Among these equivalent DFAs, it is often useful to find the smallest,
that is, the DFA with the minimum possible number of states
52
Minimization of Finite Automata
53
Minimization of Finite Automata
Unreachable states
1. A DFA sometimes contains states that cannot possibly be reached from the initial
state
2. These can easily be identified and they can be removed without changing the
language accepted by the DFA
54
Minimization of Finite Automata
Equate & collapse states having same behavior.
Build equivalence relation on states:
p≡q ↔ (∀z∈Σ*, (p,z)∈F ↔ (q,z)∈F)
z z
p p
or
z z
q q
55
56
DFA Minimization: Algorithm
57
DFA Minimization: Example
58
DFA Minimization: Example
59
Watch Video: 60
61
DFA Minimization: Example
62
DFA Minimization: Algorithm
1. Delete inaccessible ( or unreachable) states.
63
DFA Minimization: Example
0 1
0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0
0
d
e
2. Initialize table entries:
Unmarked, empty list f
a b c d e f g
64
DFA Minimization: Example
0 1
0 1 0
a b c d
1 0 1 b
0 1
1 1 0
e f g h c
1 0
0 d
e
3. Mark pairs of final &
nonfinal states f
h
a b c d e f g
65
DFA Minimization: Example
0 1
0 1 0
a b c d δ(b,0) ≡? δ(a,0)
0 1 b
1 δ(b,1) ≡? δ(a,1)
0 1
1 1 0
e f g h c
1 0
0
d
e
4. For each unmarked
pair & symbol, … f
a b c d e f g
Build equivalence relation on states:
p≡q ↔ (∀z∈Σ*, (p,z)∈F ↔ (q,z)∈F) 66
DFA Minimization: Example
0 1
0 1 0
a b c d g ≡? b Maybe.
0 1 b
1 c ≡? f No!
0 1
1 1 0
e f g h c
1 0
0
d
e
3. For each unmarked
pair & symbol, … f
h
a b c d e f g
Build equivalence relation on states:
p≡q ↔ (∀z∈Σ*, (p,z)∈F ↔ (q,z)∈F) 67
DFA Minimization: Example
0 1
0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0
0
d
e
4. For each unmarked
pair & symbol, … f
a b c d e f g
68
DFA Minimization: Example
0 1
0 1 0
a b c d δ(e,0) ≡? δ(a,0)
0 1 b
1 δ(e,1) ≡? δ(a,1)
0 1
1 1 0
e f g h c
1 0
0
d
e
4. For each unmarked
f
pair & symbol, …
g
a b c d e f g
69
DFA Minimization: Example
0 1
0 1 0
a b c d h ≡? b Maybe.
0 1 b
1 f ≡? f Yes.
0 1
1 1 0
e f g h c
1 0
0
d
e
4. For each unmarked
f
pair & symbol, …
g
h
(a,e)
a b c d e f g
70
DFA Minimization: Example
0 1
0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0
0
d
e
4. For each unmarked
f
pair & symbol, … (g,a)
g
(g,a)
h
(a,e)
a b c d e f g
71
DFA Minimization: Example
0 1
0 1 0
a b c d b Need to mark.
1 0 1
0 1 So, mark (g,a) also.
1 1 0
e f g h c
1 0
0
d
h
(a,e)
a b c d e f g
72
DFA Minimization: Example
0 1
0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0
0
d
e
4. For each unmarked
pair & symbol, … f
(g,a)
h
(a,e)
a b c d e f g
73
DFA Minimization: Example
0 1
0 1 0
a b c d b
1 0 1
0 1
1 1 0
e f g h c
1 0
0
d
e
5. Combine unmarked
pairs of states. f
0 1
a 0 b 1 g
a≡e c
e 1 h 0
b≡h 0
d≡f d 1 h
g (a,e)
1 f
0 a b c d e f g
74
DFA Minimization: Example
75
DFA Minimization: Correctness
76
DFA Minimization: Notes
77
DFA Minimization
78
Structure of Compiler
79
Lexical Analysis
80
Lexical Analysis- Example
81
Lexical Analysis- Example
82
Example of TOKENS
83
Example of TOKENS
84
85
86
Example of NON TOKENS
87
Tasks Lexical Analyzer
» Keeping track of line numbers while scanning the new line characters. These
line numbers are used by the error handler to print the error messages.
» Preprocessing of macros
88
Identify tokens and lexemes?
1. x=x*(acc+123)
89
Applications of Finite Automata to Lexical Analysis
90
Applications of Finite Automata to Lexical Analysis
91
Applications of Finite Automata to Lexical Analysis
92
Applications of Finite Automata to Lexical Analysis
93