Esempio n. 1
0
        public AcceptanceResult <S> IsWordAccepted(IEnumerable <A> word)
        {
            var res = new Dictionary <string, bool>();

            if (dpda.AcceptanceCondition.IsFinalState())
            {
                res[AcceptanceCondition.FinalState.GetId()] = DPDASimulationRunner <A, S> .RunSimulation(dpda, word.ToArray(), new AcceptanceCondition.FinalState(), token).WordAccepted;
            }
            if (dpda.AcceptanceCondition.IsEmptyStack())
            {
                res[AcceptanceCondition.EmptyStack.GetId()] = DPDASimulationRunner <A, S> .RunSimulation(dpda, word.ToArray(), new AcceptanceCondition.EmptyStack(), token).WordAccepted;
            }
            return(new AcceptanceResult <S>(res));
        }
Esempio n. 2
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));
        }