180 likes | 321 Views
Chapter 3. Lexical Analysis (2). Nondeterministic Finite Automata. A nondeterministic finite automaton (NFA) is a mathematical model that consists of 1. a set of state S 2. a set of input symbols (the input symbol alphabet )
E N D
Nondeterministic Finite Automata • A nondeterministic finite automaton(NFA) is a mathematical model that consists of 1. a set of state S 2. a set of input symbols (the input symbol alphabet) 3. a transition function move that maps state-symbol pairs to sets of states 4. a state s0 that is distinguished as the start (or initial) state 5. a set of states F distinguished as accepting (or final) states
Fig. 3.19. A nondeterministic finite automaton. Fig. 3.20. Transition table for the finite automaton of Fig. 3.19.
Deterministic Finite Automata • A deterministic finite automata(DFA) is a special case of a nondeterministic finite automaton in which 1. no state has an -transition, i.e., a transition on input , and 2. for each state s and input symbol a, there is at most one edge labeled a leaving s.
Example 3.15 -closure(move(A, a)) -closure(move({0, 1, 2, 4, 7}, a)) = -closure({3, 8}) = {1, 2 , 3, 4, 6, 7, 8} C = -closure({5}) = {1, 2, 4, 5, 6, 7} • The five different sets of states are : A = {0, 1, 2, 4, 7} D = { 1, 2, 4, 5, 6, 7, 9} B = {1, 2, 3, 4, 6, 7, 8} E = { 1, 2, 4, 5, 6, 7, 10} C = {1, 2, 4, 5, 6, 7}
Fig. 3.29. Result of applying the subset construction of Fig. 3.27.
Thompson’s construction (1/2) • For , construct the NFA • For a in , construct the NFA • Suppose N(s) and N(t) are NFA’s for regular expressions s and t. a) For the regular expression s|t, construct the following composite NFA N(s|t):
Thompson’s construction (2/2) b) For the regular expression st, construct the composite NFA N(st): • For the regular expression s*, construct the composite NFA N(s*): • For the parenthesized regular expression (s), use N(s) itself as the NFA.
Fig. 3.32. Space and time taken to recognize regular expressions.
Fig. 3.35. NFA recognizing three different patterns. (a) NFA for a, abb, and a*b+. (b) Combined NFA.
Fig. 3.41. firstpos and lastpos for nodes in syntax tree for (a|b)*abb#.