Пример #1
0
        public static bool IsInfinite <TLetter>(IRegExExpressionTree <TLetter> exp)
        {
            switch (exp.Type)
            {
            case RegExNodeTypes.Letter:
                return(false);

            case RegExNodeTypes.EmptyWord:
                return(false);

            case RegExNodeTypes.Empty:
                return(false);

            case RegExNodeTypes.KleeneClosure:
                return(!(IsEmpty(exp) || IsEmptyWord(exp)));

            case RegExNodeTypes.Choise:
                return(exp.GetChildren().Any(IsEmpty));

            case RegExNodeTypes.Concatenation:
                return(exp.GetChildren().All(x => !IsEmpty(x)) && exp.GetChildren().Any(IsEmpty));

            default:
                throw new ArgumentException();
            }
        }
Пример #2
0
        private static EpsilonDFA <TState, TLetter> GetNFA <TState, TLetter>(
            IRegExExpressionTree <TLetter> exp, Func <TState> getNewState)
        {
            switch (exp.Type)
            {
            case RegExNodeTypes.Letter:
                return(EpsilonDFA <TState, TLetter> .Letter(
                           ((Letter <TLetter>)exp).LetterToMatch,
                           getNewState));

            case RegExNodeTypes.EmptyWord:
                return(EpsilonDFA <TState, TLetter> .EmptyWord(getNewState));

            case RegExNodeTypes.Empty:
                return(EpsilonDFA <TState, TLetter> .Empty(getNewState));

            case RegExNodeTypes.KleeneClosure:
                return(EpsilonDFA <TState, TLetter> .KleeneClosure(
                           GetNFA(exp.GetChildren().First(), getNewState),
                           getNewState));

            case RegExNodeTypes.Choise:
                return(EpsilonDFA <TState, TLetter> .Choise(
                           GetNFA(exp.GetChildren().First(), getNewState),
                           GetNFA(exp.GetChildren().Last(), getNewState),
                           getNewState));

            case RegExNodeTypes.Concatenation:
                return(EpsilonDFA <TState, TLetter> .Concatenation(
                           GetNFA(exp.GetChildren().First(), getNewState),
                           GetNFA(exp.GetChildren().Last(), getNewState),
                           getNewState));

            default:
                throw new ArgumentException();
            }
        }
Пример #3
0
        public static bool IsEmptyWord <TLetter>(IRegExExpressionTree <TLetter> exp)
        {
            switch (exp.Type)
            {
            case RegExNodeTypes.Letter:
            case RegExNodeTypes.Empty:
                return(false);

            case RegExNodeTypes.EmptyWord:
                return(true);

            case RegExNodeTypes.KleeneClosure:
                return(exp.GetChildren().All(IsEmptyWord));

            case RegExNodeTypes.Choise:
                return(exp.GetChildren().All(IsEmptyWord));

            case RegExNodeTypes.Concatenation:
                return(exp.GetChildren().All(IsEmptyWord));

            default:
                throw new ArgumentException();
            }
        }
Пример #4
0
        public static IRegExExpressionTree <TLetter> Simplify <TLetter>(IRegExExpressionTree <TLetter> exp)
        {
            switch (exp.Type)
            {
            case RegExNodeTypes.Letter:
            case RegExNodeTypes.EmptyWord:
            case RegExNodeTypes.Empty:
                return(exp);   //No change

            case RegExNodeTypes.KleeneClosure:
                var child = exp.GetChildren().First();
                if (IsEmptyWord(child))
                {
                    return(child);   //(epsilon)* = epsilon
                }
                else if (child.Type == RegExNodeTypes.Choise)
                {
                    var first  = child.GetChildren().First();
                    var second = child.GetChildren().Last();

                    if (IsEmptyWord(first))
                    {
                        return(new KleeneClosure <TLetter>(second));   //(epsilon + r)* = r*
                    }
                    else if (IsEmptyWord(second))
                    {
                        return(new KleeneClosure <TLetter>(first));   //(r + epsilon)* = r*
                    }
                }
                return(exp);   //No change

            case RegExNodeTypes.Choise:
            {
                var first  = exp.GetChildren().First();
                var second = exp.GetChildren().Last();

                if (IsEmpty(first))
                {
                    return(second);        //empty + r = r
                }
                else if (IsEmpty(second))
                {
                    return(first);        //r + empty = r
                }
            }
                return(exp);   //No change

            case RegExNodeTypes.Concatenation:
            {
                var first  = exp.GetChildren().First();
                var second = exp.GetChildren().Last();

                if (IsEmpty(first))
                {
                    return(first);        //empty r = empty
                }
                else if (IsEmpty(second))
                {
                    return(second);        //r empty = empty
                }

                if (IsEmptyWord(first))
                {
                    return(second);        //epsilon r = r
                }
                else if (IsEmptyWord(second))
                {
                    return(first);        //r epsilon = r
                }

                if (first.Type == RegExNodeTypes.Choise)
                {
                    var first2  = first.GetChildren().First();
                    var second2 = first.GetChildren().Last();

                    return(new Choise <TLetter>(
                               new Concatenation <TLetter>(first2, second),
                               new Concatenation <TLetter>(second2, second)
                               ));//(s + t) r = s r + t r
                }
                else if (second.Type == RegExNodeTypes.Choise)
                {
                    var first2  = second.GetChildren().First();
                    var second2 = second.GetChildren().Last();

                    return(new Choise <TLetter>(
                               new Concatenation <TLetter>(first, first2),
                               new Concatenation <TLetter>(first, second2)
                               ));//r (s + t) r = r s + r t
                }
            }
                return(exp);   //No change

            default:
                throw new ArgumentException();
            }
        }