Example #1
0
        public Dfa ConstructDfaUsingPowerSet()
        {
            Dfa dfa = new Dfa(StartState);
            Stack <AutomataState> frontier = new Stack <AutomataState>();

            frontier.Push(StartState);
            HashSet <AutomataState> seen = new HashSet <AutomataState>();

            while (frontier.Count > 0)
            {
                AutomataState state = frontier.Pop();

                IEnumerable <char> inputs = _GetInputs(state);
                foreach (char input in inputs)
                {
                    if (input.Equals(Epsilon))
                    {
                        continue;
                    }
                    AutomataState nextState = _NextState(state, input);

                    if (!seen.Contains(nextState))
                    {
                        frontier.Push(nextState);
                        seen.Add(nextState);
                        if (_IsFinal(nextState))
                        {
                            dfa.AddFinalState(nextState);
                        }
                    }

                    if (input == Any)
                    {
                        if (!dfa.HasTransition(state, input, nextState))
                        {
                            dfa.SetDefaultTransition(state, nextState);
                        }
                    }
                    else
                    {
                        if (!dfa.HasDefaultTransition(state, nextState))
                        {
                            dfa.AddTransition(state, input, nextState);
                        }
                    }
                }
            }

            return(dfa);
        }
Example #2
0
        public Dfa ConstructDfaUsingPowerSet()
        {
            Dfa dfa = new Dfa(StartState);
            Stack<AutomataState> frontier = new Stack<AutomataState>();
            frontier.Push(StartState);
            HashSet<AutomataState> seen = new HashSet<AutomataState>();

            while (frontier.Count > 0)
            {
                AutomataState state = frontier.Pop();

                IEnumerable<char> inputs = _GetInputs(state);
                foreach (char input in inputs)
                {
                    if (input.Equals(Epsilon)) continue;
                    AutomataState nextState = _NextState(state, input);

                    if (!seen.Contains(nextState))
                    {
                        frontier.Push(nextState);
                        seen.Add(nextState);
                        if (_IsFinal(nextState))
                            dfa.AddFinalState(nextState);
                    }

                    if (input == Any)
                    {
                        if (!dfa.HasTransition(state, input, nextState))
                            dfa.SetDefaultTransition(state, nextState);
                    }
                    else
                    {
                        if (!dfa.HasDefaultTransition(state, nextState))
                            dfa.AddTransition(state, input, nextState);
                    }
                }
            }

            return dfa;
        }