Пример #1
0
Файл: DFA.cs Проект: sebgod/hime
        /// <summary>
        /// Initializes this dfa as equivalent to the given nfa
        /// </summary>
        /// <param name="nfa">A nfa</param>
        public DFA(NFA nfa)
        {
            states = new List <DFAState>();

            List <NFAStateSet> nfaStateSets = new List <NFAStateSet>();
            // Create the first NFA set, add the entry and close it
            NFAStateSet nfaInit = new NFAStateSet();

            nfaStateSets.Add(nfaInit);
            nfaInit.Add(nfa.StateEntry);
            nfaInit.Close();
            // Create the initial state for the DFA
            DFAState dfaInit = new DFAState(0);

            states.Add(dfaInit);

            // For each set in the list of the NFA states
            for (int i = 0; i != nfaStateSets.Count; i++)
            {
                // Normalize transitions
                nfaStateSets[i].Normalize();

                // Get the transitions for the set
                Dictionary <CharSpan, NFAStateSet> transitions = nfaStateSets[i].GetTransitions();
                // For each transition
                foreach (CharSpan value in transitions.Keys)
                {
                    // The child by the transition
                    NFAStateSet next = transitions[value];
                    // Search for the child in the existing sets
                    bool found = false;
                    for (int j = 0; j != nfaStateSets.Count; j++)
                    {
                        // An existing equivalent set is already present
                        if (nfaStateSets[j].Equals(next))
                        {
                            states[i].AddTransition(value, states[j]);
                            found = true;
                            break;
                        }
                    }
                    // The child is not already present
                    if (!found)
                    {
                        // Add to the sets list
                        nfaStateSets.Add(next);
                        // Create the corresponding DFA state
                        DFAState newState = new DFAState(states.Count);
                        states.Add(newState);
                        // Setup transition
                        states[i].AddTransition(value, newState);
                    }
                }
                // Add finals
                states[i].AddItems(nfaStateSets[i].GetFinals());
            }
        }
Пример #2
0
        /// <summary>
        /// Determines whether the given object is equal to this set
        /// </summary>
        /// <param name="obj">The object to compare</param>
        /// <returns>True of the object is equal to this set</returns>
        public override bool Equals(object obj)
        {
            NFAStateSet right = obj as NFAStateSet;

            if (right == null)
            {
                return(false);
            }
            if (backend.Count != right.backend.Count)
            {
                return(false);
            }
            foreach (NFAState state in backend)
            {
                if (!right.backend.Contains(state))
                {
                    return(false);
                }
            }
            return(true);
        }
Пример #3
0
        /// <summary>
        /// Gets transitions from this set to other sets
        /// </summary>
        /// <returns>The transitions from this set to other sets</returns>
        public Dictionary <CharSpan, NFAStateSet> GetTransitions()
        {
            Dictionary <CharSpan, NFAStateSet> transitions = new Dictionary <CharSpan, NFAStateSet>();

            // For each state
            foreach (NFAState State in backend)
            {
                // For each transition
                foreach (NFATransition transition in State.Transitions)
                {
                    // If this is an ε-transition : pass
                    if (transition.Span.Equals(NFA.EPSILON))
                    {
                        continue;
                    }
                    // Add the transition's target to set's transitions dictionnary
                    if (transitions.ContainsKey(transition.Span))
                    {
                        transitions[transition.Span].Add(transition.Next);
                    }
                    else
                    {
                        // Create a new child
                        NFAStateSet set = new NFAStateSet();
                        set.Add(transition.Next);
                        transitions.Add(transition.Span, set);
                    }
                }
            }
            // Close all children
            foreach (NFAStateSet set in transitions.Values)
            {
                set.Close();
            }
            return(transitions);
        }