Lesson 14
Lesson 14
Lesson 14
Overview
of
Previous Lesson(s)
Over View
Algorithm for converting RE to an NFA .
3
Over View..
Method:
Inductive rules are for constructing NFA's for the immediate sub
expressions of a given expression.
4
Over View...
Basis Step:
5
Over View...
Induction Step:
Suppose N(s) and N(t) are NFA's for regular expressions s and t,
respectively.
6
Over View...
If r = st , Then N(r) , the NFA for r, should be constructed as
7
Over View...
If r = s* , Then N(r) , the NFA for r, should be constructed as
For r = (s) , L(r) = L(s) and we can use the NFA N(s) as N(r).
8
Over View...
Algorithms that have been used to implement and optimize
pattern matchers constructed from regular expressions.
The resulting DFA also may have fewer states than the DFA constructed
via an NFA.
9
Over View...
The second algorithm minimizes the number of states of any DFA,
by combining states that have the same future behavior.
The algorithm itself is quite efficient, running in time O(n log n),
where n is the number of states of the DFA.
10
Over View...
A state of an NFA can be declared as important if it has a non-ɛ out-
transition.
NFA has only one accepting state, but this state, having no out-
transitions, is not an important state.
11
Over View...
Syntax tree for (a|b)*abb#
12
TODAY’S LESSON
13
Contents
Optimization of DFA-Based Pattern Matchers
Important States of an NFA
Functions Computed From the Syntax Tree
Computing nullable, firstpos, and lastpos
Computing followups
Converting a RE Directly to DFA
Minimizing the Number of States of DFA
Trading Time for Space in DFA Simulation
Two dimensional Table
Terminologies
14
Functions Computed From the Syntax Tree
That is, the sub-expression can be "made null" or the empty string,
even though there may be other strings it can represent as well.
15
Functions Computed From the Syntax Tree..
16
Functions Computed From the Syntax Tree...
17
Functions Computed From the Syntax Tree…
Ex. Consider the cat-node n that corresponds to (a|b)*a
nullable(n) is false:
18
Functions Computed From the Syntax Tree…
firstpos(n) = {1,2,3}
19
Functions Computed From the Syntax Tree…
lastpos(n) = {3}
20
Computing nullable, firstpos, and lastpos
nullable, firstpos, and lastpos can be computed by a straight
forward recursion on the height of the tree.
21
Computing nullable, firstpos, and lastpos..
The rules for lastpos are essentially the same as for firstpos, but
the roles of children C1 and C2 must be swapped in the rule for a
cat-node.
22
Computing nullable, firstpos, and lastpos...
Ex.
nullable(n):
None of the leaves of are
nullable, because they each correspond
to non-ɛ operands.
The or-node is not nullable, because
neither of its children is.
The star-node is nullable, because
every star-node is nullable.
The cat-nodes, having at least
one non null able child, is
not nullable.
23
Computing nullable, firstpos, and lastpos...
Rule: if (nullable(C2))
firstpos(C2) U firstpos(C1)
else firstpos(C2)
24
Computing nullable, firstpos, and lastpos...
The computation of firstpos and lastpos for each of the nodes
provides the following result:
25
Computing followpos
If n is a cat-node with left child C1 and right child C2 then for every
position i in lastpos(C1) , all positions in firstpos(C2) are in
followpos(i).
26
Computing followpos..
Ex.
Starting from lowest cat node
lastpos(c1) = {1,2}
firstpos(c2) = {3}
27
Computing followpos...
28
Computing followpos...
29
Computing followpos...
followup for star node n
lastpos(n) = {1,2}
firstpos(n) = {1,2}
ȋ = 1,2
So, applying Rule 2 we got
30
Computing followpos…
31
Computing followpos…
32
Converting RE directly to DFA
Construct Dstates, the set of states of DFA D , and Dtran, the transition
function for D (Procedure). The states of D are sets of positions in T.
Initially, each state is "unmarked," and a state becomes "marked" just before
we consider its out-transitions.
The start state of D is firstpos(n0) , where node n0 is the root of T.
The accepting states are those containing the position for the endmarker
symbol #.
33
Converting RE directly to DFA..
Ex. DFA for the regular expression r = (a|b)*abb
Putting together all previous steps:
34
Converting RE directly to DFA…
35
Converting RE directly to DFA…
36
Thank You