-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bunch.cs
285 lines (260 loc) · 7.72 KB
/
Bunch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
namespace Lex
{
/*
* Class: Bunch
*/
using System;
using System.Collections;
using BitSet;
public class Bunch
{
/*
* Member Variables
*/
ArrayList nfa_set; /* List of Nfa states in dfa state */
BitSet nfa_bit; /* BitSet representation of Nfa labels */
Accept accept; /* Accepting actions, or null if nonaccepting state */
int anchor; /* Anchors on regular expression */
int accept_index; /* Nfa index corresponding to accepting actions */
public ArrayList GetNFASet() { return nfa_set; }
public void SetNFASet(ArrayList a) { nfa_set = a; }
public BitSet GetNFABit() { return nfa_bit; }
public void SetNFABit(BitSet b) { nfa_bit = b; }
public Accept GetAccept() { return accept; }
public void SetAccept(Accept a) { accept = a; }
public int GetAnchor() { return anchor; }
public void SetAnchor(int a) { anchor = a; }
public int GetIndex() { return accept_index; }
public void SetIndex(int i) { accept_index = i; }
/*
* Function: Bunch
* Description: Constructor.
*/
public Bunch(ArrayList nfa_start_states)
{
int size = nfa_start_states.Count;
nfa_set = new ArrayList(nfa_start_states);
nfa_bit = new BitSet(size);
accept = null;
anchor = Spec.NONE;
/* Initialize bit set. */
for (int i = 0; i < size; i++)
{
int label = ((Nfa)nfa_set[i]).GetLabel();
nfa_bit.Set(label, true);
}
accept_index = Utility.INT_MAX;
}
public void dump()
{
#if DUMMY
Console.WriteLine("[CBunch Dump Begin]");
if (nfa_set == null)
Console.WriteLine("nfa_set=null");
else
{
int n1 = nfa_set.Count;
for (int i = 0; i < n1; i++)
{
Object o2 = nfa_set[i];
Console.Write("i="+Int32.ToString(i)+" elem=");
if (o2 == null)
Console.WriteLine("null");
else
{
CNfa elem = (CNfa) o2;
elem.dump();
}
}
}
if (nfa_bit == null)
Console.WriteLine("nfa_bit=null");
else
{
Console.Write("nfa_bit("+Int32.ToString(nfa_bit.GetLength())+")=");
for (int i = 0; i < nfa_bit.GetLength(); i++)
if (nfa_bit.Get(i))
Console.Write("1");
else
Console.Write("0");
Console.WriteLine("");
}
if (accept == null)
Console.WriteLine("accept=null");
else
accept.dump();
Console.WriteLine("anchor="+Int32.ToString(anchor));
Console.WriteLine("accept_index="+Int32.ToString(accept_index));
#endif
}
public bool IsEmpty()
{
return (nfa_set == null);
}
/*
* Function: e_closure
* Description: Alters input set.
*/
public void e_closure()
{
Nfa state = null;
/*
* Debug checks
*/
#if DEBUG
Utility.assert(null != nfa_set);
Utility.assert(null != nfa_bit);
#endif
accept = null;
anchor = Spec.NONE;
accept_index = Utility.INT_MAX;
/*
* Create initial stack.
*/
Stack nfa_stack = new Stack();
int size = nfa_set.Count;
for (int i = 0; i < size; i++)
{
state = (Nfa)nfa_set[i];
#if DEBUG
Utility.assert(nfa_bit.Get(state.GetLabel()));
#endif
nfa_stack.Push(state);
}
/*
* Main loop.
*/
while (nfa_stack.Count > 0)
{
Object o = nfa_stack.Pop();
if (o == null)
break;
state = (Nfa)o;
#if OLD_DUMP_DEBUG
if (null != state.GetAccept())
{
Console.WriteLine("Looking at accepting state "
+ state.GetLabel()
+ " with <"
+ state.GetAccept().action
+ ">");
}
#endif
if (null != state.GetAccept() && state.GetLabel() < accept_index)
{
accept_index = state.GetLabel();
accept = state.GetAccept();
anchor = state.GetAnchor();
#if OLD_DUMP_DEBUG
Console.WriteLine("Found accepting state "
+ state.GetLabel()
+ " with <"
+ state.GetAccept().action
+ ">");
#endif
#if DEBUG
Utility.assert(null != accept);
Utility.assert(Spec.NONE == anchor
|| 0 != (anchor & Spec.END)
|| 0 != (anchor & Spec.START));
#endif
}
if (Nfa.EPSILON == state.GetEdge())
{
if (state.GetNext() != null)
{
if (false == nfa_set.Contains(state.GetNext()))
{
#if DEBUG
Utility.assert(false == nfa_bit.Get(state.GetNext().GetLabel()));
#endif
nfa_bit.Set(state.GetNext().GetLabel(), true);
nfa_set.Add(state.GetNext());
nfa_stack.Push(state.GetNext());
}
}
if (null != state.GetSib())
{
if (false == nfa_set.Contains(state.GetSib()))
{
#if DEBUG
Utility.assert(false == nfa_bit.Get(state.GetSib().GetLabel()));
#endif
nfa_bit.Set(state.GetSib().GetLabel(), true);
nfa_set.Add(state.GetSib());
nfa_stack.Push(state.GetSib());
}
}
}
}
if (null != nfa_set)
sort_states();
}
private class NfaComp : IComparer
{
public int Compare(Object x, Object y)
{
Nfa a = (Nfa)x;
Nfa b = (Nfa)y;
return a.GetLabel() - b.GetLabel();
}
}
/*
* Function: sort_states
*/
public void sort_states()
{
nfa_set.Sort(0, nfa_set.Count, null);
//nfa_set.Sort(0, nfa_set.Count, new NfaComp());
#if OLD_DEBUG
Console.Write("NFA vector indices: ");
for (int index = 0; index < nfa_set.Count; index++)
{
Nfa elem = (Nfa) nfa_set[index];
Console.Write(elem.GetLabel() + " ");
}
Console.Write("\n");
#endif
return;
}
/*
* Function: move
*/
public void move(Dfa dfa, int b)
{
int size;
Nfa state;
ArrayList old_set = dfa.GetNFASet();
nfa_set = null;
nfa_bit = null;
size = old_set.Count;
for (int index = 0; index < size; index++)
{
state = (Nfa)old_set[index];
if (b == state.GetEdge()
|| (Nfa.CCL == state.GetEdge() && state.GetCharSet().contains(b)))
{
if (nfa_set == null)
{
nfa_set = new ArrayList();
nfa_bit = new BitSet();
}
nfa_set.Add(state.GetNext());
#if OLD_DEBUG
Console.WriteLine("Size of bitset: " + nfa_bit.GetLength());
Console.WriteLine("Reference index: " + state.GetNext().GetLabel());
#endif
nfa_bit.Set(state.GetNext().GetLabel(), true);
}
}
if (nfa_set != null)
{
#if DEBUG
Utility.assert(null != nfa_bit);
#endif
sort_states();
}
return;
}
}
}