/// <summary> /// Returns a new (deterministic) automaton that accepts a single char whose value is in the /// given interval (including both end points). /// </summary> /// <param name="min">The min.</param> /// <param name="max">The max.</param> /// <returns> /// A new (deterministic) automaton that accepts a single char whose value is in the /// given interval (including both end points). /// </returns> public static Automaton MakeCharRange(char min, char max) { if (min == max) { return(BasicAutomata.MakeChar(min)); } var a = new Automaton(); var s1 = new State(); var s2 = new State(); a.Initial = s1; s2.Accept = true; if (min <= max) { s1.Transitions.Add(new Transition(min, max, s2)); } a.IsDeterministic = true; return(a); }
public static Automaton Concatenate(Automaton a1, Automaton a2) { if (a1.IsSingleton && a2.IsSingleton) { return(BasicAutomata.MakeString(a1.Singleton + a2.Singleton)); } if (BasicOperations.IsEmpty(a1) || BasicOperations.IsEmpty(a2)) { return(BasicAutomata.MakeEmpty()); } bool deterministic = a1.IsSingleton && a2.IsDeterministic; if (a1 == a2) { a1 = a1.CloneExpanded(); a2 = a2.CloneExpanded(); } else { a1 = a1.CloneExpandedIfRequired(); a2 = a2.CloneExpandedIfRequired(); } foreach (State s in a1.GetAcceptStates()) { s.Accept = false; s.AddEpsilon(a2.Initial); } a1.IsDeterministic = deterministic; a1.ClearHashCode(); a1.CheckMinimizeAlways(); return(a1); }
private Automaton ToAutomaton( IDictionary <string, Automaton> automata, IAutomatonProvider automatonProvider, bool minimize) { IList <Automaton> list; Automaton a = null; switch (kind) { case Kind.RegexpUnion: list = new List <Automaton>(); this.FindLeaves(exp1, Kind.RegexpUnion, list, automata, automatonProvider, minimize); this.FindLeaves(exp2, Kind.RegexpUnion, list, automata, automatonProvider, minimize); a = BasicOperations.Union(list); a.Minimize(); break; case Kind.RegexpConcatenation: list = new List <Automaton>(); this.FindLeaves(exp1, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize); this.FindLeaves(exp2, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize); a = BasicOperations.Concatenate(list); a.Minimize(); break; case Kind.RegexpIntersection: a = exp1.ToAutomaton(automata, automatonProvider, minimize) .Intersection(exp2.ToAutomaton(automata, automatonProvider, minimize)); a.Minimize(); break; case Kind.RegexpOptional: a = exp1.ToAutomaton(automata, automatonProvider, minimize).Optional(); a.Minimize(); break; case Kind.RegexpRepeat: a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(); a.Minimize(); break; case Kind.RegexpRepeatMin: a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(min); a.Minimize(); break; case Kind.RegexpRepeatMinMax: a = exp1.ToAutomaton(automata, automatonProvider, minimize).Repeat(min, max); a.Minimize(); break; case Kind.RegexpComplement: a = exp1.ToAutomaton(automata, automatonProvider, minimize).Complement(); a.Minimize(); break; case Kind.RegexpChar: a = BasicAutomata.MakeChar(c); break; case Kind.RegexpCharRange: a = BasicAutomata.MakeCharRange(from, to); break; case Kind.RegexpAnyChar: { a = this.anyCharAlphabet != null?BasicAutomata.MakeCharSet(this.anyCharAlphabet) : BasicAutomata.MakeAnyChar(); break; } case Kind.RegexpEmpty: a = BasicAutomata.MakeEmpty(); break; case Kind.RegexpString: a = BasicAutomata.MakeString(s); break; case Kind.RegexpAnyString: a = BasicAutomata.MakeAnyString(); break; case Kind.RegexpAutomaton: Automaton aa = null; if (automata != null) { automata.TryGetValue(s, out aa); } if (aa == null && automatonProvider != null) { try { aa = automatonProvider.GetAutomaton(s); } catch (IOException e) { throw new ArgumentException(string.Empty, e); } } if (aa == null) { throw new ArgumentException("'" + s + "' not found"); } a = aa.Clone(); // Always clone here (ignore allowMutate). break; case Kind.RegexpInterval: a = BasicAutomata.MakeInterval(min, max, digits); break; } return(a); }
public static Automaton MakeString(string s) { return(BasicAutomata.MakeString(s)); }
public static Automaton MakeCharSet(string set) { return(BasicAutomata.MakeCharSet(set)); }
public static Automaton MakeChar(char c) { return(BasicAutomata.MakeChar(c)); }
/// <summary> /// Returns an automaton that accepts the intersection of the languages of the given automata. /// Never modifies the input automata languages. /// </summary> /// <param name="a1">The a1.</param> /// <param name="a2">The a2.</param> /// <returns></returns> public static Automaton Intersection(Automaton a1, Automaton a2) { if (a1.IsSingleton) { if (a2.Run(a1.Singleton)) { return(a1.CloneIfRequired()); } return(BasicAutomata.MakeEmpty()); } if (a2.IsSingleton) { if (a1.Run(a2.Singleton)) { return(a2.CloneIfRequired()); } return(BasicAutomata.MakeEmpty()); } if (a1 == a2) { return(a1.CloneIfRequired()); } Transition[][] transitions1 = Automaton.GetSortedTransitions(a1.GetStates()); Transition[][] transitions2 = Automaton.GetSortedTransitions(a2.GetStates()); var c = new Automaton(); var worklist = new LinkedList <StatePair>(); var newstates = new Dictionary <StatePair, StatePair>(); var p = new StatePair(c.Initial, a1.Initial, a2.Initial); worklist.AddLast(p); newstates.Add(p, p); while (worklist.Count > 0) { p = worklist.RemoveAndReturnFirst(); p.S.Accept = p.FirstState.Accept && p.SecondState.Accept; Transition[] t1 = transitions1[p.FirstState.Number]; Transition[] t2 = transitions2[p.SecondState.Number]; for (int n1 = 0, b2 = 0; n1 < t1.Length; n1++) { while (b2 < t2.Length && t2[b2].Max < t1[n1].Min) { b2++; } for (int n2 = b2; n2 < t2.Length && t1[n1].Max >= t2[n2].Min; n2++) { if (t2[n2].Max >= t1[n1].Min) { var q = new StatePair(t1[n1].To, t2[n2].To); StatePair r; newstates.TryGetValue(q, out r); if (r == null) { q.S = new State(); worklist.AddLast(q); newstates.Add(q, q); r = q; } char min = t1[n1].Min > t2[n2].Min ? t1[n1].Min : t2[n2].Min; char max = t1[n1].Max < t2[n2].Max ? t1[n1].Max : t2[n2].Max; p.S.Transitions.Add(new Transition(min, max, r.S)); } } } } c.IsDeterministic = a1.IsDeterministic && a2.IsDeterministic; c.RemoveDeadTransitions(); c.CheckMinimizeAlways(); return(c); }
public static Automaton Concatenate(IList <Automaton> l) { if (l.Count == 0) { return(BasicAutomata.MakeEmptyString()); } bool allSingleton = l.All(a => a.IsSingleton); if (allSingleton) { var b = new StringBuilder(); foreach (Automaton a in l) { b.Append(a.Singleton); } return(BasicAutomata.MakeString(b.ToString())); } else { if (l.Any(a => a.IsEmpty)) { return(BasicAutomata.MakeEmpty()); } var ids = new HashSet <int>(); foreach (Automaton a in l) { ids.Add(RuntimeHelpers.GetHashCode(a)); } bool hasAliases = ids.Count != l.Count; Automaton b = l[0]; b = hasAliases ? b.CloneExpanded() : b.CloneExpandedIfRequired(); var ac = b.GetAcceptStates(); bool first = true; foreach (Automaton a in l) { if (first) { first = false; } else { if (a.IsEmptyString()) { continue; } Automaton aa = a; aa = hasAliases ? aa.CloneExpanded() : aa.CloneExpandedIfRequired(); HashSet <State> ns = aa.GetAcceptStates(); foreach (State s in ac) { s.Accept = false; s.AddEpsilon(aa.Initial); if (s.Accept) { ns.Add(s); } } ac = ns; } } b.IsDeterministic = false; b.ClearHashCode(); b.CheckMinimizeAlways(); return(b); } }
/// <summary> /// Returns a new (deterministic) automaton that accepts any single character. /// </summary> /// <returns>A new (deterministic) automaton that accepts any single character.</returns> public static Automaton MakeAnyChar() { return(BasicAutomata.MakeCharRange(char.MinValue, char.MaxValue)); }
/// <summary> /// Constructs sub-automaton corresponding to decimal numbers of value between x.Substring(n) /// and y.Substring(n) and of length x.Substring(n).Length (which must be equal to /// y.Substring(n).Length). /// </summary> /// <param name="x">The x.</param> /// <param name="y">The y.</param> /// <param name="n">The n.</param> /// <param name="initials">The initials.</param> /// <param name="zeros">if set to <c>true</c> [zeros].</param> /// <returns></returns> private static State Between(string x, string y, int n, ICollection <State> initials, bool zeros) { var s = new State(); if (x.Length == n) { s.Accept = true; } else { if (zeros) { initials.Add(s); } char cx = x[n]; char cy = y[n]; if (cx == cy) { s.AddTransition(new Transition(cx, Between(x, y, n + 1, initials, zeros && cx == '0'))); } else { // cx < cy s.AddTransition(new Transition(cx, BasicAutomata.AtLeast(x, n + 1, initials, zeros && cx == '0'))); s.AddTransition(new Transition(cy, BasicAutomata.AtMost(y, n + 1))); if (cx + 1 < cy) { s.AddTransition(new Transition((char)(cx + 1), (char)(cy - 1), BasicAutomata.AnyOfRightLength(x, n + 1))); } } } return(s); }
private Automaton ToAutomaton( IDictionary <string, Automaton> automata, IAutomatonProvider automatonProvider, bool minimize) { IList <Automaton> list; Automaton a = null; switch (this.Kind) { case Kind.RegexpUnion: list = new List <Automaton>(); this.FindLeaves(Expr1, Kind.RegexpUnion, list, automata, automatonProvider, minimize); this.FindLeaves(Expr2, Kind.RegexpUnion, list, automata, automatonProvider, minimize); a = BasicOperations.Union(list); a.Minimize(); break; case Kind.RegexpConcatenation: list = new List <Automaton>(); this.FindLeaves(Expr1, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize); this.FindLeaves(Expr2, Kind.RegexpConcatenation, list, automata, automatonProvider, minimize); a = BasicOperations.Concatenate(list); a.Minimize(); break; case Kind.RegexpIntersection: a = Expr1.ToAutomaton(automata, automatonProvider, minimize) .Intersection(Expr2.ToAutomaton(automata, automatonProvider, minimize)); a.Minimize(); break; case Kind.RegexpOptional: a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Optional(); a.Minimize(); break; case Kind.RegexpRepeat: a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Repeat(); a.Minimize(); break; case Kind.RegexpRepeatMin: a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Repeat(Min); a.Minimize(); break; case Kind.RegexpRepeatMinMax: a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Repeat(Min, Max); a.Minimize(); break; case Kind.RegexpComplement: a = Expr1.ToAutomaton(automata, automatonProvider, minimize).Complement(); a.Minimize(); break; case Kind.RegexpChar: a = BasicAutomata.MakeChar(this.Char); break; case Kind.RegexpCharRange: a = BasicAutomata.MakeCharRange(FromChar, ToChar); break; case Kind.RegexpAnyChar: a = BasicAutomata.MakeAnyChar(); break; case Kind.RegexpEmpty: a = BasicAutomata.MakeEmpty(); break; case Kind.RegexpString: a = BasicAutomata.MakeString(SourceRegExpr); break; case Kind.RegexpAnyString: a = BasicAutomata.MakeAnyString(); break; case Kind.RegexpAutomaton: Automaton aa = null; if (automata != null) { automata.TryGetValue(SourceRegExpr, out aa); } if (aa == null && automatonProvider != null) { try { aa = automatonProvider.GetAutomaton(SourceRegExpr); } catch (IOException e) { throw new ArgumentException(string.Empty, e); } } if (aa == null) { throw new ArgumentException("'" + SourceRegExpr + "' not found"); } a = aa.Clone(); // Always clone here (ignore allowMutate). break; case Kind.RegexpInterval: a = BasicAutomata.MakeInterval(Min, Max, Digits); break; } return(a); }
internal static Automaton MakeString(string s) { return(BasicAutomata.MakeString(s)); }
internal static Automaton MakeChar(char c) { return(BasicAutomata.MakeChar(c)); }