annotate Implab/Automaton/RegularExpressions/RegularExpressionVisitor.cs @ 173:ecfece82ca11 ref20160224

Working on text scanner
author cin
date Tue, 15 Mar 2016 02:11:06 +0300
parents 92d5278d1b10
children a0ff6a0e9c44
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;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
15 Token<TTag> m_root;
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>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
21 readonly Dictionary<int, TTag> m_ends = new Dictionary<int, TTag>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
22
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
23 public Dictionary<int, HashSet<int>> FollowposMap {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
24 get { return m_followpos; }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
25 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
26
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
27 public HashSet<int> Followpos(int pos) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
28 HashSet<int> set;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
29 return m_followpos.TryGetValue(pos, out set) ? set : m_followpos[pos] = new HashSet<int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
30 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
31
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
32 bool Nullable(object n) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
33 if (n is EmptyToken<TTag> || n is StarToken<TTag>)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
34 return true;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
35 var altToken = n as AltToken<TTag>;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
36 if (altToken != null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
37 return Nullable(altToken.Left) || Nullable(altToken.Right);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
38 var catToken = n as CatToken<TTag>;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
39 if (catToken != null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
40 return Nullable(catToken.Left) && Nullable(catToken.Right);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
41 return false;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
42 }
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 public void Visit(AltToken<TTag> token) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
46 if (m_root == null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
47 m_root = token;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
48 var firtspos = new HashSet<int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
49 var lastpos = new HashSet<int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
50
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
51 token.Left.Accept(this);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
52 firtspos.UnionWith(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
53 lastpos.UnionWith(m_lastpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
54
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
55 token.Right.Accept(this);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
56 firtspos.UnionWith(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
57 lastpos.UnionWith(m_lastpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
58
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
59 m_firstpos = firtspos;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
60 m_lastpos = lastpos;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
61 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
62
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
63 public void Visit(StarToken<TTag> token) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
64 if (m_root == null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
65 m_root = token;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
66 token.Token.Accept(this);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
67
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
68 foreach (var i in m_lastpos)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
69 Followpos(i).UnionWith(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
70 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
71
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
72 public void Visit(CatToken<TTag> token) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
73 if (m_root == null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
74 m_root = token;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
75
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
76 var firtspos = new HashSet<int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
77 var lastpos = new HashSet<int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
78 token.Left.Accept(this);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
79 firtspos.UnionWith(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
80 var leftLastpos = m_lastpos;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
81
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
82 token.Right.Accept(this);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
83 lastpos.UnionWith(m_lastpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
84 var rightFirstpos = m_firstpos;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
85
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
86 if (Nullable(token.Left))
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
87 firtspos.UnionWith(rightFirstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
88
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
89 if (Nullable(token.Right))
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
90 lastpos.UnionWith(leftLastpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
91
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
92 m_firstpos = firtspos;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
93 m_lastpos = lastpos;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
94
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
95 foreach (var i in leftLastpos)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
96 Followpos(i).UnionWith(rightFirstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
97
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 public void Visit(EmptyToken<TTag> token) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
101 if (m_root == null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
102 m_root = token;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
103 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
104
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
105 public void Visit(SymbolToken<TTag> token) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
106 if (m_root == null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
107 m_root = token;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
108 m_idx++;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
109 m_indexes[m_idx] = token.Value;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
110 m_firstpos = new HashSet<int>(new[] { m_idx });
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
111 m_lastpos = new HashSet<int>(new[] { m_idx });
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
112 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
113
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
114 public void Visit(EndToken<TTag> token) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
115 if (m_root == null)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
116 m_root = token;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
117 m_idx++;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
118 m_indexes[m_idx] = DFAConst.UNCLASSIFIED_INPUT;
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
119 m_firstpos = new HashSet<int>(new[] { m_idx });
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
120 m_lastpos = new HashSet<int>(new[] { m_idx });
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
121 Followpos(m_idx);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
122 m_ends.Add(m_idx, token.Tag);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
123 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
124
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
125 public void BuildDFA(ITaggedDFABuilder<TTag> dfa) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
126 Safe.ArgumentNotNull(dfa,"dfa");
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
127
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
128 var states = new MapAlphabet<HashSet<int>>(
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
129 false,
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
130 new CustomEqualityComparer<HashSet<int>>(
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
131 (x, y) => x.SetEquals(y),
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
132 x => x.Sum(n => n.GetHashCode())
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
133 ));
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
134
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
135 var initialState = states.DefineSymbol(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
136 dfa.SetInitialState(initialState);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
137
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
138 var tags = GetStateTags(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
139 if (tags != null && tags.Length > 0)
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
140 dfa.MarkFinalState(initialState, tags);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
141
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
142 var inputMax = m_indexes.Values.Max();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
143 var queue = new Queue<HashSet<int>>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
144
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
145 queue.Enqueue(m_firstpos);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
146
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
147 while (queue.Count > 0) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
148 var state = queue.Dequeue();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
149 var s1 = states.Translate(state);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
150 Debug.Assert(s1 != DFAConst.UNCLASSIFIED_INPUT);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
151
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
152 for (int a = 0; a <= inputMax; a++) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
153 var next = new HashSet<int>();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
154 foreach (var p in state) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
155 if (m_indexes[p] == a) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
156 next.UnionWith(Followpos(p));
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
157 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
158 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
159 if (next.Count > 0) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
160 int s2 = states.Translate(next);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
161 if (s2 == DFAConst.UNCLASSIFIED_INPUT) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
162 s2 = states.DefineSymbol(next);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
163
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
164 tags = GetStateTags(next);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
165 if (tags != null && tags.Length > 0) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
166 dfa.MarkFinalState(s2);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
167 dfa.SetStateTag(s2, tags);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
168 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
169
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
170 queue.Enqueue(next);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
171 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
172 dfa.Add(new AutomatonTransition(s1, s2, a));
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
173 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
174 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
175 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
176 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
177
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
178 TTag[] GetStateTags(IEnumerable<int> state) {
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
179 Debug.Assert(state != null);
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
180 return state.Where(m_ends.ContainsKey).Select(pos => m_ends[pos]).ToArray();
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
181 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
182
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
183 }
92d5278d1b10 Working on text scanner
cin
parents:
diff changeset
184 }