Example #1
0
        public void TestPDAManyInfLoops()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZX", furtherSymbol);

            pda.AddState(1, false);
            pda.AddState(2, false);
            pda.AddState(3, false);
            pda.AddState(4, false);
            pda.AddState(5, false);
            pda.AddTransition().From(0).To(0).Read().Pop('Z').Push("XZ");
            pda.AddTransition().From(0).To(0).Read().Pop('X').Push("XX");
            pda.AddTransition().From(1).To(1).Read().Pop('X').Push("XX");
            pda.AddTransition().From(2).To(2).Read().Pop('X').Push("XX");
            pda.AddTransition().From(3).To(3).Read().Pop('X').Push("XX");
            pda.AddTransition().From(4).To(4).Read().Pop('X').Push("XX");
            pda.AddTransition().From(5).To(5).Read().Pop('X').Push("XX");

            pda.AddTransition().From(0).To(1).Read().Pop('X').Push("XX");
            pda.AddTransition().From(1).To(2).Read().Pop('X').Push("XX");
            pda.AddTransition().From(2).To(3).Read().Pop('X').Push("XX");
            pda.AddTransition().From(3).To(4).Read().Pop('X').Push("XX");
            pda.AddTransition().From(4).To(5).Read().Pop('X').Push("XX");
            pda.AddTransition().From(5).To(1).Read().Pop('X').Push("XX");
            pda.AddTransition().From(0).To(3).Read().Pop('X').Push("XX");
            pda.AddTransition().From(2).To(5).Read().Pop('X').Push("XX");
            pda.AddTransition().From(5).To(2).Read().Pop('X').Push("XX");
            pda.CreateRunner();

            Assert.IsFalse(pda.AcceptsWord("").Accepts());
            Assert.IsFalse(pda.AcceptsWord("a").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaaa").Accepts());
        }
Example #2
0
        public void TestDPDARunnerForFinalStateAndEmptyStack()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.FinalStateAndEmptyStack(), true, 'Z', false, "ZXY");

            dpda.AddState(1, false);
            dpda.AddState(2, true);

            dpda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            dpda.AddTransition().From(0).To(1).Read('v').Pop('Z').Push("Z");
            dpda.AddTransition().From(0).To(1).Read('v').Pop('X').Push("X");
            dpda.AddTransition().From(0).To(1).Read('v').Pop('Y').Push("Y");

            dpda.AddTransition().From(1).To(1).Read('a').Pop('X').Push();
            dpda.AddTransition().From(1).To(1).Read('b').Pop('Y').Push();
            dpda.AddTransition().From(1).To(2).Read().Pop('Z').Push("");

            dpda.CreateRunner();

            Assert.IsFalse(dpda.AcceptsWord("").Accepts());
            Assert.IsFalse(dpda.AcceptsWord("ababababababvbabababababab").Accepts());
            Assert.IsTrue(dpda.AcceptsWord("abbabaababbabbbbbaaaaabbbbbabaaabbbabaaababbbabvbabbbabaaababbbaaababbbbbaaaaabbbbbabbabaababba").Accepts());
            Assert.IsTrue(dpda.AcceptsWord("abavaba").Accepts());
        }
Example #3
0
        public void TestPDA2()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.FinalState(), false, 'Z', false, "Z", furtherSymbol);

            pda.AddState(1, false);
            pda.AddState(2, false);
            pda.AddState(3, false);
            pda.AddState(4, false);
            pda.AddState(5, true);
            pda.AddTransition().From(0).To(0).Read().Pop('Z').Push("ZZ");
            pda.AddTransition().From(0).To(1).Read().Pop('Z').Push();
            pda.AddTransition().From(1).To(2).Read().Pop('Z').Push();
            pda.AddTransition().From(1).To(3).Read('a').Pop('Z').Push();
            pda.AddTransition().From(3).To(4).Read('a').Pop('Z').Push();
            pda.AddTransition().From(4).To(5).Read('a').Pop('Z').Push();
            pda.CreateRunner();

            Assert.IsTrue(pda.AcceptsWord("aaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("").Accepts());
            Assert.IsFalse(pda.AcceptsWord("a").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaaaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("b").Accepts());
            Assert.IsFalse(pda.AcceptsWord("ab").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aab").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaab").Accepts());
        }
