annotate Implab/Parsing/DFABuilder.cs @ 159:5558e43c79bb v2

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