Beispiel #1
0
        public void SimplePalTest()
        {
            var q  = new States(3);
            var f  = new AcceptingStates(q[2]);
            var tf = new PushdownTransitionFunction()
            {
                new PushdownTransition(q[0], 'a', Alphabet.Z, q[0], $"a{Alphabet.Z}"),
                new PushdownTransition(q[0], 'b', Alphabet.Z, q[0], $"b{Alphabet.Z}"),
                new PushdownTransition(q[0], 'a', 'a', q[0], "aa"),
                new PushdownTransition(q[0], 'b', 'b', q[0], "bb"),
                new PushdownTransition(q[0], 'b', 'a', q[0], "ba"),
                new PushdownTransition(q[0], 'a', 'b', q[0], "ab"),
                new PushdownTransition(q[0], 'c', 'a', q[1], "a"),
                new PushdownTransition(q[0], 'c', 'b', q[1], "b"),
                new PushdownTransition(q[0], 'c', Alphabet.Z, q[1], $"{Alphabet.Z}"),
                new PushdownTransition(q[1], 'a', 'a', q[1], Alphabet.EmptyString.ToString()),
                new PushdownTransition(q[1], 'b', 'b', q[1], Alphabet.EmptyString.ToString()),
                new PushdownTransition(q[1], Alphabet.EmptyString, Alphabet.Z, q[2], Alphabet.Z.ToString())
            };
            var m = new PushdownAutomaton(q, Alphabet.Abc, new StackAlphabet(Alphabet.Abc), tf, q[0], f);

            Assert.True(m.Run("aca"));
            Assert.True(m.Run("abacaba"));
            Assert.True(m.Run("c"));
            Assert.True(m.Run("bacab"));
            Assert.False(m.Run("ab"));
            Assert.False(m.Run("babb"));
            Assert.False(m.Run("acab"));
            Assert.False(m.Run("bacba"));
        }
Beispiel #2
0
        public void SimpleReverseWordTestInvalid()
        {
            var transitions = new [] {
                new Transition(new State("S0"), new Symbol("1"), new Symbol("Z0"), new State("Q1"), new Symbol[] { new Symbol("1"), new Symbol("Z0") }),
                new Transition(new State("S0"), new Symbol("2"), new Symbol("Z0"), new State("Q1"), new Symbol[] { new Symbol("2"), new Symbol("Z0") }),

                new Transition(new State("Q1"), new Symbol("1"), new Symbol("1"), new State("Q1"), new Symbol[] { new Symbol("1"), new Symbol("1") }),
                new Transition(new State("Q1"), new Symbol("1"), new Symbol("2"), new State("Q1"), new Symbol[] { new Symbol("1"), new Symbol("2") }),
                new Transition(new State("Q1"), new Symbol("2"), new Symbol("1"), new State("Q1"), new Symbol[] { new Symbol("2"), new Symbol("1") }),
                new Transition(new State("Q1"), new Symbol("2"), new Symbol("2"), new State("Q1"), new Symbol[] { new Symbol("2"), new Symbol("2") }),

                new Transition(new State("Q1"), new Symbol("0"), new Symbol("1"), new State("Q2"), new Symbol[] { new Symbol("1") }),
                new Transition(new State("Q1"), new Symbol("0"), new Symbol("2"), new State("Q2"), new Symbol[] { new Symbol("2") }),

                new Transition(new State("Q2"), new Symbol("1"), new Symbol("1"), new State("Q2"), new Symbol[] { }),
                new Transition(new State("Q2"), new Symbol("2"), new Symbol("2"), new State("Q2"), new Symbol[] { }),

                new Transition(new State("Q2"), new Symbol(null), new Symbol("Z0"), new State("F"), new Symbol[] { new Symbol("Z0") }),
            };

            var automaton = new PushdownAutomaton
            {
                AcceptingStates = new [] { new State("F") },
                StartState      = new State("S0"),
                Transitions     = transitions
            };

            Assert.Throws <NoTransitionFoundException>(() =>
                                                       ProcessInput(automaton, new [] { "1", "2", "2", "1", "1", "0", "1", "1", "2" })
                                                       );
        }