Example #4
0
        public void RunSimulationCFGTest3()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.FinalState(), false, 'Z', false, "ZXY", 'V');

            pda.AddState(1, false);
            pda.AddState(2, true);
            pda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            pda.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            pda.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            pda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            pda.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");
            pda.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            pda.AddTransition().From(0).To(1).Read().Pop('X').Push("X");
            pda.AddTransition().From(0).To(1).Read().Pop('Y').Push("Y");
            pda.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pda.AddTransition().From(1).To(1).Read('a').Pop('X').Push();
            pda.AddTransition().From(1).To(1).Read('b').Pop('Y').Push();
            pda.AddTransition().From(1).To(2).Read().Pop('Z').Push("ZZ");

            pda.CreateRunner();

            var path = CFGSimulationRunner <char> .RunSimulation(pda, "abaabbaaba".ToCharArray());

            var act = path.ToXml();

            var         reader    = new StreamReader(@"..\..\..\TestPDL\PDATest\XmlPathCFG3.xml");
            XmlDocument expXmlDoc = new XmlDocument();

            expXmlDoc.Load(reader);
            XDocument xDoc = XDocument.Load(new XmlNodeReader(expXmlDoc));
            var       exp  = xDoc.Root;

            Assert.IsTrue(XNode.DeepEquals(exp, act));
        }
        public void MergeDPDAsWithEmptyStackTest()
        {
            var dpda1 = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'A', false, "ABC", 'Z');

            dpda1.AddState(1, false);
            dpda1.AddState(2, false);
            dpda1.AddState(3, false);
            dpda1.AddState(4, false);
            dpda1.AddState(5, false);
            dpda1.AddState(6, false);
            dpda1.AddState(7, false);

            dpda1.AddTransition().From(0).To(1).Read().Pop('A').Push("AAA");
            dpda1.AddTransition().From(1).To(2).Read('a').Pop('A').Push();
            dpda1.AddTransition().From(2).To(3).Read('a').Pop('A').Push();
            dpda1.AddTransition().From(3).To(4).Read().Pop('A').Push("BBCC");
            dpda1.AddTransition().From(4).To(4).Read('b').Pop('B').Push();
            dpda1.AddTransition().From(4).To(4).Read('c').Pop('C').Push();

            dpda1.AddTransition().From(1).To(5).Read().Pop('B').Push();
            dpda1.AddTransition().From(2).To(6).Read().Pop('B').Push("B");
            dpda1.AddTransition().From(6).To(7).Read().Pop('B').Push("B");
            dpda1.AddTransition().From(7).To(6).Read().Pop('B').Push("B");


            var dpda2 = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'A', false, "ABC", 'Z');

            dpda2.AddState(1, false);
            dpda2.AddState(2, false);
            dpda2.AddState(3, false);
            dpda2.AddState(4, false);

            dpda2.AddTransition().From(0).To(1).Read().Pop('A').Push("AAA");
            dpda2.AddTransition().From(1).To(2).Read('a').Pop('A').Push();
            dpda2.AddTransition().From(2).To(3).Read('a').Pop('A').Push();
            dpda2.AddTransition().From(3).To(4).Read().Pop('A').Push("BBCC");
            dpda2.AddTransition().From(4).To(4).Read('b').Pop('B').Push();
            dpda2.AddTransition().From(4).To(4).Read('c').Pop('C').Push();

            var mergedDPDAForSimulationOf1 = PDATransformer <char, char> .MergeDPDAsWithEmptyStack(dpda1, dpda2, 'X', 'Y');

            dpda1.CreateRunner();
            mergedDPDAForSimulationOf1.CreateRunner();

            var equalityResult1 = new PDAEqualityResult <char, char>(dpda1, mergedDPDAForSimulationOf1, "abc", 10, 400000, 3000);

            Assert.IsTrue(equalityResult1.AreEqual);


            var mergedDPDAForSimulationOf2 = PDATransformer <char, char> .MergeDPDAsWithEmptyStack(dpda2, dpda1, 'X', 'Y');

            dpda2.CreateRunner();
            mergedDPDAForSimulationOf2.CreateRunner();

            var equalityResult2 = new PDAEqualityResult <char, char>(dpda2, mergedDPDAForSimulationOf2, "abc", 10, 400000, 3000);

            Assert.IsTrue(equalityResult2.AreEqual);
        }
Example #6
0
        public void TestPerformance()
        {
            var pdaWithShorterSequence = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZXY");

            pdaWithShorterSequence.AddState(1, false);
            pdaWithShorterSequence.AddTransition().From(0).To(0).Read().Pop('Z').Push("ZZZZZZZZZZZZ");
            pdaWithShorterSequence.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pdaWithShorterSequence.AddTransition().From(1).To(1).Read().Pop('Z').Push();
            pdaWithShorterSequence.AddTransition().From(1).To(1).Read('a').Pop('Z').Push();
            pdaWithShorterSequence.CreateRunner();

            var stopwatch = new Stopwatch();

            stopwatch.Start();
            Assert.IsTrue(pdaWithShorterSequence.AcceptsWord("a").Accepts());
            stopwatch.Stop();
            Console.WriteLine("Runtime for shorter:" + stopwatch.Elapsed);

            var pdaWithLongerSequence = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZXY");

            pdaWithLongerSequence.AddState(1, false);
            pdaWithLongerSequence.AddTransition().From(0).To(0).Read().Pop('Z').Push("ZZZZZZZZZZZZZZ");
            pdaWithLongerSequence.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pdaWithLongerSequence.AddTransition().From(1).To(1).Read().Pop('Z').Push();
            pdaWithLongerSequence.AddTransition().From(1).To(1).Read('a').Pop('Z').Push();
            pdaWithLongerSequence.CreateRunner();

            var stopwatch2 = new Stopwatch();

            stopwatch2.Start();
            Assert.IsTrue(pdaWithLongerSequence.AcceptsWord("a").Accepts());
            stopwatch2.Stop();
            Console.WriteLine("Runtime for longer:" + stopwatch2.Elapsed);
        }
