Пример #1
0
        /// <summary>
        /// Gets the new dfa states produced by this partition
        /// </summary>
        /// <returns>The dfa states produced by this partition</returns>
        public List <DFAState> GetDFAStates()
        {
            // For each group in the partition
            foreach (DFAStateGroup group in groups)
            {
                bool found = false;
                foreach (DFAState state in group.States)
                {
                    if (state.ID == 0)
                    {
                        groups.Remove(group);
                        groups.Insert(0, group);
                        found = true;
                        break;
                    }
                }
                if (found)
                {
                    break;
                }
            }

            List <DFAState> states = new List <DFAState>();

            // For each group in the partition
            // Create the coresponding state and add the finals
            foreach (DFAStateGroup group in groups)
            {
                // Create a new state
                DFAState newState = new DFAState(states.Count);
                // Add the terminal from the group to the new state
                newState.AddItems(group.Representative.Items);
                states.Add(newState);
            }
            // Do linkage
            for (int i = 0; i != groups.Count; i++)
            {
                foreach (CharSpan Key in groups[i].Representative.Transitions)
                {
                    states[i].AddTransition(Key, states[GetDFAStates_GetGroupIndexOf(groups[i].Representative.GetChildBy(Key))]);
                }
            }
            return(states);
        }
Пример #2
0
Файл: DFA.cs Проект: sebgod/hime
        /// <summary>
        /// Extracts the sub-DFA that leads to any of the specified states
        /// </summary>
        /// <param name="targets">States in this DFA</param>
        /// <returns>The sub-DFA for the specified states</returns>
        public DFA ExtractSubTo(IEnumerable <DFAState> targets)
        {
            if (targets == null)
            {
                throw new System.ArgumentException("The specified collection must not be null");
            }

            // build a list of all the required states
            DFAInverse      inverse   = new DFAInverse(this);
            List <DFAState> originals = inverse.CloseByAntecedents(targets);

            // setup the list
            int index = originals.IndexOf(states[0]);

            if (index == -1)
            {
                throw new System.ArgumentException("The specified states are not reachable from the starting state");
            }
            originals[index] = originals[0];
            originals[0]     = states[0];

            // copies the states
            List <DFAState> copy = new List <DFAState>();

            for (int i = 0; i != originals.Count; i++)
            {
                copy.Add(new DFAState(i));
            }
            for (int i = 0; i != originals.Count; i++)
            {
                DFAState original = originals[i];
                DFAState clone    = copy[i];
                clone.AddItems(original.Items);
                foreach (CharSpan key in original.Transitions)
                {
                    index = originals.IndexOf(original.GetChildBy(key));
                    if (index != -1)
                    {
                        clone.AddTransition(key, copy[index]);
                    }
                }
            }
            return(new DFA(copy));
        }