Beispiel #1
0
        public override int CompareTo(RegEx <T> other)
        {
            var diff = TypeId - other.TypeId;

            if (diff != 0)
            {
                return(diff);
            }

            var intersection = other as RegExIntersection <T>;

            diff = Regexes.Length.CompareTo(intersection.Regexes.Length);
            if (diff != 0)
            {
                return(diff);
            }

            for (var i = 0; i < Regexes.Length; i++)
            {
                diff = Regexes[i].CompareTo(intersection.Regexes[i]);
                if (diff != 0)
                {
                    return(diff);
                }
            }

            return(0);
        }
Beispiel #2
0
        public override int CompareTo(RegEx <T> other)
        {
            var diff = TypeId - other.TypeId;

            if (diff != 0)
            {
                return(diff);
            }

            var concat = other as RegExConcat <T>;

            diff = Regexes.Length - concat.Regexes.Length;
            if (diff != 0)
            {
                return(diff);
            }

            for (var i = 0; i < Regexes.Length; i++)
            {
                diff = Regexes[i].CompareTo(concat.Regexes[i]);
                if (diff != 0)
                {
                    return(diff);
                }
            }

            return(0);
        }
Beispiel #3
0
        private static RegExDfa <char> MakeRegexDfa(RegEx <char> regex, uint category)
        {
            var result = new RegExDfa <char>(regex, category);

            DfaUtils.DfaStatesConcpetCheck <char> .CheckDfaStates(result);

            return(result);
        }
Beispiel #4
0
        // typeid diff if other is not star
        // compareTo( Regex, other.Regex )  otherwise
        public override int CompareTo(RegEx <T> other)
        {
            var diff = TypeId - other.TypeId;

            if (diff != 0)
            {
                return(diff);
            }

            var star = other as RegExStar <T>;

            return(Regex.CompareTo(star.Regex));
        }
        // typeid diff if other is not star
        // c - other.c  otherwise
        public override int CompareTo(RegEx <T> other)
        {
            var diff = TypeId - other.TypeId;

            if (diff != 0)
            {
                return(diff);
            }

            var range = other as RegExRange <T>;

            return(Character.CompareTo(range.Character));
        }
        // typeid diff if other is not complement
        // compareTo( Regex, other.Regex ) otherwise
        public override int CompareTo(RegEx <T> other)
        {
            var diff = TypeId - other.TypeId;

            if (diff != 0)
            {
                return(diff);
            }

            var complement = other as RegExComplement <T>;

            return(Regex.CompareTo(complement.Regex));
        }
Beispiel #7
0
        private DFAState <T> CalculateDfaState(RegEx <T> regEx)
        {
            if (dictionaryOfDfaStates.ContainsKey(regEx))
            {
                return(dictionaryOfDfaStates[regEx]); //return this state
            }
            var new_state = new DFAState <T>();

            dictionaryOfDfaStates.Add(regEx, new_state);

            var listOfTransitions = new List <KeyValuePair <T, DFAState <T> > >();

            foreach (var c in regEx.DerivChanges())
            {
                listOfTransitions.Add(new KeyValuePair <T, DFAState <T> >(c, CalculateDfaState(regEx.Derivative(c))));
            }

            if (regEx.HasEpsilon())
            {
                new_state._accepting = accepting;
            }
            else
            {
                new_state._accepting = 0;
            }

            if (listOfTransitions.Count == 0 || !listOfTransitions[0].Key.Equals(MinSymbol <T>()))
            {
                if (deadState == null)
                {
                    deadState              = new DFAState <T>();
                    deadState._accepting   = 0;
                    deadState._transitions = new KeyValuePair <T, DFAState <T> >[] { new KeyValuePair <T, DFAState <T> >(MinSymbol <T>(), deadState) };
                }
                listOfTransitions.Insert(0, new KeyValuePair <T, DFAState <T> >(MinSymbol <T>(), deadState));
                for (int i = 1; i < listOfTransitions.Count; i++)
                {
                    if (listOfTransitions[i - 1].Key.CompareTo(listOfTransitions[i].Key) >= 0)
                    {
                        throw new Exception("Inpropper order");
                    }
                }
            }

            new_state._transitions = listOfTransitions.ToArray();

            return(new_state);
        }
Beispiel #8
0
        protected bool isRegExComplex(RegEx <T> r)
        {
            var intersection = r as RegExIntersection <T>;
            var union        = r as RegExUnion <T>;

            if (intersection != null)
            {
                return(intersection.Regexes.Length > 1);
            }

            if (union != null)
            {
                return(union.Regexes.Length > 1);
            }

            return(false);
        }
        /// <summary>
        ///     RegEx representing Kleene star of RegExes.
        ///     Properties:
        ///     X** = X*
        ///     epsi* ~ empty* ~ epsi
        ///     all* ~ all
        /// </summary>
        /// <param name="regex">RegEx to be starred</param>
        public static RegEx <T> Star <T>(RegEx <T> regex) where T : IComparable <T>, IEquatable <T>
        {
            // all* ~ all
            if (0 == All <T> ().CompareTo(regex))
            {
                return(All <T> ());
            }

            // epsi* ~ empty* ~ epsi
            if (0 == Epsilon <T>().CompareTo(regex) || 0 == Empty <T>().CompareTo(regex))
            {
                return(Epsilon <T>());
            }

            // X** ~ X*
            return(regex is RegExStar <T>?regex : new RegExStar <T>(regex));
        }
        /// <summary>
        ///     Properties:
        ///     ~~X ~ X
        ///     ~all ~ empty
        ///     ~empty ~ all
        /// </summary>
        /// <param name="regex"></param>
        public static RegEx <T> Complement <T>(RegEx <T> regex) where T : IComparable <T>, IEquatable <T>
        {
            // ~all ~ empty
            if (All <T> ().Equals(regex))
            {
                return(Empty <T> ());
            }

            // ~empty ~ all
            if (Empty <T> ().Equals(regex))
            {
                return(All <T> ());
            }

            // ~~X ~ X
            var rcomp = regex as RegExComplement <T>;

            return(rcomp != null ? rcomp.Regex : new RegExComplement <T>(regex));
        }
