/
DfaState.cs
284 lines (263 loc) · 7.22 KB
/
DfaState.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
using System.Diagnostics;
using System.Text;
using Cyjb.Collections.ObjectModel;
namespace Cyjb.Compilers.Lexers;
/// <summary>
/// 表示确定有穷自动机(DFA)的状态。
/// </summary>
[DebuggerTypeProxy(typeof(DebugView))]
public sealed class DfaState : ReadOnlyCollectionBase<DfaState>
{
/// <summary>
/// 包含当前状态的 DFA。
/// </summary>
private readonly Dfa dfa;
/// <summary>
/// DFA 状态的转移。
/// </summary>
private readonly Dictionary<CharClass, DfaState> transitions = new();
/// <summary>
/// 初始化 <see cref="DfaState"/> 类的新实例。
/// </summary>
/// <param name="dfa">包含当前状态的 DFA。</param>
/// <param name="index">状态的索引。</param>
internal DfaState(Dfa dfa, int index)
{
this.dfa = dfa;
Index = index;
Symbols = Array.Empty<int>();
ConflictedSymbols = Array.Empty<int>();
}
/// <summary>
/// 获取当前状态的索引。
/// </summary>
public int Index { get; internal set; }
/// <summary>
/// 获取当前状态的符号列表,按符号索引排序。
/// </summary>
/// <remarks>使用负数表示向前看的头状态。</remarks>
public int[] Symbols { get; internal set; }
/// <summary>
/// 获取出现冲突时被忽略的符号列表,按符号索引排序。
/// </summary>
/// <remarks>使用负数表示向前看的头状态。</remarks>
public int[] ConflictedSymbols { get; internal set; }
/// <summary>
/// 获取指定字符类转移到的状态。
/// </summary>
/// <param name="charClass">要获取转移的字符类。</param>
/// <returns>转移到的状态。</returns>
public DfaState? this[CharClass charClass]
{
get
{
if (transitions.TryGetValue(charClass, out DfaState? state))
{
return state;
}
return null;
}
internal set
{
if (value == null)
{
transitions.Remove(charClass);
}
else
{
transitions[charClass] = value;
}
}
}
/// <summary>
/// 获取指定字符类索引转移到的状态。
/// </summary>
/// <param name="charClass">要获取转移的字符类索引。</param>
/// <returns>转移到的状态。</returns>
public DfaState? this[int charClass]
{
get
{
if (transitions.TryGetValue(dfa.CharClasses[charClass], out DfaState? state))
{
return state;
}
return null;
}
}
/// <summary>
/// 返回当前状态的转移可以覆盖其它状态的转移的个数。
/// </summary>
/// <param name="state">要检查的另一状态。</param>
/// <returns>当前状态可以覆盖其它状态的转移的个数。</returns>
internal int CountCoverTransition(DfaState state)
{
int coverCount = 0;
int sameCount = 0;
foreach (var (charClass, nextState) in transitions)
{
if (state.transitions.TryGetValue(charClass, out DfaState? target))
{
coverCount++;
if (target == nextState)
{
sameCount++;
}
}
}
// 未能覆盖 state 的全部状态转移,
if (coverCount < state.Count)
{
return 0;
}
return sameCount;
}
/// <summary>
/// 返回转移列表。
/// </summary>
/// <param name="defaultState">默认状态。</param>
/// <returns>转移列表。</returns>
internal KeyValuePair<int, int>[] GetTransitions(DfaState? defaultState)
{
IEnumerable<KeyValuePair<CharClass, DfaState>> transitions = this.transitions;
if (defaultState != null)
{
transitions = transitions.Where(pair =>
{
if (defaultState.transitions.TryGetValue(pair.Key, out DfaState? target))
{
return target != pair.Value;
}
else
{
return true;
}
});
}
return transitions.Select(pair => new KeyValuePair<int, int>(pair.Key.Index, pair.Value.Index))
.OrderBy(pair => pair.Key)
.ToArray();
}
/// <summary>
/// 更新状态。
/// </summary>
/// <param name="map">状态映射表。</param>
internal void UpdateState(Dictionary<DfaState, DfaState> map)
{
Dictionary<CharClass, DfaState> transitionMap = new();
foreach (var (charClass, state) in transitions)
{
if (map.TryGetValue(state, out DfaState? newState))
{
transitionMap[charClass] = newState;
}
}
foreach (var (charClass, state) in transitionMap)
{
transitions[charClass] = state;
}
}
/// <summary>
/// 移除指定的字符类。
/// </summary>
/// <param name="list">要移除的字符类集合。</param>
internal void RemoveCharClass(IEnumerable<CharClass> list)
{
foreach (CharClass charClass in list)
{
transitions.Remove(charClass);
}
}
/// <summary>
/// 返回符号信息。
/// </summary>
/// <returns>符号信息。</returns>
public string GetSymbolInfo()
{
// 输出对应的符号索引。
if (Symbols.Length > 0)
{
StringBuilder result = new();
result.Append(string.Join(", ", Symbols));
if (ConflictedSymbols.Length > 0)
{
result.Append(" conflict ");
result.Append(string.Join(", ", ConflictedSymbols));
}
return result.ToString();
}
else
{
return string.Empty;
}
}
/// <summary>
/// 返回当前对象的字符串表示形式。
/// </summary>
/// <returns>当前对象的字符串表示形式。</returns>
public override string ToString()
{
if (Symbols.Length == 0)
{
return $"State #{Index}";
}
else
{
return $"State #{Index} [{string.Join(",", Symbols)}]";
}
}
#region ReadOnlyCollectionBase<DFAState?> 成员
/// <summary>
/// 获取当前集合包含的元素数。
/// </summary>
/// <value>当前集合中包含的元素数。</value>
public override int Count => transitions.Count;
/// <summary>
/// 确定当前集合是否包含指定对象。
/// </summary>
/// <param name="item">要在当前集合中定位的对象。</param>
/// <returns>如果在当前集合中找到 <paramref name="item"/>,则为 <c>true</c>;否则为 <c>false</c>。</returns>
public override bool Contains(DfaState item)
{
if (item == null)
{
return false;
}
return transitions.ContainsValue(item);
}
/// <summary>
/// 返回一个循环访问集合的枚举器。
/// </summary>
/// <returns>可用于循环访问集合的 <see cref="IEnumerator{T}"/> 对象。</returns>
public override IEnumerator<DfaState> GetEnumerator()
{
return transitions.Values.GetEnumerator();
}
#endregion // ReadOnlyCollectionBase<DFAState?> 成员
#region 调试视图
/// <summary>
/// 调试视图。
/// </summary>
private sealed class DebugView
{
/// <summary>
/// 调试视图的源状态。
/// </summary>
private readonly DfaState state;
/// <summary>
/// 使用指定的源状态初始化 <see cref="DebugView"/> 类的实例。
/// </summary>
/// <param name="state">使用调试视图的源状态。</param>
public DebugView(DfaState state)
{
this.state = state;
}
/// <summary>
/// 获取源状态中的所有项。
/// </summary>
/// <value>包含了源状态中的所有项的数组。</value>
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public string[] Items => state.transitions.Select(pair => $"{pair.Key} -> {pair.Value}").ToArray();
}
#endregion // 调试视图
}