Example #7
0
        public void SimulationRunnerComparisonTest()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "Z");

            pda.AddState(1, false);
            pda.AddTransition().From(0).To(0).Read().Pop('Z').Push("ZZZZZZZZZZZZZZZZZZ");
            pda.AddTransition().From(0).To(1).Read().Pop('Z').Push();
            pda.AddTransition().From(1).To(1).Read('a').Pop('Z').Push();

            var word = "aaaaaaaaaaaaaaaaa".ToCharArray();

            var s = new Stopwatch();

            s.Start();
            var path1 = CFGSimulationRunner <char> .RunSimulation(pda, word);

            s.Stop();

            Console.WriteLine(s.ElapsedMilliseconds);

            s.Restart();
            pda.CreateRunner();
            var path2 = DirectSimulationRunner <char, char> .RunSimulation(pda, word);

            s.Stop();

            Console.WriteLine(s.ElapsedMilliseconds);
        }
        public void TestRealDPDAToNormalForm()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'Z', false, "ZYX", 'A');

            dpda.AddState(1, false);

            dpda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            dpda.AddTransition().From(0).To(1).Read('x').Pop('Z').Push("Z");
            dpda.AddTransition().From(0).To(1).Read('x').Pop('X').Push("X");
            dpda.AddTransition().From(0).To(1).Read('x').Pop('Y').Push("Y");

            dpda.AddTransition().From(1).To(1).Read('a').Pop('X').Push();
            dpda.AddTransition().From(1).To(1).Read('b').Pop('Y').Push();
            dpda.AddTransition().From(1).To(1).Read().Pop('Z').Push();

            var dpdaInNormalForm = DPDAToNormalFormConverter <char, char> .ToNormalForm(dpda);

            dpda.CreateRunner();
            dpdaInNormalForm.CreateRunner();

            var equalityResult = new PDAEqualityResult <char, StackSymbolSequence <char> >(dpda, dpdaInNormalForm, "abx", 10, 600000, 3000);

            Assert.IsTrue(equalityResult.AreEqual);
        }
Example #9
0
 private static void AddStatesToPDA(PDA <A, S> origin, PDA <A, StackSymbolSequence <S> > target)
 {
     foreach (var state in origin.States.Where(s => s.Key != origin.InitialState.Id))
     {
         target.AddState(state.Key, state.Value.Final);
     }
 }
        public void ToDPDAWithAddedAlphabetSymbolTest()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'Z', false, "ZYX");

            dpda.AddState(1, false);
            dpda.AddState(2, false);

            dpda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            dpda.AddTransition().From(0).To(1).Read('x').Pop('Z').Push("Z");
            dpda.AddTransition().From(0).To(1).Read('x').Pop('X').Push("X");
            dpda.AddTransition().From(0).To(1).Read('x').Pop('Y').Push("Y");

            dpda.AddTransition().From(1).To(1).Read('a').Pop('X').Push("");
            dpda.AddTransition().From(1).To(1).Read('b').Pop('Y').Push("");

            dpda.AddTransition().From(1).To(2).Read().Pop('Z').Push();

            var act = PDATransformer <char, char> .ToDPDAWithAddedAlphabetSymbol(dpda, '$');

            act.CreateRunner();

            Assert.IsFalse(act.AcceptsWord("").Accepts());
            Assert.IsFalse(act.AcceptsWord("ax").Accepts());
            Assert.IsFalse(act.AcceptsWord("aaxa").Accepts());
            Assert.IsFalse(act.AcceptsWord("xa").Accepts());
            Assert.IsFalse(act.AcceptsWord("axaa").Accepts());
            Assert.IsFalse(act.AcceptsWord("ax$").Accepts());
            Assert.IsFalse(act.AcceptsWord("aaxa$").Accepts());
            Assert.IsFalse(act.AcceptsWord("xa$").Accepts());
            Assert.IsFalse(act.AcceptsWord("axaa$").Accepts());
            Assert.IsFalse(act.AcceptsWord("$").Accepts());

            Assert.IsTrue(act.AcceptsWord("x$").Accepts());
            Assert.IsTrue(act.AcceptsWord("axa$").Accepts());
            Assert.IsTrue(act.AcceptsWord("aaxaa$").Accepts());
            Assert.IsTrue(act.AcceptsWord("aaaxaaa$").Accepts());
        }
