示例#1
0
 //Regram --> DFA
 public static DFA <MultiState <T>, S> ConvertToDFA(Regram <T, S> regram, IMultiStateView <T> view)
 {
     return(Converter <T, S> .GrammarConvertToDFA(regram, new MultiState <T>(new T[] { regram.StartState }, view), view, default(S)));
 }
示例#2
0
        private static DFA <MultiState <T>, S> GrammarConvertToDFA(Grammar <T, S> grammar, MultiState <T> startState, IMultiStateView <T> view, S epsilon)
        {
            DFA <MultiState <T>, S>   dfa  = new DFA <MultiState <T>, S>(grammar.Alphabet);
            HashSet <MultiState <T> > todo = new HashSet <MultiState <T> >();
            HashSet <MultiState <T> > done = new HashSet <MultiState <T> >();

            MultiState <T> newStartState = new MultiState <T>(view);

            foreach (T subStartState in startState)
            {
                newStartState.Add(subStartState);

                HashSet <T> otherStates = getAllEpsilonStatesFromState(grammar, subStartState, epsilon);
                foreach (T otherState in otherStates)
                {
                    newStartState.Add(otherState);
                }
            }

            startState = newStartState;

            todo.Add(startState);

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

                Dictionary <S, MultiState <T> > states = new Dictionary <S, MultiState <T> >();

                foreach (T part in from)
                {
                    Dictionary <S, HashSet <T> > partStates = grammar.GetStates(part);

                    foreach (S symbol in grammar.Alphabet)
                    {
                        if (!states.ContainsKey(symbol))
                        {
                            states.Add(symbol, new MultiState <T>(view));
                        }

                        if (partStates.ContainsKey(symbol))
                        {
                            HashSet <T> partToStates = partStates[symbol];
                            foreach (T partToState in partToStates)
                            {
                                states[symbol].Add(partToState);
                                foreach (T epsilonState in getAllEpsilonStatesFromState(grammar, partToState, epsilon))
                                {
                                    states[symbol].Add(epsilonState);
                                }
                            }
                        }
                    }
                }

                foreach (S symbol in grammar.Alphabet)
                {
                    dfa.AddTransition(from, states[symbol], symbol);
                    if (!done.Contains(states[symbol]))
                    {
                        //If the to state is 'empty'
                        if (states[symbol].Count == 0)
                        {
                            //Add fail state
                            done.Add(states[symbol]);
                            foreach (S s in grammar.Alphabet)
                            {
                                dfa.AddTransition(states[symbol], s);
                            }
                        }
                        else
                        {
                            todo.Add(states[symbol]);
                        }
                    }
                }

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

            foreach (MultiState <T> state in dfa.GetStates())
            {
                foreach (T partState in state)
                {
                    if (grammar.EndStates.Contains(partState))
                    {
                        dfa.EndStates.Add(state);
                    }
                }
            }
            dfa.StartState = startState;
            return(dfa);
        }
示例#3
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);
        }
示例#4
0
 //NDFA --> DFA
 public static DFA <MultiState <T>, S> ConvertToDFA(NDFA <T, S> ndfa, IMultiStateView <T> view)
 {
     return(Converter <T, S> .GrammarConvertToDFA(ndfa, new MultiState <T>(ndfa.StartStates, view), view, ndfa.Epsilon));
 }
示例#5
0
 public MultiState(IEnumerable <T> collection, IEqualityComparer <T> comparer, IMultiStateView <T> view)
     : base(collection, comparer)
 {
     this.View = view;
 }
示例#6
0
 public MultiState(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, IMultiStateView <T> view)
     : base(info, context)
 {
     this.View = view;
 }
示例#7
0
 public MultiState(IEqualityComparer <T> comparer, IMultiStateView <T> view)
     : base(comparer)
 {
     this.View = view;
 }
示例#8
0
 public MultiState(IEnumerable <T> collection, IMultiStateView <T> view)
     : base(collection)
 {
     this.View = view;
 }
示例#9
0
 public MultiState(IMultiStateView <T> view) : base()
 {
     this.View = view;
 }