1 using System;
2 using System.Collections;
3
4
5
6
7
8 namespace soulcm.scmlg
9 {
10 public class DfaState
11 {
12 public DfaState(int id_, const List<NfaState*>& nfaStates_) :
13 id(id_), nfaStates(nfaStates_), next(), marked(false), accept(false), statementIndex(-1)
14 {
15 }
16 public int Id() const
17 {
18 return id;
19 }
20 public bool IsMarked() const
21 {
22 return marked;
23 }
24 public void Mark()
25 {
26 marked = true;
27 }
28 public bool Accept() const
29 {
30 return accept;
31 }
32 public void SetAccept(bool accept_)
33 {
34 accept = accept_;
35 }
36 public int StatementIndex() const
37 {
38 return statementIndex;
39 }
40 public void SetStatementIndex(int index)
41 {
42 statementIndex = index;
43 }
44 public const List<NfaState*>& NfaStates() const
45 {
46 return nfaStates;
47 }
48 public void AddNext(DfaState* n)
49 {
50 next.Add(n);
51 }
52 public DfaState* Next(int i) const
53 {
54 if (i >= 0 && i < next.Count())
55 {
56 return next[i];
57 }
58 else
59 {
60 return null;
61 }
62 }
63 private int id;
64 private List<NfaState*> nfaStates;
65 private List<DfaState*> next;
66 private bool marked;
67 private bool accept;
68 private int statementIndex;
69 }
70 public class Dfa
71 {
72 public const List<DfaState*>& States() const
73 {
74 return states;
75 }
76 public void AddState(DfaState* state)
77 {
78 states.Add(state);
79 }
80 public void Finalize()
81 {
82 for (DfaState* state : states)
83 {
84 for (NfaState* nfaState : state->NfaStates())
85 {
86 if (nfaState->Accept())
87 {
88 state->SetAccept(true);
89 if (state->StatementIndex() == -1 || (nfaState->StatementIndex() != -1 && nfaState->StatementIndex() < state->StatementIndex()))
90 {
91 state->SetStatementIndex(nfaState->StatementIndex());
92 }
93 }
94 }
95 }
96 }
97 private List<DfaState*> states;
98 }
99 }