Ejemplo n.º 1
0
        public void Accepts(string input)
        {
            var currentState = Q0;
            var steps        = new StringBuilder();

            foreach (var symbol in input.ToCharArray())
            {
                var transition = Delta.Find((t) =>
                {
                    if (t.Symbols == null)
                    {
                        return(t.StartState == currentState && t.Symbol == symbol);
                    }

                    if (t.StartState == currentState && t.Symbols.Contains(symbol))
                    {
                        t.ChosenSymbol = symbol;
                        return(true);
                    }
                    return(false);
                });
                if (transition == null)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Error.WriteLine("No transitions for current state and symbol");
                    Console.Error.WriteLine(steps);
                    return;
                }
                currentState = transition.EndState;
                steps.Append(transition + "\n");
            }
            if (F.Contains(currentState))
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Accepted the input with steps:\n" + steps);
                return;
            }
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Error.WriteLine("Stopped in state " + currentState +
                                    " which is not a final state.");
            Console.Error.WriteLine(steps);
        }