using Implab; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; namespace Implab.Automaton.RegularExpressions { /// /// Используется для построения ДКА по регулярному выражению, сначала обходит /// регулярное выражение и вычисляет followpos, затем используется метод /// для построения автомата. /// public class RegularDFABuilder : IVisitor { int m_idx; Token m_root; HashSet m_firstpos; HashSet m_lastpos; readonly Dictionary> m_followpos = new Dictionary>(); readonly Dictionary m_indexes = new Dictionary(); readonly Dictionary m_ends = new Dictionary(); public Dictionary> FollowposMap { get { return m_followpos; } } public HashSet Followpos(int pos) { HashSet set; return m_followpos.TryGetValue(pos, out set) ? set : m_followpos[pos] = new HashSet(); } bool Nullable(object n) { if (n is EmptyToken || n is StarToken) return true; var altToken = n as AltToken; if (altToken != null) return Nullable(altToken.Left) || Nullable(altToken.Right); var catToken = n as CatToken; if (catToken != null) return Nullable(catToken.Left) && Nullable(catToken.Right); return false; } public void Visit(AltToken token) { if (m_root == null) m_root = token; var firtspos = new HashSet(); var lastpos = new HashSet(); token.Left.Accept(this); firtspos.UnionWith(m_firstpos); lastpos.UnionWith(m_lastpos); token.Right.Accept(this); firtspos.UnionWith(m_firstpos); lastpos.UnionWith(m_lastpos); m_firstpos = firtspos; m_lastpos = lastpos; } public void Visit(StarToken token) { if (m_root == null) m_root = token; token.Token.Accept(this); foreach (var i in m_lastpos) Followpos(i).UnionWith(m_firstpos); } public void Visit(CatToken token) { if (m_root == null) m_root = token; var firtspos = new HashSet(); var lastpos = new HashSet(); token.Left.Accept(this); firtspos.UnionWith(m_firstpos); var leftLastpos = m_lastpos; token.Right.Accept(this); lastpos.UnionWith(m_lastpos); var rightFirstpos = m_firstpos; if (Nullable(token.Left)) firtspos.UnionWith(rightFirstpos); if (Nullable(token.Right)) lastpos.UnionWith(leftLastpos); m_firstpos = firtspos; m_lastpos = lastpos; foreach (var i in leftLastpos) Followpos(i).UnionWith(rightFirstpos); } public void Visit(EmptyToken token) { if (m_root == null) m_root = token; } public void Visit(SymbolToken token) { if (m_root == null) m_root = token; m_idx++; m_indexes[m_idx] = token.Value; m_firstpos = new HashSet(new[] { m_idx }); m_lastpos = new HashSet(new[] { m_idx }); } public void Visit(EndToken token) { if (m_root == null) m_root = token; m_idx++; m_indexes[m_idx] = DFAConst.UNCLASSIFIED_INPUT; m_firstpos = new HashSet(new[] { m_idx }); m_lastpos = new HashSet(new[] { m_idx }); Followpos(m_idx); m_ends.Add(m_idx, token.Tag); } public void BuildDFA(IDFATableBuilder dfa) { Safe.ArgumentNotNull(dfa,"dfa"); var states = new MapAlphabet>(new CustomEqualityComparer>( (x, y) => x.SetEquals(y), x => x.Sum(n => n.GetHashCode()) )); var initialState = states.DefineSymbol(m_firstpos); dfa.SetInitialState(initialState); var tags = GetStateTags(m_firstpos); if (tags != null && tags.Length > 0) dfa.MarkFinalState(initialState, tags); var inputMax = m_indexes.Values.Max(); var queue = new Queue>(); queue.Enqueue(m_firstpos); while (queue.Count > 0) { var state = queue.Dequeue(); var s1 = states.Translate(state); Debug.Assert(s1 != DFAConst.UNCLASSIFIED_INPUT); for (int a = 0; a <= inputMax; a++) { var next = new HashSet(); foreach (var p in state) { if (m_indexes[p] == a) { next.UnionWith(Followpos(p)); } } if (next.Count > 0) { int s2 = states.Translate(next); if (s2 == DFAConst.UNCLASSIFIED_INPUT) { s2 = states.DefineSymbol(next); tags = GetStateTags(next); if (tags != null && tags.Length > 0) dfa.MarkFinalState(s2, tags); queue.Enqueue(next); } dfa.DefineTransition(s1, s2, a); } } } } TTag[] GetStateTags(IEnumerable state) { Debug.Assert(state != null); return state.Where(m_ends.ContainsKey).Select(pos => m_ends[pos]).ToArray(); } } }