Example #1
0
        private static NFA Concat(NFA part1, NFA part2)                            //Concatenation operator
        {
            NFA result = new NFA();                                                //Create empty NFA

            result.finalStateId    = part1.StateCount + part2.StateCount - 2;      //Set final state id
            result.transitionsList = new List <Transition>(part1.transitionsList); //Set result transitions list to part1 transitions list
            foreach (var t in part2.transitionsList)
            {
                result.transitionsList.Add(new Transition(t.fromStateId + part1.finalStateId, t.toStateId + part1.finalStateId, t.symbol)); //Copy part2 transitions to part1
            }
            return(result);
        }
Example #2
0
        private static NFA Star(NFA part)                          //Star operator
        {
            NFA result = new NFA();                                //Create empty NFA

            result.finalStateId = part.StateCount + 1;             //Set final state id
            result.transitionsList.Add(new Transition(0, 1, 'E')); //Create new start transition
            foreach (var t in part.transitionsList)
            {
                result.transitionsList.Add(new Transition(t.fromStateId + 1, t.toStateId + 1, t.symbol)); //Copy all existing transitions
            }
            result.transitionsList.Add(new Transition(part.StateCount, part.StateCount + 1, 'E'));        //Add transition from n final state to new final state
            result.transitionsList.Add(new Transition(part.StateCount, 1, 'E'));                          //Add loop transition from last state of n to initial state of n
            result.transitionsList.Add(new Transition(0, part.StateCount + 1, 'E'));                      //Add transition from new inital state to new final state
            return(result);
        }
Example #3
0
        private static NFA Union(NFA part1, NFA part2)
        {
            NFA result = new NFA();                                        //Create empty NFA

            result.finalStateId = part1.StateCount + part2.StateCount + 1; //Set final state id
            result.transitionsList.Add(new Transition(0, 1, 'E'));         //Add new initial transition
            foreach (var t in part1.transitionsList)
            {
                result.transitionsList.Add(new Transition(t.fromStateId + 1, t.toStateId + 1, t.symbol));               //Copy exisiting transitions from part1
            }
            result.transitionsList.Add(new Transition(part1.StateCount, part1.StateCount + part2.StateCount + 1, 'E')); //Add transition from last state of part1 to new final state

            result.transitionsList.Add(new Transition(0, part1.StateCount + 1, 'E'));                                   //Add transition from start to part2 start
            foreach (var t in part2.transitionsList)
            {
                result.transitionsList.Add(new Transition(t.fromStateId + part1.StateCount + 1, t.toStateId + part1.StateCount + 1, t.symbol)); //Copy exisiting transitions from part2
            }
            result.transitionsList.Add(new Transition(part2.StateCount + part1.StateCount, part1.StateCount + part2.StateCount + 1, 'E'));      //Add transition from end of part2 to new final state
            return(result);
        }
