1 using System;
2 using System.Collections;
3
4
5
6
7
8 namespace System.RegularExpressions
9 {
10 public List<NfaState*> EpsilonClosure(const List<NfaState*>& states)
11 {
12 List<NfaState*> stack;
13 for (NfaState* state : states)
14 {
15 stack.Add(state);
16 }
17 List<NfaState*> epsilonClosure = states;
18 while (!stack.IsEmpty())
19 {
20 NfaState* s = stack.Back();
21 stack.RemoveLast();
22 List<NfaState*> u = s->Next(eps);
23 for (NfaState* v : u)
24 {
25 if (Find(epsilonClosure.CBegin(), epsilonClosure.CEnd(), v) == epsilonClosure.CEnd())
26 {
27 epsilonClosure.Add(v);
28 stack.Add(v);
29 }
30 }
31 }
32 return epsilonClosure;
33 }
34 public List<NfaState*> EpsilonClosure(NfaState* state)
35 {
36 List<NfaState*> states(1, state);
37 return EpsilonClosure(states);
38 }
39 public List<NfaState*> Move(const List<NfaState*>& states, uchar c)
40 {
41 List<NfaState*> next;
42 for (NfaState* state : states)
43 {
44 List<NfaState*> n = state->Next(c);
45 for (NfaState* s : n)
46 {
47 if (Find(next.CBegin(), next.CEnd(), s) == next.CEnd())
48 {
49 next.Add(s);
50 }
51 }
52 }
53 return next;
54 }
55 public bool Match(Nfa& nfa, const ustring& s)
56 {
57 List<NfaState*> states = EpsilonClosure(nfa.Start());
58 for (uchar c : s)
59 {
60 states = EpsilonClosure(Move(states, c));
61 }
62 for (NfaState* state : states)
63 {
64 if (state->Accept()) return true;
65 }
66 return false;
67 }
68 }