0% found this document useful (0 votes)
36 views38 pages

Compiler Design: NFA to DFA Conversion

Uploaded by

jouf00008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views38 pages

Compiler Design: NFA to DFA Conversion

Uploaded by

jouf00008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Faculty of Information Technology

Spring 2024

Compiler Design
CS-352
Lecture (4)

1
Outline

• Deterministic Finite Automata


• From Regular Expression to Automata
• Construction of an NFA-ε from a Regular
Expression
• Conversion of an NFA-ε to DFA

2
Outline

• Deterministic Finite Automata


• From Regular Expression to Automata
• Construction of an NFA-ε from a Regular
Expression
• Conversion of an NFA-ε to DFA

3
Deterministic Finite Automat

• A deterministic finite automaton (DFA) is a special case of an


NFA where:-
1. There are no moves on input ε, and
2. For each state s and input symbol a, there is exactly one edge
out of s labelled a.

• If we are using a transition table to represent a DFA, then


each entry is a single state. We may therefore represent
this state without the curly braces that we use to form
sets.

• Every regular expression and every NFA can be converted


to a DFA accepting the same language, because it is the
DFA that we really implement or simulate when building
lexical analyzers.

4
Deterministic Finite Automat
(Cont.)

5
Outline

• Deterministic Finite Automata


• From Regular Expression to Automata
• Construction of an NFA-ε from a Regular
Expression
• Conversion of an NFA-ε to DFA

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.

• Because an NFA often has a choice of move on an input


symbol or on ε, or even a choice of making a transition on ε
or on a real input symbol, its simulation is less
straightforward than for a DFA.

• Thus, often it is important to convert an NFA to a DFA that


accepts the same language.

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

• Deterministic Finite Automata


• From Regular Expression to Automata
• Construction of an NFA-ε from a Regular
Expression
• Conversion of an NFA-ε to DFA

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-ε.

• For any subexpression a in Σ, construct the NFA-ε.

• where again i and f are new states, the start and accepting states,
respectively.

• Note that in both of the basis constructions, we construct a distinct


NFA-ε, with new states, for every occurrence of ε or some a as a
subexpression of r.
12
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Suppose r = s | t

s
ε ε

ε ε
t

NFA-ε for the Union of two Regular Expressions

13
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Suppose r = st

ε s ε t ε

s t

NFA-ε for the Concatenation of two Regular Expressions

14
Construction of an NFA-ε from a
Regular Expression (Cont.)
• Suppose r = s*
ε

ε s ε

NFA-ε for the Closure of a Regular Expression

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.

• The following Figure shows a parse tree for r that is analogous to


the parse trees constructed for arithmetic expressions.

Parse Tree for (a|b)*abb

16
Construction of an NFA-ε from a
Regular Expression (Cont.)
• For subexpression r1, the first a, we construct the NFA-ε:-

• State numbers have been chosen for consistency with what


follows. For r2 we construct:-

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.

• The NFA-ε for r4 = (r3) is the same as that for r3.

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.

• It is not permissible to reuse the NFA-ε we constructed for r1,


even though r1 and r6 are the same expression.

• The NFA-ε for r6 is:-

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

• Deterministic Finite Automata


• From Regular Expression to Automata
• Construction of an NFA-ε from a Regular
Expression
• Conversion of an NFA-ε to DFA

24
From Regular Expression to
Automata (Cont.)

Lexical Analyzer
R.E.
NFA
F.A.
DFA
R.G.

25
Conversion of an NFA-ε to DFA

• We shall first show how to convert NFA’s-ε to DFA's. Then,


we use this technique, known as "the subset construction," to
give a useful algorithm for simulating NFA’s-ε directly, in
situations (other than lexical analysis) where the NFA-to-DFA
conversion takes more time than the direct simulation.

• The general idea behind the subset construction is that each


state of the constructed DFA corresponds to a set of NFA-ε
states.

• 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.)

Operations on NFA States

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.)

Result of Applying the Subset


Construction

36

You might also like