- B. Ward — Spring 2014
- NFA Example
1
NFA Example Input: a a - - PowerPoint PPT Presentation
NFA Example Input: a a B. Ward Spring 2014 1 NFA Example current state Input: a a
1
2
current input character
current state
3
current input character
current state
4
current input character
current state
5
current input character
current state
6
current input character
current state
7
current input character
current state
8
RE to NFA
NFA to DFA Optimization
9
10
➡Base case: a single character. ➡Concatenation: joining two REs in sequence. ➡Alternation: joining two REs in parallel. ➡Kleene Closure: repeating a RE.
11
12
13
14
ε ε ε ε
15
ε ε ε ε
16
ε ε ε ε
17
ε ε ε ε
18
ε ε ε ε
19
ε ε ε ε
ε ε ε ε
20
21
B A
ε ε ε ε
22
B A
23
S
A
ε ε ε ε
24
B A
25
➡Either in parallel (memory consumption!). ➡Or with backtracking (large trees!). ➡Both are impractical.
➡Instead of doing a specific parallel search each time that we
simulate the NFA, we do it only once in general.
can be reached.
➡These are the states that a parallel search would explore. ➡Create a DFA state + transitions for each such set. ➡Final states: a DFA state is a final state if its corresponding set
26
NFA-to-DFA-CONVERSION: todo: stack of sets of NFA states. push {NFA start state and all epsilon-reachable states} onto todo while (todo is not empty): curNFA: set of NFA states curDFA: a DFA state
mark curNFA as done
reachableNFA = find each state that is reachable from a state in curNFA via one x transition and any number of epsilon transitions
push reachableNFA onto todo reachableDFA = find or create DFA state corresponding to reachableNFA add transition on x from curDFA to reachableDFA end for end while
27
28
29
30
31
32
33
34
35
done
done
36
done
37
done
38
39
done
40
done done
41
done done
42
done done
43
done done
44
done done done
45
done done done
46
done done done
done
98
47
done done done done
48
done done done done done
49
done done done done done
50
done done done done done done
51
52
➡For each RE, the minimal DFA is unique (ignoring
simple renaming).
➡DFA minimization: merge states that are
equivalent.
➡Start with two partitions: final and non-final states. ➡Repeatedly split partitions until all partitions
contain only equivalent states.
➡Two states S1, S2 are equivalent if all their
transitions “agree,” i.e., if there exists an input symbol x such that the DFA transitions (on input x) to a state in partition P1 if in S1 and to state in partition P2 if in S2 and P1≠P2, then S1 and S2 are not equivalent.
➡For each RE, the minimal DFA is unique (ignoring
simple renaming).
➡DFA minimization: merge states that are
equivalent.
➡Start with two partitions: final and non-final states. ➡Repeatedly split partitions until all partitions
contain only equivalent states.
➡Two states S1, S2 are equivalent if all their
transitions “agree,” i.e., if there exists an input symbol x such that the DFA transitions (on input x) to a state in partition P1 if in S1 and to state in partition P2 if in S2 and P1≠P2, then S1 and S2 are not equivalent.
54
➡For each RE, the minimal DFA is unique (ignoring
simple renaming).
➡DFA minimization: merge states that are
equivalent.
➡Start with two partitions: final and non-final states. ➡Repeatedly split partitions until all partitions
contain only equivalent states.
➡Two states S1, S2 are equivalent if all their
transitions “agree,” i.e., if there exists an input symbol x such that the DFA transitions (on input x) to a state in partition P1 if in S1 and to state in partition P2 if in S2 and P1≠P2, then S1 and S2 are not equivalent.
55
56
57
58
59
60
61
62
➡Create a NFA for each token type RE as before. ➡Join all token NFAs as shown below:
63
Type 1
ε ε
Type 2
Type N
ε
➡Create a NFA for each token type RE as before. ➡Join all token NFAs as shown below:
64
Type 1
ε ε
Type 2
Type N
ε
65
66
67
68
69
70
71
72
73
74
75
76
77
➡Example: report (sum), ((sum)) and (((sum)))
➡But not: ((sum)
78