Beispiel #3
0
        public void SimpleReverseWordTest()
        {
            var transitions = new [] {
                new Transition(new State("S0"), new Symbol("1"), new Symbol("Z0"), new State("Q1"), new Symbol[] { new Symbol("1"), new Symbol("Z0") }),
                new Transition(new State("S0"), new Symbol("2"), new Symbol("Z0"), new State("Q1"), new Symbol[] { new Symbol("2"), new Symbol("Z0") }),

                new Transition(new State("Q1"), new Symbol("1"), new Symbol("1"), new State("Q1"), new Symbol[] { new Symbol("1"), new Symbol("1") }),
                new Transition(new State("Q1"), new Symbol("1"), new Symbol("2"), new State("Q1"), new Symbol[] { new Symbol("1"), new Symbol("2") }),
                new Transition(new State("Q1"), new Symbol("2"), new Symbol("1"), new State("Q1"), new Symbol[] { new Symbol("2"), new Symbol("1") }),
                new Transition(new State("Q1"), new Symbol("2"), new Symbol("2"), new State("Q1"), new Symbol[] { new Symbol("2"), new Symbol("2") }),

                new Transition(new State("Q1"), new Symbol("0"), new Symbol("1"), new State("Q2"), new Symbol[] { new Symbol("1") }),
                new Transition(new State("Q1"), new Symbol("0"), new Symbol("2"), new State("Q2"), new Symbol[] { new Symbol("2") }),

                new Transition(new State("Q2"), new Symbol("1"), new Symbol("1"), new State("Q2"), new Symbol[] { }),
                new Transition(new State("Q2"), new Symbol("2"), new Symbol("2"), new State("Q2"), new Symbol[] { }),

                new Transition(new State("Q2"), new Symbol(null), new Symbol("Z0"), new State("F"), new Symbol[] { new Symbol("Z0") }),
            };

            var automaton = new PushdownAutomaton
            {
                AcceptingStates = new [] { new State("F") },
                StartState      = new State("S0"),
                Transitions     = transitions
            };

            ProcessInput(automaton, new [] { "1", "2", "2", "1", "1", "0", "1", "1", "2", "2", "1" });

            Assert.True(automaton.IsInAcceptingState);
            Assert.Single(automaton.GetStackContents());
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            string file = "Program.txt";

            do
            {
                Console.Write("File:");
                string s = Console.ReadLine();
                if (s == "q")
                {
                    break;
                }
                if (s != "p")
                {
                    file = s;
                }
                PushdownAutomaton PA = PushdownAutomaton.fromFile(file);
                if (PA.FindCycle(out string cc))
                {
                    Console.WriteLine("Найден цикл!!"); Console.WriteLine($"Зацикливающая конфигурация: {cc}");
                }
                else
                {
                    Console.Write("Input:");
                    Console.WriteLine(PA.Process(Console.ReadLine()));
                    Console.ReadKey();
                    Console.Clear();
                }
            }while (true);
        }
Beispiel #5
0
        private void ProcessInput(PushdownAutomaton automaton, string[] inputs)
        {
            foreach (var input in inputs)
            {
                automaton.Read(new Symbol(input));
            }

            automaton.Read(new Symbol(null));
        }
Beispiel #6
0
        public void NoTransition_ShouldThrow()
        {
            var automaton = new PushdownAutomaton
            {
                StartState = new State("S1")
            };

            Assert.Throws <NoTransitionFoundException>(() => {
                automaton.Read(new Symbol("0"));
            });
        }
Beispiel #7
0
        public void ValidTransition_ShouldPushMoveAndEndInAcceptingState()
        {
            var startState   = new State("S1");
            var otherState   = new State("S2");
            var finalState   = new State("F");
            var pushedSymbol = new Symbol("1");
            var inputSymbol  = new Symbol("0");

            var automaton = new PushdownAutomaton
            {
                StartState  = new State("S1"),
                Transitions = new [] {
                    new Transition(
                        startState,
                        inputSymbol,
                        new Symbol("Z0"),
                        otherState,
                        new Symbol[] { pushedSymbol, new Symbol("Z0") }
                        ),
                    new Transition(
                        otherState,
                        inputSymbol,
                        pushedSymbol,
                        finalState,
                        new Symbol[] { }
                        )
                },
                AcceptingStates = new [] { finalState }
            };

            automaton.Read(inputSymbol);

            Assert.Equal(otherState, automaton.State);
            Assert.Collection(
                automaton.GetStackContents(),
                actual => Assert.Equal(pushedSymbol, actual),
                actual => Assert.Equal(new Symbol("Z0"), actual)
                );
            Assert.False(automaton.IsInAcceptingState);

            automaton.Read(inputSymbol);

            Assert.Equal(finalState, automaton.State);
            Assert.Collection(
                automaton.GetStackContents(),
                actual => Assert.Equal(new Symbol("Z0"), actual)
                );
            Assert.True(automaton.IsInAcceptingState);
        }
