Beispiel #1
0
        public static Automaton Repeat(Automaton a, int min, int max)
        {
            if (min > max)
            {
                return(BasicAutomata.MakeEmpty());
            }

            max -= min;
            a.ExpandSingleton();
            Automaton b;

            if (min == 0)
            {
                b = BasicAutomata.MakeEmptyString();
            }
            else if (min == 1)
            {
                b = a.Clone();
            }
            else
            {
                var @as = new List <Automaton>();
                while (min-- > 0)
                {
                    @as.Add(a);
                }

                b = Concatenate(@as);
            }

            if (max > 0)
            {
                var d = a.Clone();
                while (--max > 0)
                {
                    var c = a.Clone();
                    foreach (var p in c.GetAcceptStates())
                    {
                        p.AddEpsilon(d.Initial);
                    }

                    d = c;
                }

                foreach (var p in b.GetAcceptStates())
                {
                    p.AddEpsilon(d.Initial);
                }

                b.IsDeterministic = false;
                b.ClearHashCode();
                b.CheckMinimizeAlways();
            }

            return(b);
        }