Beispiel #11
0
 // 0 if other is RegExEpsilon
 // a < 0 otherwise
 // => RegExEpsilon is the 'smallest' one
 public override int CompareTo(RegEx <T> other)
 {
     return(TypeId - other.TypeId);
 }
Beispiel #12
0
 /// <summary>
 /// Create RegExDfa from RegEx
 /// </summary>
 /// <param name="regEx">Regular Expression from which is made automata</param>
 /// <param name="acceptingStateMarker">Number for accepting states</param>
 public RegExDfa(RegEx <T> regEx, uint acceptingStateMarker)
 {
     accepting = acceptingStateMarker;
     _start    = CalculateDfaState(regEx);
 }
 internal RegExComplement(RegEx <T> regex)
 {
     TypeId = 3;
     Regex  = regex;
 }
Beispiel #14
0
 public virtual int CompareTo(RegEx <T> other)
 {
     //Derived classes should override this method
     throw new NotImplementedException();
 }
Beispiel #15
0
 internal RegExStar(RegEx <T> regex)
 {
     this.TypeId = 2;
     this.Regex  = regex;
 }
 private static RegEx <char> Sanitize(RegEx <char> result)
 {
     return(RegExFactory.Intersection(result, RegExFactory.Star(CharactersClasses.print)));
 }
Beispiel #17
0
 public bool Equals(RegEx <T> other)
 {
     // should be correlated with compareTo
     return(CompareTo(other) == 0);
 }
Beispiel #18
0
        public static RegEx <N> Convert <N>(RegEx <T> regex, Func <T, N> converter)
            where N : IComparable <N>, IEquatable <N>
        {
            switch (regex.TypeId)
            {
            case 0:
                return(RegExEpsilon <N> .RegexEpsilon);

            case 1:
            {
                var r = regex as RegExRange <T>;
                return(new RegExRange <N>(converter(r.Character)));
            }

            case 2:
            {
                var r = regex as RegExStar <T>;
                return(new RegExStar <N>(Convert(r.Regex, converter)));
            }

            case 3:
            {
                var r = regex as RegExComplement <T>;
                return(new RegExComplement <N>(Convert(r.Regex, converter)));
            }

            case 4:
            {
                var r = regex as RegExConcat <T>;
                var l = new LinkedList <RegEx <N> >();
                foreach (var recr in r.Regexes)
                {
                    l.AddLast(Convert(recr, converter));
                }
                return(new RegExConcat <N>(l.ToArray()));
            }

            case 5:
            {
                var r = regex as RegExUnion <T>;
                var l = new LinkedList <RegEx <N> >();
                foreach (var recr in r.Regexes)
                {
                    l.AddLast(Convert(recr, converter));
                }
                return(new RegExUnion <N>(l.ToArray()));
            }

            case 6:
            {
                var r = regex as RegExIntersection <T>;
                var l = new LinkedList <RegEx <N> >();
                foreach (var recr in r.Regexes)
                {
                    l.AddLast(Convert(recr, converter));
                }
                return(new RegExIntersection <N>(l.ToArray()));
            }
            }
            return(null);
        }
        private static RegEx <char> ParseAtom()
        {
            if (HasPrefix("["))
            {
                Eat(1);

                bool complement = false;

                if (HasPrefix("^"))
                {
                    Eat(1);
                    complement = true;
                }

                RegEx <char> atom = null;

                if (HasPrefix(":digit:"))
                {
                    atom = CharactersClasses.digit;
                }
                if (HasPrefix(":print:"))
                {
                    atom = CharactersClasses.print;
                }
                if (HasPrefix(":space:"))
                {
                    atom = CharactersClasses.space;
                }

                if (atom == null)
                {
                    List <RegEx <char> > chars = new List <RegEx <char> > ();
                    char ch;
                    while ((ch = Peek()) != ']')
                    {
                        if (ch.Equals('\\'))
                        {
                            Accept("]");
                            chars.Add(SingleChar(']'));
                        }
                        else
                        {
                            chars.Add(SingleChar(ch));
                        }
                    }
                    atom = RegExFactory.Union(chars.ToArray());
                }
                else
                {
                    Eat(7);
                    Accept("]");
                }

                if (complement)
                {
                    return(RegExFactory.Intersection(RegExFactory.Range((char)0), RegExFactory.Complement(atom)));
                }
                else
                {
                    return(atom);
                }
            }

            if (HasPrefix("."))
            {
                Eat(1);
                return(RegExFactory.Range((char)0));
            }

            if (HasPrefix("("))
            {
                Eat(1);
                var node = ParseBinaryOperators();
                if (node == null)
                {
                    throw new ParseError("Parentheses around the null expression.");
                }
                Accept(")");
                return(node);
            }

            if (HasPrefix("^"))
            {
                Eat(1);
                var node = ParseAtom();
                if (node == null)
                {
                    throw new ParseError("Unassigned ^.");
                }
                return(RegExFactory.Complement(node));
            }

            if (HasPrefix("\\"))
            {
                Eat(1);
                char a = Peek();

                if (!specialCharacters.Contains("" + a))
                {
                    throw new ParseError("Special character required.");
                }
                return(SingleChar(a));
            }

            if (HasNext())
            {
                var a = Peek();
                if (specialCharacters.Contains("" + a))
                {
                    --head;
                    return(null);
                }
                return(SingleChar(a));
            }

            return(null);
        }