Esempio n. 1
0
        //Regram --> NDFA
        public static NDFA <T, S> ConvertToNDFA(Regram <T, S> regram, S epsilon)
        {
            NDFA <T, S> ndfa = new NDFA <T, S>(regram.Alphabet, epsilon);

            foreach (T fromState in regram.GetStates())
            {
                Dictionary <S, HashSet <T> > states = regram.GetStates(fromState);
                foreach (S key in states.Keys)
                {
                    foreach (T toState in states[key])
                    {
                        ndfa.AddTransition(fromState, toState, key);
                    }
                }
            }

            foreach (T endstate in regram.EndStates)
            {
                ndfa.EndStates.Add(endstate);
            }
            ndfa.StartState = regram.StartState;
            return(ndfa);
        }
Esempio n. 2
0
        //NDFA --> Regram
        public static Regram <MultiState <T>, S> ConvertToRegram(NDFA <T, S> ndfa, IMultiStateView <T> view)
        {
            Regram <MultiState <T>, S> regram     = new Regram <MultiState <T>, S>(ndfa.Alphabet);
            MultiState <T>             startState = new MultiState <T>(view);
            HashSet <MultiState <T> >  todo       = new HashSet <MultiState <T> >();
            HashSet <MultiState <T> >  done       = new HashSet <MultiState <T> >();

            foreach (T subStartState in ndfa.StartStates)
            {
                startState.Add(subStartState);

                HashSet <T> otherStates = getAllEpsilonStatesFromState(ndfa, subStartState, ndfa.Epsilon);
                foreach (T otherState in otherStates)
                {
                    startState.Add(otherState);
                }
            }

            todo.Add(startState);

            //While there are items that needs to be progressed
            while (todo.Count > 0)
            {
                MultiState <T> from = todo.First <MultiState <T> >();

                foreach (T part in from)
                {
                    if (isRegramStateValid(ndfa, part))
                    {
                        regram.AddState(from);
                    }

                    Dictionary <S, HashSet <T> > partStates = ndfa.GetStates(part);

                    foreach (S symbol in partStates.Keys)
                    {
                        if (symbol.Equals(ndfa.Epsilon))
                        {
                            continue;
                        }

                        HashSet <T> partToStates = partStates[symbol];
                        foreach (T partToState in partToStates)
                        {
                            MultiState <T> newState = new MultiState <T>(view);
                            newState.Add(partToState);
                            if (isRegramStateValid(ndfa, partToState) || ndfa.EndStates.Contains(partToState))
                            {
                                regram.AddTransition(from, newState, symbol);
                            }

                            if (!done.Contains(newState))
                            {
                                todo.Add(newState);
                            }

                            foreach (T epsilonState in getAllEpsilonStatesFromState(ndfa, partToState, ndfa.Epsilon))
                            {
                                MultiState <T> newEpsilonState = new MultiState <T>(view);
                                newEpsilonState.Add(epsilonState);
                                if (isRegramStateValid(ndfa, epsilonState) || ndfa.EndStates.Contains(epsilonState))
                                {
                                    regram.AddTransition(from, newEpsilonState, symbol);
                                }

                                if (!done.Contains(newEpsilonState))
                                {
                                    todo.Add(newEpsilonState);
                                }
                            }
                        }
                    }
                }

                todo.Remove(from);
                done.Add(from);
            }

            foreach (MultiState <T> state in regram.GetStates().Where((t) => t.Any((a) => ndfa.EndStates.Contains(a))))
            {
                regram.EndStates.Add(state);
            }
            regram.StartState = startState;
            return(regram);
        }