Download - Chapter 3. Lexical Analysis (2)
![Page 1: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/1.jpg)
Chapter 3.
Lexical Analysis (2)
![Page 2: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/2.jpg)
2
Nondeterministic Finite Automata
A nondeterministic finite automaton(NFA) is a mathematical model that consists of
1. a set of state S2. 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
![Page 3: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/3.jpg)
3
STATEINPUT SYMBOL
a b
0
1
2
{0, 1}
--
{0}
{2}
{3}
Fig. 3.20. Transition table for the finite automaton of Fig. 3.19.
b
0start
1a
2b b
103
a
Fig. 3.19. A nondeterministic finite automaton.
![Page 4: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/4.jpg)
4
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.
![Page 5: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/5.jpg)
5
Fig. 3.21. NFA accepting aa* |bb*.
0start
1
a102
a
3
b104
b
![Page 6: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/6.jpg)
6
Fig. 3.23. DFA accepting (a|b)*abb.
0start
1a
2b b
103
b
a a
a
b
![Page 7: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/7.jpg)
7
Fig. 3.24. Operations on NFA states.
OPERATION DESCRIPTION
-closure(s) Set of NFA states reachable from NFA state s on -transitions alone.
-closure(T) Set of NFA states reachable from some NFA state s in T on -transitions alone.
move(T, a) Set of NFA states to which there is a transition on input symbol a from some NFA state s in T.
![Page 8: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/8.jpg)
8
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}
![Page 9: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/9.jpg)
9
Fig. 3.27. NFA N for (a|b)*abb.
1start
2
1010
a3
4 5
0 6
b
87 9 a b b
![Page 10: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/10.jpg)
10
Fig. 3.28. Translation table Dtran for DFA.
STATEINPUT SYMBOL
a b
A
B
C
D
E
B
B
B
B
B
C
D
C
E
C
![Page 11: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/11.jpg)
11
Fig. 3.29. Result of applying the subset construction of Fig. 3.27.
Astart
Ba
Db b
10E
b
a a
a
bC
b
a
![Page 12: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/12.jpg)
12
Thompson’s construction (1/2)
1. For , construct the NFA
2. For a in , construct the NFA
3. 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):
starti 10f
starti 10f
a
i
start10f
N(s)
N(t)
![Page 13: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/13.jpg)
13
Thompson’s construction (2/2)
b) For the regular expression st, construct the composite NFA N(st):
c) For the regular expression s*, construct the composite NFA N(s*):
d) For the parenthesized regular expression (s), use N(s) itself as the NFA.
istart
10fN(s) N(t)
N(s)istart
10f
![Page 14: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/14.jpg)
14
Fig. 3.32. Space and time taken to recognize regular expressions.
AUTOMATON SPACE TIME
NFA
DFA
O(|r|)
O(2|r|)
O(|r||x|)
O(|x|)
![Page 15: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/15.jpg)
15
Fig. 3.35. NFA recognizing three different patterns.
4
1start a
102
3start a
1065b b
7start b
108
ba
4
1
start
a102
3a
1065b b
7b
108
ba
0
(a) NFA for a, abb, and a*b+.
(b) Combined NFA.
![Page 16: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/16.jpg)
16
Fig. 3. 38. NFA recognizing Fortran keyword IF
2start
1I
3F (
0
4)
1065letter
any
![Page 17: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/17.jpg)
17
Fig. 3.41. firstpos and lastpos for nodes in syntax tree for (a|b)*abb#.
{1,2,3} {6}
{1,2,3} {5}
{1,2,3} {4}
{1,2,3} {3}
{1,2} {1,2}*
{6} # {6}
{5} b {5}
{4} b {4}
{3} a {3}
{1,2} {1,2}|
{2} b {2}{1} a {1}
![Page 18: Chapter 3. Lexical Analysis (2)](https://reader036.vdocuments.us/reader036/viewer/2022081421/56813198550346895d980c61/html5/thumbnails/18.jpg)
18
Fig. 3.42. The function followpos.
NODE followpos
1
2
3
4
5
6
{1, 2, 3}
{1, 2, 3}
{4}
{5}
{6}
-