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