Example #11
0
        public void TestMaximumNumberOfWords()
        {
            var pda1 = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZXY");

            pda1.AddState(1, false);
            pda1.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            pda1.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            pda1.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            pda1.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            pda1.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            pda1.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            pda1.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pda1.AddTransition().From(0).To(1).Read().Pop('X').Push("X");
            pda1.AddTransition().From(0).To(1).Read().Pop('Y').Push("Y");

            pda1.AddTransition().From(1).To(1).Read('c').Pop('X').Push();
            pda1.AddTransition().From(1).To(1).Read('d').Pop('Y').Push();
            pda1.AddTransition().From(1).To(1).Read().Pop('Z').Push();

            var pda2 = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZXY");

            pda2.AddState(1, false);
            pda2.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            pda2.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            pda2.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            pda2.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            pda2.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            pda2.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            pda2.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pda2.AddTransition().From(0).To(1).Read().Pop('X').Push("X");
            pda2.AddTransition().From(0).To(1).Read().Pop('Y').Push("Y");

            pda2.AddTransition().From(1).To(1).Read('c').Pop('X').Push();
            pda2.AddTransition().From(1).To(1).Read('d').Pop('Y').Push();
            pda2.AddTransition().From(1).To(1).Read('a').Pop('Z').Push();

            pda1.CreateRunner();
            pda2.CreateRunner();

            var watch = new Stopwatch();

            watch.Start();
            var res = new PDAEqualityResult <char, char>(pda1, pda2, "abcd", 10, 400000, 60000);

            watch.Stop();

            Console.WriteLine(watch.ElapsedMilliseconds);

            Assert.IsFalse(res.AreEqual);
        }
        public void ToSDAInNormalFormTest()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'Z', false, "ZA");

            dpda.AddState(1, false);

            dpda.HasNormalForm();

            dpda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("AZ");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('A').Push("AA");
            dpda.AddTransition().From(0).To(1).Read('b').Pop('A').Push("A");
            dpda.AddTransition().From(1).To(1).Read('c').Pop('A').Push();
            dpda.AddTransition().From(1).To(1).Read().Pop('Z').Push();

            var symbols = new Dictionary <string, TripleStackSymbol <char> >()
            {
                { "0A1", new TripleStackSymbol <char>(0, 'A', 1) },
                { "0Z1", new TripleStackSymbol <char>(0, 'Z', 1) },
                { "1A1", new TripleStackSymbol <char>(1, 'A', 1) }
            };

            var exp = new SDA <char, TripleStackSymbol <char> >(symbols.Values);

            exp.AddTransition('a', symbols["0Z1"], new TripleStackSymbol <char>[] { symbols["0A1"] });
            exp.AddTransition('a', symbols["0A1"], new TripleStackSymbol <char>[] { symbols["0A1"], symbols["1A1"] });
            exp.AddTransition('b', symbols["0A1"], new TripleStackSymbol <char>[] { symbols["1A1"] });
            exp.AddTransition('c', symbols["1A1"]);

            var act = DPDAInNormalFormToSDAConverter <char, char> .ToSDAInNormalForm(dpda).sda;

            Assert.IsTrue(exp.Equals(act));

            var actPDA = act.ToPDA(symbols["0Z1"]);

            actPDA.CreateRunner();
            dpda.CreateRunner();

            var equalityResult = new PDAEqualityResult <char, TripleStackSymbol <char> >(dpda, actPDA, "abc", 7, 300000, 3000);

            Assert.IsTrue(equalityResult.AreEqual);

            var actDeterminisedSDA = DeterminisedSDA <char, TripleStackSymbol <char> > .FromSDAInNormalForm(act);

            actDeterminisedSDA.CalculateShortestWordsOfStackSymbols();

            Assert.IsTrue(actDeterminisedSDA.ShortestWordsOfStackSymbols[symbols["1A1"]].SequenceEqual("c"));
            Assert.IsTrue(actDeterminisedSDA.ShortestWordsOfStackSymbols[symbols["0A1"]].SequenceEqual("bc"));
            Assert.IsTrue(actDeterminisedSDA.ShortestWordsOfStackSymbols[symbols["0Z1"]].SequenceEqual("abc"));
        }
Example #13
0
        public void TestDPDARunnerForEmptyLanguage2()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'Z', false, "ZX");

            dpda.AddState(1, false);

            dpda.AddTransition().From(0).To(1).Read().Pop('Z').Push("X");
            dpda.AddTransition().From(1).To(1).Read().Pop('X').Push("Z");
            dpda.AddTransition().From(1).To(0).Read().Pop('Z').Push("X");
            dpda.AddTransition().From(0).To(0).Read().Pop('X').Push("Z");

            dpda.CreateRunner();

            Assert.IsFalse(dpda.AcceptsWord("").Accepts());
        }
Example #14
0
        public void TestPDA13()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.FinalState(), false, 'Z', false, stackAlphabet, furtherSymbol);

            pda.AddState(1, false);
            pda.AddState(2, false);
            pda.AddTransition().From(0).To(0).Read().Pop('Z').Push("ZY");
            pda.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pda.AddTransition().From(1).To(1).Read().Pop('Z').Push("ZX");
            pda.AddTransition().From(1).To(2).Read().Pop('Z').Push("Z");
            pda.AddTransition().From(2).To(2).Read().Pop('Z').Push();
            pda.AddTransition().From(2).To(2).Read().Pop('X').Push();
            pda.AddTransition().From(2).To(2).Read().Pop('Y').Push("Z");
            pda.CreateRunner();

            Assert.IsFalse(pda.AcceptsWord("").Accepts());
            Assert.IsFalse(pda.AcceptsWord("a").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("b").Accepts());
            Assert.IsFalse(pda.AcceptsWord("ab").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aab").Accepts());
        }
