annotate Implab/Parsing/DFABuilder.cs @ 107:f5220e5472ef v2

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