Aho-3 7
Aho-3 7
Aho-3 7
LEXICAL ANALYSIS
Exercise 3.6.4 : Repeat Exercise 3.6.3 for the NFA of Fig. 3.30.
Exercise 3.6.5 : Give the transition tables for the NFA of:
a) Exercise 3.6.3.
b) Exercise 3.6.4.
c) Figure 3.26.
ala2 - - .a,, the DFA is in that state which corresponds to the set of states that
the NFA can reach, from its start state, following paths labeled ala2 . . an.
It is possible that the number of DFA states is exponential in the number
of NFA states, which could lead to difficulties when we try to implement this
DFA. However, part of the power of the automaton-based approach to lexical
analysis is that for real languages, the NFA and DFA have approximately the
same number of states, and the exponential behavior is not seen.
We must explore those sets of states that N can be in after seeing some input
string. As a basis, before reading the first input symbol, N can be in any of the
states of E-closure(so),where so is its start state. For the induction, suppose
that N can be in set of states T after reading input string x. If it next reads
input a, then N can immediately go to any of the states in move(T, a). However,
after reading a, it may also make several €-transitions; thus N could be in any
state of e-closure(move(T, a)) after reading input xu. Following these ideas, the
construction of the set of D's states, Dstates, and its transition function Dtran,
is shown in Fig. 3.32.
The start state of D is c-closure(so), and the accepting states of D are all
those sets of N's states that include at least one accepting state of N . To
complete our description of the subset construction, we need only to show how
CHAPTER 3.- LEXICAL ANALYSIS
E-closure(T) is computed for any set of NFA states T. This process, shown in
Fig. 3.33, is a straightforward search in a graph from a set of states. In this
case, imagine that only the €-labeled edges are available in the graph.
Example 3.21 : Figure 3.34 shows another NFA accepting (a1b ) *abb; it hap-
pens to be the one we shall construct directly from this regular expression in
Section 3.7. Let us apply Algorithm 3.20 to Fig. 3.29.
The start state A of the equivalent DFA is E-closure(O),or A = {0,1,2,4,7),
since these are exactly the states reachable from state 0 via a path all of whose
edges have label e. Note that a path can have zero edges, so state 0 is reachable
from itself by an €-labeled path.
The input alphabet is {a, b). Thus, our first step is to mark A and compute
Dtran[A, a] = E-closure(moue(A,a)) and Dtran[A, b] = t- closure(moue(A, b)) .
Among the states 0, 1, 2, 4, and 7, only 2 and 7 have transitions on a, to
3 and 8, respectively. Thus, move(A, a) = {3,8). Also, t-closure({3,8) =
{1,2,3,4,6,7,8),so we conclude
3.7. FROM REGULAR EXPRESSIONS T O AUTOMATA
S = E-closure(so);
c = nextchar();
while ( c != eof ) {
S = E-closure (move(S, c)) ;
c = nextchar();
1
if ( S n F != 0 ) return Ityesll;
else return "no";