@@ -1,262 +1,264 | |||
|
1 | 1 | using Implab; |
|
2 | 2 | using System; |
|
3 | 3 | using System.Collections.Generic; |
|
4 | 4 | using System.Diagnostics; |
|
5 | 5 | using System.Linq; |
|
6 | 6 | |
|
7 | 7 | namespace Implab.Parsing { |
|
8 | 8 | public class DFADefinition : IDFADefinition { |
|
9 | 9 | readonly List<DFAStateDescriptior> m_states; |
|
10 | 10 | |
|
11 | 11 | public const int INITIAL_STATE = 1; |
|
12 | 12 | public const int UNREACHEBLE_STATE = 0; |
|
13 | 13 | |
|
14 | 14 | DFAStateDescriptior[] m_statesArray; |
|
15 | 15 | readonly int m_alpabetSize; |
|
16 | 16 | |
|
17 | 17 | public DFADefinition(int alphabetSize) { |
|
18 | 18 | m_states = new List<DFAStateDescriptior>(); |
|
19 | 19 | m_alpabetSize = alphabetSize; |
|
20 | 20 | |
|
21 | 21 | m_states.Add(new DFAStateDescriptior()); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | public DFAStateDescriptior[] States { |
|
25 | 25 | get { |
|
26 | 26 | if (m_statesArray == null) |
|
27 | 27 | m_statesArray = m_states.ToArray(); |
|
28 | 28 | return m_statesArray; |
|
29 | 29 | } |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | public bool InitialStateIsFinal { |
|
33 | 33 | get { |
|
34 | 34 | return m_states[INITIAL_STATE].final; |
|
35 | 35 | } |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | public int AddState() { |
|
39 | 39 | var index = m_states.Count; |
|
40 | 40 | m_states.Add(new DFAStateDescriptior { |
|
41 | 41 | final = false, |
|
42 | 42 | transitions = new int[AlphabetSize] |
|
43 | 43 | }); |
|
44 | 44 | m_statesArray = null; |
|
45 | 45 | |
|
46 | 46 | return index; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | public int AddState(int[] tag) { |
|
50 | 50 | var index = m_states.Count; |
|
51 | 51 | bool final = tag != null && tag.Length != 0; |
|
52 | 52 | m_states.Add(new DFAStateDescriptior { |
|
53 | 53 | final = final, |
|
54 | 54 | transitions = new int[AlphabetSize], |
|
55 | 55 | tag = final ? tag : null |
|
56 | 56 | }); |
|
57 | 57 | m_statesArray = null; |
|
58 | 58 | return index; |
|
59 | 59 | } |
|
60 | 60 | |
|
61 | 61 | public void DefineTransition(int s1,int s2, int symbol) { |
|
62 | 62 | Safe.ArgumentInRange(s1, 0, m_states.Count-1, "s1"); |
|
63 | 63 | Safe.ArgumentInRange(s2, 0, m_states.Count-1, "s2"); |
|
64 | 64 | Safe.ArgumentInRange(symbol, 0, AlphabetSize-1, "symbol"); |
|
65 | 65 | |
|
66 | 66 | m_states[s1].transitions[symbol] = s2; |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | public void Optimize<TA>(IDFADefinition minimalDFA,IAlphabet<TA> sourceAlphabet, IAlphabet<TA> minimalAlphabet) { |
|
70 | 70 | Safe.ArgumentNotNull(minimalDFA, "minimalDFA"); |
|
71 | 71 | Safe.ArgumentNotNull(minimalAlphabet, "minimalAlphabet"); |
|
72 | 72 | |
|
73 | 73 | var setComparer = new CustomEqualityComparer<HashSet<int>>( |
|
74 | 74 | (x, y) => x.SetEquals(y), |
|
75 | 75 | (s) => s.Sum(x => x.GetHashCode()) |
|
76 | 76 | ); |
|
77 | 77 | |
|
78 | 78 | var arrayComparer = new CustomEqualityComparer<int[]>( |
|
79 | 79 | (x,y) => (new HashSet<int>(x)).SetEquals(new HashSet<int>(y)), |
|
80 | 80 | (a) => a.Sum(x => x.GetHashCode()) |
|
81 | 81 | ); |
|
82 | 82 | |
|
83 | 83 | var optimalStates = new HashSet<HashSet<int>>(setComparer); |
|
84 | 84 | var queue = new HashSet<HashSet<int>>(setComparer); |
|
85 | 85 | |
|
86 | 86 | foreach (var g in Enumerable |
|
87 | 87 | .Range(INITIAL_STATE, m_states.Count-1) |
|
88 | 88 | .Select(i => new { |
|
89 | 89 | index = i, |
|
90 | 90 | descriptor = m_states[i] |
|
91 | 91 | }) |
|
92 | 92 | .Where(x => x.descriptor.final) |
|
93 | 93 | .GroupBy(x => x.descriptor.tag, arrayComparer) |
|
94 | 94 | ) { |
|
95 | 95 | optimalStates.Add(new HashSet<int>(g.Select(x => x.index))); |
|
96 | 96 | } |
|
97 | 97 | |
|
98 | 98 | var state = new HashSet<int>( |
|
99 | 99 | Enumerable |
|
100 | 100 | .Range(INITIAL_STATE, m_states.Count - 1) |
|
101 | 101 | .Where(i => !m_states[i].final) |
|
102 | 102 | ); |
|
103 | 103 | optimalStates.Add(state); |
|
104 | 104 | queue.Add(state); |
|
105 | 105 | |
|
106 | 106 | while (queue.Count > 0) { |
|
107 | 107 | var stateA = queue.First(); |
|
108 | 108 | queue.Remove(stateA); |
|
109 | 109 | |
|
110 | 110 | for (int c = 0; c < AlphabetSize; c++) { |
|
111 | 111 | var stateX = new HashSet<int>(); |
|
112 | 112 | |
|
113 | 113 | for(int s = 1; s < m_states.Count; s++) { |
|
114 | 114 | if (stateA.Contains(m_states[s].transitions[c])) |
|
115 | 115 | stateX.Add(s); |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | foreach (var stateY in optimalStates.ToArray()) { |
|
119 | 119 | if (stateX.Overlaps(stateY) && !stateY.IsSubsetOf(stateX)) { |
|
120 | 120 | var stateR1 = new HashSet<int>(stateY); |
|
121 | 121 | var stateR2 = new HashSet<int>(stateY); |
|
122 | 122 | |
|
123 | 123 | stateR1.IntersectWith(stateX); |
|
124 | 124 | stateR2.ExceptWith(stateX); |
|
125 | 125 | |
|
126 | 126 | optimalStates.Remove(stateY); |
|
127 | 127 | optimalStates.Add(stateR1); |
|
128 | 128 | optimalStates.Add(stateR2); |
|
129 | 129 | |
|
130 | 130 | if (queue.Contains(stateY)) { |
|
131 | 131 | queue.Remove(stateY); |
|
132 | 132 | queue.Add(stateR1); |
|
133 | 133 | queue.Add(stateR2); |
|
134 | 134 | } else { |
|
135 | 135 | queue.Add(stateR1.Count <= stateR2.Count ? stateR1 : stateR2); |
|
136 | 136 | } |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | } |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | // ΡΡΡΠΎΠΈΠΌ ΠΊΠ°ΡΡΡ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΠΈΡ ΠΎΠΏΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΠΉ Ρ ΠΎΡΠΈΠ³ΠΈΠ½Π°Π»ΡΠ½ΡΠΌΠΈ |
|
143 | 143 | |
|
144 | 144 | var initialState = optimalStates.Single(x => x.Contains(INITIAL_STATE)); |
|
145 | 145 | |
|
146 | 146 | // ΠΊΠ°ΡΡΠ° ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΡ ΠΎΠΏΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ³ΠΎ ΡΠΎΡΡΠΎΡΠ½ΠΈΡ ΠΏΠΎ ΡΠΎΠΎΡΠ²Π΅ΡΡΠ²ΡΡΡΠ΅ΠΌΡ Π΅ΠΌΡ ΠΏΡΠΎΡΡΠΎΠΌΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΡ |
|
147 | 147 | int[] reveseOptimalMap = new int[m_states.Count]; |
|
148 | 148 | // ΠΊΠ°ΡΡΠ° Ρ ΠΈΠ½Π΄Π΅ΠΊΡΠ°ΠΌΠΈ ΠΎΠΏΡΠΈΠΌΠ°Π»ΡΠ½ΡΡ ΡΠΎΡΡΠΎΡΠ½ΠΈΠΉ |
|
149 | 149 | HashSet<int>[] optimalMap = new HashSet<int>[optimalStates.Count + 1]; |
|
150 | 150 | { |
|
151 | 151 | optimalMap[0] = new HashSet<int>(); // unreachable state |
|
152 | 152 | optimalMap[1] = initialState; // initial state |
|
153 | 153 | foreach (var ss in initialState) |
|
154 | 154 | reveseOptimalMap[ss] = 1; |
|
155 | 155 | |
|
156 | 156 | int i = 2; |
|
157 | 157 | foreach (var s in optimalStates) { |
|
158 | 158 | if (s.SetEquals(initialState)) |
|
159 | 159 | continue; |
|
160 | 160 | optimalMap[i] = s; |
|
161 | 161 | foreach (var ss in s) |
|
162 | 162 | reveseOptimalMap[ss] = i; |
|
163 | 163 | i++; |
|
164 | 164 | } |
|
165 | 165 | } |
|
166 | 166 | |
|
167 | 167 | // ΠΏΠΎΠ»ΡΡΠ°Π΅ΠΌ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΠΉ Π°Π»ΡΠ°Π²ΠΈΡ |
|
168 | 168 | |
|
169 | 169 | var minClasses = new HashSet<HashSet<int>>(setComparer); |
|
170 | 170 | var alphaQueue = new Queue<HashSet<int>>(); |
|
171 | 171 | alphaQueue.Enqueue(new HashSet<int>(Enumerable.Range(0,AlphabetSize))); |
|
172 | 172 | |
|
173 | 173 | for (int s = 1 ; s < optimalMap.Length; s++) { |
|
174 | 174 | var newQueue = new Queue<HashSet<int>>(); |
|
175 | 175 | |
|
176 | 176 | foreach (var A in alphaQueue) { |
|
177 | 177 | if (A.Count == 1) { |
|
178 | 178 | minClasses.Add(A); |
|
179 | 179 | continue; |
|
180 | 180 | } |
|
181 | 181 | |
|
182 | 182 | // ΡΠ°Π·Π»ΠΈΡΠ°Π΅ΠΌ ΠΊΠ»Π°ΡΡΡ ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΡΡ Π² ΡΠ°Π·Π»ΠΈΡΠ½ΡΠ΅ ΠΎΠΏΡΠΈΠΌΠ°Π»ΡΠ½ΡΠ΅ ΡΠΎΡΡΠΎΡΠ½ΠΈΡ |
|
183 | 183 | // optimalState -> alphaClass |
|
184 | 184 | var classes = new Dictionary<int, HashSet<int>>(); |
|
185 | 185 | |
|
186 | 186 | foreach (var term in A) { |
|
187 | 187 | // ΠΈΡΠ΅ΠΌ Π²ΡΠ΅ ΠΏΠ΅ΡΠ΅Ρ ΠΎΠ΄Ρ ΠΊΠ»Π°ΡΡΠ° ΠΏΠΎ ΡΠΈΠΌΠ²ΠΎΠ»Ρ term |
|
188 | 188 | var s2 = reveseOptimalMap[ |
|
189 | 189 | optimalMap[s].Select(x => m_states[x].transitions[term]).FirstOrDefault(x => x != 0) // ΠΏΠ΅ΡΠ²ΠΎΠ΅ Π΄ΠΎΠΏΡΡΡΠΈΠΌΠΎΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΡΠ½ΠΎΠ΅ ΡΠΎΡΡΠΎΡΠ½ΠΈΠ΅, Π΅ΡΠ»ΠΈ Π΅ΡΡΡ |
|
190 | 190 | ]; |
|
191 | 191 | |
|
192 | 192 | HashSet<int> A2; |
|
193 | 193 | if (!classes.TryGetValue(s2, out A2)) { |
|
194 | 194 | A2 = new HashSet<int>(); |
|
195 | 195 | newQueue.Enqueue(A2); |
|
196 | 196 | classes[s2] = A2; |
|
197 | 197 | } |
|
198 | 198 | A2.Add(term); |
|
199 | 199 | } |
|
200 | 200 | } |
|
201 | 201 | |
|
202 | 202 | if (newQueue.Count == 0) |
|
203 | 203 | break; |
|
204 | 204 | alphaQueue = newQueue; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | foreach (var A in alphaQueue) |
|
208 | 208 | minClasses.Add(A); |
|
209 | 209 | |
|
210 | 210 | var alphabetMap = sourceAlphabet.Reclassify(minimalAlphabet, minClasses); |
|
211 | 211 | |
|
212 | 212 | // ΠΏΠΎΡΡΡΠΎΠ΅Π½ΠΈΠ΅ Π°Π²ΡΠΎΠΌΠ°ΡΠ° |
|
213 | 213 | |
|
214 | 214 | var states = new int[ optimalMap.Length ]; |
|
215 | 215 | states[0] = UNREACHEBLE_STATE; |
|
216 | 216 | |
|
217 | 217 | for(var s = INITIAL_STATE; s < states.Length; s++) { |
|
218 | 218 | var tags = optimalMap[s].SelectMany(x => m_states[x].tag ?? Enumerable.Empty<int>()).Distinct().ToArray(); |
|
219 | 219 | if (tags.Length > 0) |
|
220 | 220 | states[s] = minimalDFA.AddState(tags); |
|
221 | 221 | else |
|
222 | 222 | states[s] = minimalDFA.AddState(); |
|
223 | 223 | } |
|
224 | 224 | |
|
225 | 225 | Debug.Assert(states[INITIAL_STATE] == INITIAL_STATE); |
|
226 | 226 | |
|
227 | 227 | for (int s1 = 1; s1 < m_states.Count; s1++) { |
|
228 | 228 | for (int c = 0; c < AlphabetSize; c++) { |
|
229 | 229 | var s2 = m_states[s1].transitions[c]; |
|
230 | 230 | if (s2 != UNREACHEBLE_STATE) { |
|
231 | 231 | minimalDFA.DefineTransition( |
|
232 | 232 | reveseOptimalMap[s1], |
|
233 | 233 | reveseOptimalMap[s2], |
|
234 | 234 | alphabetMap[c] |
|
235 | 235 | ); |
|
236 | 236 | } |
|
237 | 237 | } |
|
238 | 238 | } |
|
239 | 239 | |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | public void PrintDFA<TA>(IAlphabet<TA> alphabet) { |
|
243 | 243 | |
|
244 | 244 | var reverseMap = alphabet.CreateReverseMap(); |
|
245 | 245 | |
|
246 | 246 | for (int i = 1; i < reverseMap.Length; i++) { |
|
247 | 247 | Console.WriteLine("C{0}: {1}", i, String.Join(",", reverseMap[i])); |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | for (int i = 1; i < m_states.Count; i++) { |
|
251 | 251 | var s = m_states[i]; |
|
252 | 252 | for (int c = 0; c < AlphabetSize; c++) |
|
253 | 253 | if (s.transitions[c] != UNREACHEBLE_STATE) |
|
254 | 254 | Console.WriteLine("S{0} -{1}-> S{2}{3}", i, String.Join(",", reverseMap[c]), s.transitions[c], m_states[s.transitions[c]].final ? "$" : ""); |
|
255 | 255 | } |
|
256 | 256 | } |
|
257 | 257 | |
|
258 | 258 | public int AlphabetSize { |
|
259 |
get |
|
|
259 | get { | |
|
260 | return m_alpabetSize; | |
|
260 | 261 | } |
|
261 | 262 | } |
|
262 | 263 | } |
|
264 | } |
General Comments 0
You need to be logged in to leave comments.
Login now