Example #15
0
        public void TestPDA18()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "Z", furtherSymbol);

            pda.AddState(1, false);
            pda.AddTransition().From(0).To(1).Read().Pop('Z').Push("ZZZZZZZZZZ");
            pda.AddTransition().From(1).To(1).Read().Pop('Z').Push();
            pda.CreateRunner();

            Assert.IsTrue(pda.AcceptsWord("").Accepts());
            Assert.IsFalse(pda.AcceptsWord("a").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aaaa").Accepts());
            Assert.IsFalse(pda.AcceptsWord("b").Accepts());
            Assert.IsFalse(pda.AcceptsWord("ab").Accepts());
            Assert.IsFalse(pda.AcceptsWord("aab").Accepts());
        }
Example #16
0
        public void RunSimulationTest()
        {
            var pda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZAB");

            pda.AddState(1, false);

            pda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("AZ");
            pda.AddTransition().From(0).To(0).Read('a').Pop('A').Push("AA");
            pda.AddTransition().From(0).To(0).Read('a').Pop('B').Push("AB");
            pda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("BZ");
            pda.AddTransition().From(0).To(0).Read('b').Pop('A').Push("BA");
            pda.AddTransition().From(0).To(0).Read('b').Pop('B').Push("BB");

            pda.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pda.AddTransition().From(0).To(1).Read().Pop('A').Push("A");
            pda.AddTransition().From(0).To(1).Read().Pop('B').Push("B");
            pda.AddTransition().From(0).To(1).Read('a').Pop('Z').Push("Z");
            pda.AddTransition().From(0).To(1).Read('a').Pop('A').Push("A");
            pda.AddTransition().From(0).To(1).Read('a').Pop('B').Push("B");
            pda.AddTransition().From(0).To(1).Read('b').Pop('Z').Push("Z");
            pda.AddTransition().From(0).To(1).Read('b').Pop('A').Push("A");
            pda.AddTransition().From(0).To(1).Read('b').Pop('B').Push("B");

            pda.AddTransition().From(1).To(1).Read('a').Pop('A').Push();
            pda.AddTransition().From(1).To(1).Read('b').Pop('B').Push();
            pda.AddTransition().From(1).To(1).Read().Pop('Z').Push();

            pda.CreateRunner();

            var path = DirectSimulationRunner <char, char> .RunSimulation(pda, "abbababba".ToCharArray());

            var act = path.ToXml();

            var         reader    = new StreamReader(@"..\..\..\TestPDL\PDATest\XmlPath.xml");
            XmlDocument expXmlDoc = new XmlDocument();

            expXmlDoc.Load(reader);
            XDocument xDoc = XDocument.Load(new XmlNodeReader(expXmlDoc));
            var       exp  = xDoc.Root;

            Assert.IsTrue(XNode.DeepEquals(exp, act));
        }
        public void TestDPDAToNormalForm()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'A', false, "ABC", 'Z');

            dpda.AddState(1, false);
            dpda.AddState(2, false);
            dpda.AddState(3, false);
            dpda.AddState(4, false);
            dpda.AddState(5, false);
            dpda.AddState(6, false);
            dpda.AddState(7, false);

            dpda.AddTransition().From(0).To(1).Read().Pop('A').Push("AAA");
            dpda.AddTransition().From(1).To(2).Read('a').Pop('A').Push();
            dpda.AddTransition().From(2).To(3).Read('a').Pop('A').Push();
            dpda.AddTransition().From(3).To(4).Read().Pop('A').Push("BBCC");
            dpda.AddTransition().From(4).To(4).Read('b').Pop('B').Push();
            dpda.AddTransition().From(4).To(4).Read('c').Pop('C').Push();

            dpda.AddTransition().From(1).To(5).Read().Pop('B').Push();
            dpda.AddTransition().From(2).To(6).Read().Pop('B').Push("B");
            dpda.AddTransition().From(6).To(7).Read().Pop('B').Push("B");
            dpda.AddTransition().From(7).To(6).Read().Pop('B').Push("B");


            var allStackSymbols = new List <StackSymbolSequence <char> >()
            {
                new StackSymbolSequence <char>("A"),
                new StackSymbolSequence <char>("B"),
                new StackSymbolSequence <char>("C"),
                new StackSymbolSequence <char>("AA"),
                new StackSymbolSequence <char>("AAA"),
                new StackSymbolSequence <char>("BBCC"),
                new StackSymbolSequence <char>("BCC"),
                new StackSymbolSequence <char>("CC"),
            };
            var exp = new PDA <char, StackSymbolSequence <char> >(new AcceptanceCondition.EmptyStack(), true, new StackSymbolSequence <char>('A'), false, allStackSymbols);

            exp.AddState(1, false);
            exp.AddState(2, false);
            exp.AddState(3, false);
            exp.AddState(4, false);
            exp.AddState(5, false);
            exp.AddState(6, false);
            exp.AddState(7, false);

            exp.AddTransition().From(0).To(2).Read('a').Pop(new StackSymbolSequence <char>("A")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("AA") });
            exp.AddTransition().From(0).To(2).Read('a').Pop(new StackSymbolSequence <char>("AA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("AA"), new StackSymbolSequence <char>("A") });
            exp.AddTransition().From(0).To(2).Read('a').Pop(new StackSymbolSequence <char>("AAA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("AA"), new StackSymbolSequence <char>("AA") });
            exp.AddTransition().From(1).To(5).Read().Pop(new StackSymbolSequence <char>("B")).Push();

            exp.AddTransition().From(1).To(2).Read('a').Pop(new StackSymbolSequence <char>("A")).Push();
            exp.AddTransition().From(1).To(2).Read('a').Pop(new StackSymbolSequence <char>("AA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("A") });
            exp.AddTransition().From(1).To(2).Read('a').Pop(new StackSymbolSequence <char>("AAA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("AA") });

            exp.AddTransition().From(2).To(3).Read('a').Pop(new StackSymbolSequence <char>("A")).Push();
            exp.AddTransition().From(2).To(3).Read('a').Pop(new StackSymbolSequence <char>("AA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("A") });
            exp.AddTransition().From(2).To(3).Read('a').Pop(new StackSymbolSequence <char>("AAA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("AA") });

            exp.AddTransition().From(3).To(4).Read('b').Pop(new StackSymbolSequence <char>("A")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("BCC") });
            exp.AddTransition().From(3).To(4).Read('b').Pop(new StackSymbolSequence <char>("AA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("BCC"), new StackSymbolSequence <char>("A") });
            exp.AddTransition().From(3).To(4).Read('b').Pop(new StackSymbolSequence <char>("AAA")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("BCC"), new StackSymbolSequence <char>("AA") });

            exp.AddTransition().From(4).To(4).Read('b').Pop(new StackSymbolSequence <char>("B")).Push();
            exp.AddTransition().From(4).To(4).Read('b').Pop(new StackSymbolSequence <char>("BCC")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("CC") });
            exp.AddTransition().From(4).To(4).Read('b').Pop(new StackSymbolSequence <char>("BBCC")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("BCC") });
            exp.AddTransition().From(4).To(4).Read('c').Pop(new StackSymbolSequence <char>("C")).Push();
            exp.AddTransition().From(4).To(4).Read('c').Pop(new StackSymbolSequence <char>("CC")).Push(new StackSymbolSequence <char>[] { new StackSymbolSequence <char>("C") });


            var act = DPDAToNormalFormConverter <char, char> .ToNormalForm(dpda);

            Assert.IsTrue(exp.Equals(act));

            dpda.CreateRunner();
            exp.CreateRunner();
            act.CreateRunner();

            var equalityResult1 = new PDAEqualityResult <StackSymbolSequence <char>, StackSymbolSequence <char> >(exp, act, "abc", 7, 300000, 3000);

            Assert.IsTrue(equalityResult1.AreEqual);

            var equalityResult2 = new PDAEqualityResult <char, StackSymbolSequence <char> >(dpda, act, "abc", 10, 600000, 3000);

            Assert.IsTrue(equalityResult2.AreEqual);

            //this is actually unnecessary
            Assert.IsTrue(act.AcceptsWord("aabbcc").Accepts());
            Assert.IsTrue(!act.AcceptsWord("aabbc").Accepts());
            Assert.IsTrue(!act.AcceptsWord("aabcc").Accepts());
            Assert.IsTrue(!act.AcceptsWord("abbcc").Accepts());
            Assert.IsTrue(!act.AcceptsWord("").Accepts());
        }
Example #18
0
        public void DPDASimulationRunnerTest()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.FinalStateAndEmptyStack(), true, 'Z', false, "ZXY");

            dpda.AddState(1, false);
            dpda.AddState(2, true);

            dpda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            dpda.AddTransition().From(0).To(1).Read('v').Pop('Z').Push("Z");
            dpda.AddTransition().From(0).To(1).Read('v').Pop('X').Push("X");
            dpda.AddTransition().From(0).To(1).Read('v').Pop('Y').Push("Y");

            dpda.AddTransition().From(1).To(1).Read('a').Pop('X').Push();
            dpda.AddTransition().From(1).To(1).Read('b').Pop('Y').Push();
            dpda.AddTransition().From(1).To(2).Read().Pop('Z').Push("");

            var word = "aababbbavabbbabaa".ToCharArray();

            var watch = new Stopwatch();

            watch.Start();
            var path = DPDASimulationRunner <char, char> .RunSimulation(dpda, word, dpda.AcceptanceCondition);

            watch.Stop();

            Console.WriteLine(string.Format("For DPDA runner: {0}", watch.Elapsed));

            var act = path.ToXml();

            var         reader    = new StreamReader(@"..\..\..\TestPDL\PDATest\XmlPathCFG4.xml");
            XmlDocument expXmlDoc = new XmlDocument();

            expXmlDoc.Load(reader);
            XDocument xDoc = XDocument.Load(new XmlNodeReader(expXmlDoc));
            var       exp  = xDoc.Root;

            Assert.IsTrue(XNode.DeepEquals(exp, act));

            watch.Restart();
            path = CFGSimulationRunner <char> .RunSimulation(dpda, word);

            watch.Stop();

            Console.WriteLine(string.Format("For CFG runner: {0}", watch.Elapsed));

            Assert.IsTrue(XNode.DeepEquals(exp, act));

            path = DPDASimulationRunner <char, char> .RunSimulation(dpda, word.Concat("a").ToArray(), dpda.AcceptanceCondition);

            act = path.ToXml();

            reader    = new StreamReader(@"..\..\..\TestPDL\PDATest\XmlPathCFG5.xml");
            expXmlDoc = new XmlDocument();
            expXmlDoc.Load(reader);
            xDoc = XDocument.Load(new XmlNodeReader(expXmlDoc));
            exp  = xDoc.Root;

            Assert.IsTrue(XNode.DeepEquals(exp, act));
        }
        public void ToDPDAWithEmptyStackTest()
        {
            var dpda = new PDA <char, char>(new AcceptanceCondition.FinalState(), true, 'Z', false, "ZYX", 'B');

            dpda.AddState(1, false);
            dpda.AddState(2, true);

            dpda.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("XZ");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('X').Push("XX");
            dpda.AddTransition().From(0).To(0).Read('a').Pop('Y').Push("XY");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("YZ");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('X').Push("YX");
            dpda.AddTransition().From(0).To(0).Read('b').Pop('Y').Push("YY");

            dpda.AddTransition().From(0).To(1).Read('x').Pop('Z').Push("Z");
            dpda.AddTransition().From(0).To(1).Read('x').Pop('X').Push("X");
            dpda.AddTransition().From(0).To(1).Read('x').Pop('Y').Push("Y");

            dpda.AddTransition().From(1).To(1).Read('a').Pop('X').Push("");
            dpda.AddTransition().From(1).To(1).Read('b').Pop('Y').Push("");

            dpda.AddTransition().From(1).To(2).Read().Pop('Z').Push("Z");

            var exp = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), true, 'B', false, "BZYX");

            exp.AddState(1, false);
            exp.AddState(2, false);
            exp.AddState(3, false);
            exp.AddState(4, false);

            exp.AddTransition().From(0).To(1).Read().Pop('B').Push("ZB");

            exp.AddTransition().From(1).To(1).Read('a').Pop('Z').Push("XZ");
            exp.AddTransition().From(1).To(1).Read('a').Pop('X').Push("XX");
            exp.AddTransition().From(1).To(1).Read('a').Pop('Y').Push("XY");
            exp.AddTransition().From(1).To(1).Read('b').Pop('Z').Push("YZ");
            exp.AddTransition().From(1).To(1).Read('b').Pop('X').Push("YX");
            exp.AddTransition().From(1).To(1).Read('b').Pop('Y').Push("YY");

            exp.AddTransition().From(1).To(2).Read('x').Pop('Z').Push("Z");
            exp.AddTransition().From(1).To(2).Read('x').Pop('X').Push("X");
            exp.AddTransition().From(1).To(2).Read('x').Pop('Y').Push("Y");

            exp.AddTransition().From(2).To(2).Read('a').Pop('X').Push("");
            exp.AddTransition().From(2).To(2).Read('b').Pop('Y').Push("");

            exp.AddTransition().From(2).To(3).Read().Pop('Z').Push("Z");

            exp.AddTransition().From(3).To(4).Read('$').Pop('Z').Push();
            exp.AddTransition().From(3).To(4).Read('$').Pop('X').Push();
            exp.AddTransition().From(3).To(4).Read('$').Pop('Y').Push();
            exp.AddTransition().From(3).To(4).Read('$').Pop('B').Push();

            exp.AddTransition().From(4).To(4).Read().Pop('Y').Push();
            exp.AddTransition().From(4).To(4).Read().Pop('X').Push();
            exp.AddTransition().From(4).To(4).Read().Pop('Z').Push();
            exp.AddTransition().From(4).To(4).Read().Pop('B').Push();

            var act = PDATransformer <char, char> .ToDPDAWithEmptyStack(dpda, '$');

            Assert.IsTrue(act.Equals(exp));

            act.CreateRunner();

            Assert.IsFalse(act.AcceptsWord("").Accepts());
            Assert.IsFalse(act.AcceptsWord("ax").Accepts());
            Assert.IsFalse(act.AcceptsWord("aaxa").Accepts());
            Assert.IsFalse(act.AcceptsWord("xa").Accepts());
            Assert.IsFalse(act.AcceptsWord("axaa").Accepts());
            Assert.IsFalse(act.AcceptsWord("ax$").Accepts());
            Assert.IsFalse(act.AcceptsWord("aaxa$").Accepts());
            Assert.IsFalse(act.AcceptsWord("xa$").Accepts());
            Assert.IsFalse(act.AcceptsWord("axaa$").Accepts());
            Assert.IsFalse(act.AcceptsWord("$").Accepts());

            Assert.IsTrue(act.AcceptsWord("x$").Accepts());
            Assert.IsTrue(act.AcceptsWord("axa$").Accepts());
            Assert.IsTrue(act.AcceptsWord("aaxaa$").Accepts());
            Assert.IsTrue(act.AcceptsWord("aaaxaaa$").Accepts());
        }
Example #20
0
        public void TestMethod()
        {
            var pdaMirrored = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZAB");

            pdaMirrored.AddState(1, false);

            pdaMirrored.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("AZ");
            pdaMirrored.AddTransition().From(0).To(0).Read('a').Pop('A').Push("AA");
            pdaMirrored.AddTransition().From(0).To(0).Read('a').Pop('B').Push("AB");
            pdaMirrored.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("BZ");
            pdaMirrored.AddTransition().From(0).To(0).Read('b').Pop('A').Push("BA");
            pdaMirrored.AddTransition().From(0).To(0).Read('b').Pop('B').Push("BB");

            pdaMirrored.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pdaMirrored.AddTransition().From(0).To(1).Read().Pop('A').Push("A");
            pdaMirrored.AddTransition().From(0).To(1).Read().Pop('B').Push("B");
            pdaMirrored.AddTransition().From(0).To(1).Read('a').Pop('Z').Push("Z");
            pdaMirrored.AddTransition().From(0).To(1).Read('a').Pop('A').Push("A");
            pdaMirrored.AddTransition().From(0).To(1).Read('a').Pop('B').Push("B");
            pdaMirrored.AddTransition().From(0).To(1).Read('b').Pop('Z').Push("Z");
            pdaMirrored.AddTransition().From(0).To(1).Read('b').Pop('A').Push("A");
            pdaMirrored.AddTransition().From(0).To(1).Read('b').Pop('B').Push("B");

            pdaMirrored.AddTransition().From(1).To(1).Read('a').Pop('A').Push();
            pdaMirrored.AddTransition().From(1).To(1).Read('b').Pop('B').Push();
            pdaMirrored.AddTransition().From(1).To(1).Read().Pop('Z').Push();

            pdaMirrored.CreateRunner();


            var pdaMirroredEven = new PDA <char, char>(new AcceptanceCondition.EmptyStack(), false, 'Z', false, "ZAB");

            pdaMirroredEven.AddState(1, false);

            pdaMirroredEven.AddTransition().From(0).To(0).Read('a').Pop('Z').Push("AZ");
            pdaMirroredEven.AddTransition().From(0).To(0).Read('a').Pop('A').Push("AA");
            pdaMirroredEven.AddTransition().From(0).To(0).Read('a').Pop('B').Push("AB");
            pdaMirroredEven.AddTransition().From(0).To(0).Read('b').Pop('Z').Push("BZ");
            pdaMirroredEven.AddTransition().From(0).To(0).Read('b').Pop('A').Push("BA");
            pdaMirroredEven.AddTransition().From(0).To(0).Read('b').Pop('B').Push("BB");

            pdaMirroredEven.AddTransition().From(0).To(1).Read().Pop('Z').Push("Z");
            pdaMirroredEven.AddTransition().From(0).To(1).Read().Pop('A').Push("A");
            pdaMirroredEven.AddTransition().From(0).To(1).Read().Pop('B').Push("B");
            pdaMirroredEven.AddTransition().From(0).To(1).Read('a').Pop('Z').Push("Z");
            pdaMirroredEven.AddTransition().From(0).To(1).Read('a').Pop('A').Push("A");
            pdaMirroredEven.AddTransition().From(0).To(1).Read('a').Pop('B').Push("B");
            pdaMirroredEven.AddTransition().From(0).To(1).Read('b').Pop('Z').Push("Z");
            pdaMirroredEven.AddTransition().From(0).To(1).Read('b').Pop('A').Push("A");

            pdaMirroredEven.AddTransition().From(1).To(1).Read('a').Pop('A').Push();
            pdaMirroredEven.AddTransition().From(1).To(1).Read('b').Pop('B').Push();
            pdaMirroredEven.AddTransition().From(1).To(1).Read().Pop('Z').Push();
            pdaMirroredEven.AddTransition().From(1).To(1).Read('a').Pop('Z').Push();

            pdaMirroredEven.CreateRunner();

            var s       = new Stopwatch();
            var results = new Dictionary <int, List <Tuple <bool, TimeSpan> > >();

            for (int i = 1; i <= 15; i++)
            {
                results[i] = new List <Tuple <bool, TimeSpan> >();
            }

            for (int j = 0; j < 1; j++) //TODO: count until 10 for a good average
            {
                for (int i = 1; i <= 15; i++)
                {
                    s.Restart();
                    var eq = new PDAEqualityResult <char, char>(pdaMirrored, pdaMirroredEven, "ab", i, 65535, 20000);
                    s.Stop();
                    results[i].Add(new Tuple <bool, TimeSpan>(eq.AreEqual, s.Elapsed));
                }
            }

            foreach (var t in results)
            {
                var equal = t.Value.All(v => v.Item1);
                var time  = t.Value.Average(v => v.Item2.TotalMilliseconds);
                Console.WriteLine(string.Format("Maximum word length: {0}; Equal: {1}; Time: {2}", t.Key, equal, time));
            }
        }