Beispiel #8
0
        // {0^n1^n | n >= 0}
        public void ZeroNOneNTest()
        {
            var q  = new States(3);
            var f  = new AcceptingStates(q[0], q[2]);
            var tf = new PushdownTransitionFunction()
            {
                new PushdownTransition(q[0], '0', Alphabet.Z, q[0], $"0{Alphabet.Z}"),
                new PushdownTransition(q[0], '0', '0', q[0], "00"),
                new PushdownTransition(q[0], '1', '0', q[1], new [] { Alphabet.EmptyString }),
                new PushdownTransition(q[1], '1', '0', q[1], new [] { Alphabet.EmptyString }),
                new PushdownTransition(q[1], Alphabet.EmptyString, Alphabet.Z, q[2], new [] { Alphabet.Z })
            };
            var m = new PushdownAutomaton(q, Alphabet.Binary, new StackAlphabet(Alphabet.Binary), tf, q[0], f);

            Assert.True(m.Run(""));
            Assert.True(m.Run("0011"));
            Assert.True(m.Run("01"));
            Assert.True(m.Run("0000011111"));
            Assert.False(m.Run("011010"));
            Assert.False(m.Run("101010001"));
            Assert.False(m.Run("111000"));
        }
Beispiel #9
0
        /// <summary>
        /// Converts source code string into well formed S-Expressions.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static IReadOnlyList <SExpression> Read(IEnumerable <char> input)
        {
            PushdownAutomaton pda = new PushdownAutomaton();

            // Process input char stream
            foreach (var ch in input)
            {
                switch (pda.CurrentState)
                {
                case PushdownAutomaton.State.Listening:
                    if (Utils.IsLeftBracket(ch))
                    {
                        pda.Push();
                    }
                    else if (Utils.IsRightBracket(ch))
                    {
                        pda.PopAndCheck();
                    }
                    else if (char.IsWhiteSpace(ch))
                    {
                        // do nothing
                    }
                    else if (ch == ';')
                    {
                        pda.CurrentState = PushdownAutomaton.State.Comment;
                    }
                    else if (ch == '\"')
                    {
                        pda.AppendCharToCache(ch);
                        pda.CurrentState = PushdownAutomaton.State.String;
                    }
                    else
                    {
                        pda.AppendCharToCache(ch);
                        pda.CurrentState = PushdownAutomaton.State.Symbol;
                    }
                    break;

                case PushdownAutomaton.State.Symbol:
                    if (Utils.IsLeftBracket(ch))
                    {
                        pda.AddAndClearCache();
                        pda.Push();
                        pda.CurrentState = PushdownAutomaton.State.Listening;
                    }
                    else if (Utils.IsRightBracket(ch))
                    {
                        pda.AddAndClearCache();
                        pda.PopAndCheck();
                        pda.CurrentState = PushdownAutomaton.State.Listening;
                    }
                    else if (char.IsWhiteSpace(ch))
                    {
                        pda.AddAndClearCache();
                        pda.CurrentState = PushdownAutomaton.State.Listening;
                    }
                    else if (ch == ';')
                    {
                        pda.AddAndClearCache();
                        pda.CurrentState = PushdownAutomaton.State.Comment;
                    }
                    else if (ch == '\"')
                    {
                        pda.AddAndClearCache();
                        pda.AppendCharToCache(ch);
                        pda.CurrentState = PushdownAutomaton.State.String;
                    }
                    else
                    {
                        pda.AppendCharToCache(ch);
                    }
                    break;

                case PushdownAutomaton.State.String:
                    if (ch == '\"')
                    {
                        pda.AppendCharToCache(ch);
                        pda.AddAndClearCache();
                        pda.CurrentState = PushdownAutomaton.State.Listening;
                    }
                    else
                    {
                        pda.AppendCharToCache(ch);
                    }
                    break;

                case PushdownAutomaton.State.Comment:
                    if (ch == '\n')
                    {
                        pda.CurrentState = PushdownAutomaton.State.Listening;
                    }
                    break;

                default:
                    throw new ArgumentException($"Unknown state: {pda.CurrentState}");
                }
            }


            // Process the EOF
            if (pda.CurrentState == PushdownAutomaton.State.Symbol)
            {
                pda.AddAndClearCache();
            }
            else if (pda.CurrentState == PushdownAutomaton.State.String)
            {
                throw new ScannerErrorException("Unclosed string");
            }


            return(pda.CheckAndReturn());
        }