Example #4
0
        public void AutoTest(string regex, string str, bool shouldReturn, bool displayNFA = false) //Method for doing one auto test
        {
            NFA autoTest = NFA.Create(regex);                                                      //Create NFA for given regex

            testCount++;                                                                           //Increase test count
            if (autoTest != null)
            {
                if (displayNFA || displayNFAOverride) //Show NFA if needed
                {
                    Console.WriteLine(autoTest.ToString());
                }
                string status     = "FAILED";                  //Default status
                bool   testStatus = autoTest.CheckString(str); //Check given string
                if (shouldReturn == testStatus)                //Compare responses
                {
                    status = "OK";
                }
                Console.WriteLine(regex + " : " + str + " : " + testStatus + " :   " + status); //Test output
                StatusFilter(status);                                                           //Add to summary
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            //Auto testing
            AutoTester at = new AutoTester();

            Console.WriteLine("****** AUTO TEST START ******");
            Console.WriteLine("regex : string : match : STATUS");
            Console.WriteLine();
            at.AutoTest("ab", "a", false);
            at.AutoTest("ab", "", false);
            at.AutoTest("ab", "aa", false);
            at.AutoTest("ab", "aaa", false);
            at.AutoTest("ab", "ab", true);
            at.AutoTest("ab", "b", false);
            at.AutoTest("ab", "bb", false);
            Console.WriteLine();
            at.AutoTest("a", "a", true);
            at.AutoTest("a", "", false);
            at.AutoTest("a", "aa", false);
            at.AutoTest("a", "aaa", false);
            at.AutoTest("a", "ab", false);
            at.AutoTest("a", "b", false);
            at.AutoTest("a", "bb", false);
            Console.WriteLine();
            at.AutoTest("a*", "", true);
            at.AutoTest("a*", "a", true);
            at.AutoTest("a*", "aa", true);
            at.AutoTest("a*", "aaa", true);
            at.AutoTest("a*", "ab", false);
            at.AutoTest("a*", "b", false);
            at.AutoTest("a*", "bb", false);
            Console.WriteLine();
            at.AutoTest("a*b", "", false);
            at.AutoTest("a*b", "a", false);
            at.AutoTest("a*b", "aa", false);
            at.AutoTest("a*b", "aaa", false);
            at.AutoTest("a*b", "ab", true);
            at.AutoTest("a*b", "b", true);
            at.AutoTest("a*b", "bb", false);
            at.AutoTest("a*b", "aaaaab", true);
            Console.WriteLine();
            at.AutoTest("a|b", "", false);
            at.AutoTest("a|b", "a", true);
            at.AutoTest("a|b", "aa", false);
            at.AutoTest("a|b", "aaa", false);
            at.AutoTest("a|b", "ab", false);
            at.AutoTest("a|b", "b", true);
            Console.WriteLine();
            at.AutoTest("a*|b", "", true);
            at.AutoTest("a*|b", "a", true);
            at.AutoTest("a*|b", "aaa", true);
            at.AutoTest("a*|b", "b", true);
            at.AutoTest("a*|b", "ba", false);
            at.AutoTest("a*|b", "ab", false);
            Console.WriteLine();
            at.AutoTest("a(ab)*c", "aababababc", true);
            at.AutoTest("a(ab)*c", "aababababcc", false);
            at.AutoTest("a(ab)*c", "ac", true);
            at.AutoTest("a(ab)*c", "", false);
            at.AutoTest("a(ab)*c", "c", false);
            Console.WriteLine();
            at.AutoTest("(a*)|b", "", true);
            at.AutoTest("(a*)|b", "a", true);
            at.AutoTest("(a*)|b", "aa", true);
            at.AutoTest("(a*)|b", "aaa", true);
            at.AutoTest("(a*)|b", "ab", false);
            at.AutoTest("(a*)|b", "b", true);
            at.AutoTest("(a*)|b", "bb", false);
            Console.WriteLine();
            at.AutoTest("((a*b)*c)|b", "aaaaabaabc", true);
            at.AutoTest("((a*b)*c)|b", "c", true);
            at.AutoTest("((a*b)*c)|b", "ca", false);
            at.AutoTest("((a*b)*c)|b", "cb", false);
            at.AutoTest("((a*b)*c)|b", "cc", false);
            at.AutoTest("((a*b)*c)|b", "", false);
            Console.WriteLine();
            at.AutoTest("a*b*c*", "abb", true);
            at.AutoTest("a*b*c*", "aaaaabb", true);
            at.AutoTest("a*b*c*", "aaaaac", true);
            at.AutoTest("a*b*c*", "aaaaabbccccc", true);
            at.AutoTest("a*b*c*", "ccc", true);
            Console.WriteLine();
            at.AutoTest("(a*)", "a", true);
            at.AutoTest("(a*)", "aaa", true);
            at.AutoTest("(a*)b", "aaab", true);
            at.AutoTest("b(a*)", "b", true);
            at.AutoTest("b(a*)", "ba", true);
            Console.WriteLine();
            at.AutoTest("()", "", true);
            at.AutoTest("()", "a", false);
            Console.WriteLine();
            at.AutoTest("(c|b(a*))*", "cccbbaaaaabab", true);
            at.AutoTest("(c|b(a*))*", "cccbbaaaaababc", true);
            at.AutoTest("(c|(a))*", "caaaaaccc", true);
            Console.WriteLine();
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "0", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "00", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "11", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "000", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "011", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "110", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "0000", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "0011", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "0110", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "1001", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "1100", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "1111", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "00000", true);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "00001", false);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "0011101", false);
            at.AutoTest("(0|(1(01*(00)*0)*1)*)*", "1011100000011100000110111000000111000001", true);
            Console.WriteLine("Invalid regex tests: ");
            at.AutoTest("((a*)", "", true);
            at.AutoTest("(a*))", "", true);
            at.AutoTest("|a", "", true);
            at.AutoTest("|(a)", "", true);
            at.AutoTest("a**", "aaaa", true);
            at.AutoTest("a**", "", true);
            at.AutoTest("a**", "b", true);
            at.AutoTest("a||b", "a", true);
            at.AutoTest("a||b", "b", true);
            Console.WriteLine();
            at.StatusFilterDisplay();
            Console.WriteLine("****** AUTO TEST END ******");
            Console.WriteLine();
            //Manual testing loop
            Console.WriteLine("Input regex");
            var input    = Console.ReadLine();
            NFA inputNFA = NFA.Create(input);

            if (inputNFA != null)
            {
                Console.WriteLine(inputNFA.ToString());

                while (true)
                {
                    Console.WriteLine("=====================");
                    Console.WriteLine("Input string to check");
                    input = Console.ReadLine();
                    Console.WriteLine(inputNFA.CheckString(input));
                }
            }
        }