annotate Implab/Automaton/RegularExpressions/DFABuilder.cs @ 162:0526412bbb26 ref20160224

DFA refactoring
author cin
date Wed, 24 Feb 2016 08:39:53 +0300
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
1 using Implab;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
2 using System;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
3 using System.Collections.Generic;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
4 using System.Diagnostics;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
5 using System.Linq;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
6
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
7 namespace Implab.Automaton.RegularExpressions {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
8 /// <summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
9 /// Используется для построения ДКА по регулярному выражению, сначала обходит
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
10 /// регулярное выражение и вычисляет followpos, затем используется метод
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
11 /// <see cref="BuildDFA(IDFADefinition)"/> для построения автомата.
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
12 /// </summary>
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
13 public class DFABuilder<TTag> : IVisitor<TTag> {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
14 int m_idx = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
15 Token<TTag> m_root;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
16 HashSet<int> m_firstpos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
17 HashSet<int> m_lastpos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
18
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
19 readonly Dictionary<int, HashSet<int>> m_followpos = new Dictionary<int, HashSet<int>>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
20 readonly Dictionary<int, int> m_indexes = new Dictionary<int, int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
21 readonly Dictionary<int, TTag> m_ends = new Dictionary<int, TTag>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
22
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
23 public Dictionary<int, HashSet<int>> FollowposMap {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
24 get { return m_followpos; }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
25 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
26
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
27 public HashSet<int> Followpos(int pos) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
28 HashSet<int> set;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
29 if (m_followpos.TryGetValue(pos, out set))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
30 return set;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
31 return m_followpos[pos] = new HashSet<int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
32 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
33
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
34 bool Nullable(object n) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
35 if (n is EmptyToken<TTag> || n is StarToken<TTag>)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
36 return true;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
37 if (n is AltToken<TTag>)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
38 return Nullable(((AltToken<TTag>)n).Left) || Nullable(((AltToken<TTag>)n).Right);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
39 if (n is CatToken<TTag>)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
40 return Nullable(((CatToken<TTag>)n).Left) && Nullable(((CatToken<TTag>)n).Right);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
41 return false;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
42 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
43
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
44
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
45 public void Visit(AltToken<TTag> token) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
46 if (m_root == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
47 m_root = token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
48 var firtspos = new HashSet<int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
49 var lastpos = new HashSet<int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
50
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
51 token.Left.Accept(this);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
52 firtspos.UnionWith(m_firstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
53 lastpos.UnionWith(m_lastpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
54
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
55 token.Right.Accept(this);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
56 firtspos.UnionWith(m_firstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
57 lastpos.UnionWith(m_lastpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
58
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
59 m_firstpos = firtspos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
60 m_lastpos = lastpos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
61 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
62
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
63 public void Visit(StarToken<TTag> token) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
64 if (m_root == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
65 m_root = token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
66 token.Token.Accept(this);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
67
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
68 foreach (var i in m_lastpos)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
69 Followpos(i).UnionWith(m_firstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
70 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
71
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
72 public void Visit(CatToken<TTag> token) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
73 if (m_root == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
74 m_root = token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
75
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
76 var firtspos = new HashSet<int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
77 var lastpos = new HashSet<int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
78 token.Left.Accept(this);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
79 firtspos.UnionWith(m_firstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
80 var leftLastpos = m_lastpos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
81
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
82 token.Right.Accept(this);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
83 lastpos.UnionWith(m_lastpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
84 var rightFirstpos = m_firstpos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
85
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
86 if (Nullable(token.Left))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
87 firtspos.UnionWith(rightFirstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
88
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
89 if (Nullable(token.Right))
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
90 lastpos.UnionWith(leftLastpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
91
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
92 m_firstpos = firtspos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
93 m_lastpos = lastpos;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
94
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
95 foreach (var i in leftLastpos)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
96 Followpos(i).UnionWith(rightFirstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
97
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
98 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
99
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
100 public void Visit(EmptyToken<TTag> token) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
101 if (m_root == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
102 m_root = token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
103 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
104
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
105 public void Visit(SymbolToken<TTag> token) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
106 if (m_root == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
107 m_root = token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
108 m_idx++;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
109 m_indexes[m_idx] = token.Value;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
110 m_firstpos = new HashSet<int>(new[] { m_idx });
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
111 m_lastpos = new HashSet<int>(new[] { m_idx });
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
112 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
113
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
114 public void Visit(EndToken<TTag> token) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
115 if (m_root == null)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
116 m_root = token;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
117 m_idx++;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
118 m_indexes[m_idx] = DFAConst.UNCLASSIFIED_INPUT;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
119 m_firstpos = new HashSet<int>(new[] { m_idx });
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
120 m_lastpos = new HashSet<int>(new[] { m_idx });
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
121 Followpos(m_idx);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
122 m_ends.Add(m_idx, token.Tag);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
123 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
124
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
125 public void BuildDFA(IDFADefinitionBuilder<TTag> dfa, IAlphabetBuilder<int> states) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
126 Safe.ArgumentNotNull(dfa,"dfa");
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
127
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
128 var stateMap = new Dictionary<HashSet<int>, int>(new CustomEqualityComparer<HashSet<int>>(
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
129 (x, y) => x.SetEquals(y),
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
130 x => x.Sum(n => n.GetHashCode())
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
131 ));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
132
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
133 int nextState = 0;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
134
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
135 int initialState = states.DefineSymbol(nextState++);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
136 stateMap[m_firstpos] = initialState;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
137
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
138 var tags = GetStateTags(m_firstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
139 if (tags != null && tags.Length > 0)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
140 dfa.MarkFinalState(initialState, tags);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
141
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
142 var inputMax = m_indexes.Values.Max();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
143 var queue = new Queue<HashSet<int>>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
144
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
145 queue.Enqueue(m_firstpos);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
146
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
147 while (queue.Count > 0) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
148 var state = queue.Dequeue();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
149 var s1 = stateMap[state];
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
150
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
151 for (int a = 0; a <= inputMax; a++) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
152 var next = new HashSet<int>();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
153 foreach (var p in state) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
154 if (m_indexes[p] == a) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
155 next.UnionWith(Followpos(p));
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
156 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
157 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
158 if (next.Count > 0) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
159 int s2;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
160 if (!stateMap.TryGetValue(next, out s2)) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
161 s2 = states.DefineSymbol(nextState++);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
162 stateMap[next] = s2;
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
163 tags = GetStateTags(next);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
164 if (tags != null && tags.Length > 0)
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
165 dfa.MarkFinalState(s2, tags);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
166
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
167 queue.Enqueue(next);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
168 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
169 dfa.DefineTransition(s1, s2, a);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
170 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
171 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
172 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
173 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
174
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
175 TTag[] GetStateTags(IEnumerable<int> state) {
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
176 Debug.Assert(state != null);
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
177 return state.Where(m_ends.ContainsKey).Select(pos => m_ends[pos]).ToArray();
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
178 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
179
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
180 }
0526412bbb26 DFA refactoring
cin
parents:
diff changeset
181 }