Beispiel #1
0
        /// <summary>
        /// Adds epsilon transitions to the given automaton. This method adds extra character interval
        /// transitions that are equivalent to the given set of epsilon transitions.
        /// </summary>
        /// <param name="a">The automaton.</param>
        /// <param name="pairs">A collection of <see cref="StatePair"/> objects representing pairs of
        /// source/destination states where epsilon transitions should be added.</param>
        internal static void AddEpsilons(Automaton a, ICollection <StatePair> pairs)
        {
            a.ExpandSingleton();
            var forward = new Dictionary <State, HashSet <State> >();
            var back    = new Dictionary <State, HashSet <State> >();

            foreach (StatePair p in pairs)
            {
                HashSet <State> to = forward[p.FirstState];
                if (to == null)
                {
                    to = new HashSet <State>();
                    forward.Add(p.FirstState, to);
                }

                to.Add(p.SecondState);
                HashSet <State> from = back[p.SecondState];
                if (from == null)
                {
                    from = new HashSet <State>();
                    back.Add(p.SecondState, from);
                }

                from.Add(p.FirstState);
            }

            var worklist = new LinkedList <StatePair>(pairs);
            var workset  = new HashSet <StatePair>(pairs);

            while (worklist.Count != 0)
            {
                StatePair p = worklist.RemoveAndReturnFirst();
                workset.Remove(p);
                HashSet <State> to   = forward[p.SecondState];
                HashSet <State> from = back[p.FirstState];
                if (to != null)
                {
                    foreach (State s in to)
                    {
                        var pp = new StatePair(p.FirstState, s);
                        if (!pairs.Contains(pp))
                        {
                            pairs.Add(pp);
                            forward[p.FirstState].Add(s);
                            back[s].Add(p.FirstState);
                            worklist.AddLast(pp);
                            workset.Add(pp);
                            if (from != null)
                            {
                                foreach (State q in from)
                                {
                                    var qq = new StatePair(q, p.FirstState);
                                    if (!workset.Contains(qq))
                                    {
                                        worklist.AddLast(qq);
                                        workset.Add(qq);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Add transitions.
            foreach (StatePair p in pairs)
            {
                p.FirstState.AddEpsilon(p.SecondState);
            }

            a.IsDeterministic = false;
            a.ClearHashCode();
            a.CheckMinimizeAlways();
        }
Beispiel #2
0
        /// <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>
        internal 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);
        }