Professional Documents
Culture Documents
Spring 2024
Compiler Design
CS-352
Lecture (4)
1
Outline
2
Outline
3
Deterministic Finite Automat
4
Deterministic Finite Automat
(Cont.)
5
Outline
6
From Regular Expression to
Automata
• The regular expression is the notation of choice for
describing lexical analyzers and other pattern-processing
software. However, implementation of that software requires
the simulation of a DFA, or perhaps simulation of an NFA.
7
From Regular Expression to
Automata (Cont.)
Lexical Analyzer
R.E.
NFA
F.A.
DFA
R.G.
8
From Regular Expression to
Automata (Cont.)
Lexical Analyzer
R.E.
NFA
F.A.
DFA
R.G.
9
Outline
10
Construction of an NFA-ε from a
Regular Expression
• We now give an algorithm for converting any regular
expression to an NFA-ε that defines the same language. For
each subexpression the algorithm constructs an NFA-ε with a
single accepting state.
11
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Basis: For expression ε construct the NFA-ε.
• Here, i is a new state, the start state of this NFA-ε, and f is another
new state, the accepting state for the NFA-ε.
• where again i and f are new states, the start and accepting states,
respectively.
s
ε ε
ε ε
t
13
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Suppose r = st
ε s ε t ε
s t
14
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Suppose r = s*
ε
ε s ε
15
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Example: Let us use Algorithm 3.23 to construct an NFA-ε for r =
(a|b)*abb.
16
Construction of an NFA-ε from a
Regular Expression (Cont.)
• For subexpression r1, the first a, we construct the NFA-ε:-
17
Construction of an NFA-ε from a
Regular Expression (Cont.)
• We can now combine N(r1) and N(r2), using the construction
of Union to obtain the NFA-ε for r3 = r1|r2; this NFA-ε is
shown as follows.
18
Construction of an NFA-ε from a
Regular Expression (Cont.)
• The NFA-ε for r5 = (r3)* is then as shown as follows. We have
used the construction of Closure to build this NFA-ε from the
NFA-ε in the former Figure.
19
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Now, consider subexpression r6, which is another a. We use
the basis construction for a again, but we must use new
states.
20
Construction of an NFA-ε from a
Regular Expression (Cont.)
• To obtain the NFA-ε for r7 = r5r6, we apply the construction
of Concatenation. We merge states 7 and 7’, yielding the
NFA-ε of the following Figure. Continuing in this fashion with
new NFA's-ε for the two subexpressions b called r8 and r10,
we eventually construct the NFA for (a|b)*abb.
21
Construction of an NFA-ε from a
Regular Expression (Cont.)
• NFA-ε to recognize a regular expression letter (letter|digit)*.
22
Construction of an NFA-ε from a
Regular Expression (Cont.)
• NFA-ε to recognize a regular expression letter (letter|digit)*.
23
Outline
24
From Regular Expression to
Automata (Cont.)
Lexical Analyzer
R.E.
NFA
F.A.
DFA
R.G.
25
Conversion of an NFA-ε to DFA
• After reading input a1a2 ... an, the DFA is in that state which
corresponds to the set of states that the NFA-ε can reach,
from its start state, following paths labelled a1a2 ... an.
26
Conversion of an NFA-ε to DFA
(Cont.)
27
Conversion of an NFA-ε to DFA
(Cont.)
28
Conversion of an NFA-ε to DFA
(Cont.)
• Example: The following NFA-ε shows accepting (a|b)*abb; it
happens to be the one we shall construct directly from this
regular expression. Let us apply Algorithm 3.20 to this NFA-
ε.
29
Conversion of an NFA-ε to DFA
(Cont.)
• -closure({0}) = {0,1, 2, 4, 7}= A
• move(A, a) = {3, 8}
• -closure({3, 8}) = {3, 6, 7, 1, 2, 4, 8} = {1, 2, 3, 4, 6, 7, 8} = B
• Dtrans[A, a] = B
• move (A, b) = {5}
• -closure({5}) = {5, 6, 7, 1, 2, 4} = {1, 2, 4, 5, 6, 7} = C
• Dtrans[A, b] = C
30
Conversion of an NFA-ε to DFA
(Cont.)
• move(B, a) = {3, 8}
• -closure({3, 8}) = B
• Dtrans[B, a] = B
• move(B, b) = {5, 9}
• -closure({5, 9}) = {1, 2, 4, 5, 6, 7, 9} = D
• Dtrans[B, b] = D
31
Conversion of an NFA-ε to DFA
(Cont.)
• move(C, a) = {3, 8}
• -closure({3, 8}) = B
• Dtrans[C, a] = B
• move(C, b) = {5}
• -closure({5}) = C
• Dtrans[C, b] = C
32
Conversion of an NFA-ε to DFA
(Cont.)
• move(D, a) = {3, 8}
• -closure({3, 8}) = B
• Dtrans[D, a] = B
• move(D, b) = {5, 10}
• -closure({5, 10}) = {1, 2, 4, 5, 6, 7, 10} = E
• Dtrans[D, b] = E
33
Conversion of an NFA-ε to DFA
(Cont.)
• move(E, a) = {3, 8}
• -closure({3, 8}) = B
• Dtrans[E, a] = B
• move(E, b) = {5}
• -closure({5}) = C
• Dtrans[E, b] = C
34
Conversion of an NFA-ε to DFA
(Cont.)
Alphabet
NFA State DFA State
a b
{0,1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
{1, 2, 4, 5, 6, 7} C B C
{1, 2, 4, 5, 6, 7, 9} D B E
{1, 2, 4, 5, 6, 7, 10} E B C
35
Conversion of an NFA-ε to DFA
(Cont.)
36