annotate Implab/Automaton/RegularExpressions/RegularExpressionVisitor.cs @ 177:a0ff6a0e9c44 ref20160224

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