Beispiel #1
0
        /**
         * Calculates the set of states that can be reached from another
         * set of states <code>start</code> with an specified input
         * character <code>input</code>
         *
         * @param start the set of states to start from
         * @param input the input character for which to search the next states
         *
         * @return the set of states that are reached from <code>start</code>
         *         via <code>input</code>
         */
        private StateSet DFAEdge(StateSet start, char input)
        {
            // Out.debug("Calculating DFAEdge for state set "+start+" and input '"+input+"'");

            tempStateSet.clear();

            states.reset(start);
            while (states.hasMoreElements())
            {
                tempStateSet.add(table[states.nextElement()][input]);
            }

            StateSet result = new StateSet(tempStateSet);

            states.reset(tempStateSet);
            while (states.hasMoreElements())
            {
                result.add(epsilon[states.nextElement()]);
            }

            // Out.debug("DFAEdge is : "+result);

            return(result);
        }
Beispiel #2
0
        private int _dfaStart;   // in nfa coordinates

        private void removeDead(int start)
        {
            // Out.debug("removeDead ("+start+")");

            if (visited[start] || live[start])
            {
                return;
            }
            visited[start] = true;

            // Out.debug("not yet visited");

            if (closure(start).isElement(_end))
            {
                live[start] = true;
            }

            // Out.debug("is final :"+live[start]);

            for (int i = 0; i < numInput; i++)
            {
                StateSet           nextState = closure(table[start][i]);
                StateSetEnumerator states    = nextState.states();
                while (states.hasMoreElements())
                {
                    int next = states.nextElement();

                    if (next != start)
                    {
                        removeDead(next);

                        if (live[next])
                        {
                            live[start] = true;
                        }
                        else
                        {
                            table[start][i] = null;
                        }
                    }
                }
            }

            StateSet           _nextState = closure(epsilon[start]);
            StateSetEnumerator _states    = _nextState.states();

            while (_states.hasMoreElements())
            {
                int next = _states.nextElement();

                if (next != start)
                {
                    removeDead(next);

                    if (live[next])
                    {
                        live[start] = true;
                    }
                }
            }

            // Out.debug("state "+start+" is live :"+live[start]);
        }
Beispiel #3
0
        /**
         * Returns an DFA that accepts the same language as this NFA.
         * This DFA is usualy not minimal.
         */
        public DFA getDFA()
        {
            Hashtable dfaStates = new PrettyHashtable(numStates);
            ArrayList dfaVector = new PrettyArrayList(numStates);

            DFA dfa = new DFA(2 * numLexStates, numInput);

            int numDFAStates    = 0;
            int currentDFAState = 0;

            Out.println("Converting NFA to DFA : ");

            epsilonFill();

            StateSet currentState, newState;

            for (int i = 0; i < 2 * numLexStates; i++)
            {
                newState = epsilon[i];

                dfaStates[newState] = new Integer(numDFAStates);
                dfaVector.Add(newState);

                dfa.setLexState(i, numDFAStates);

                dfa.setFinal(numDFAStates, containsFinal(newState));
                dfa.setPushback(numDFAStates, containsPushback(newState));
                dfa.setAction(numDFAStates, getAction(newState));

                numDFAStates++;
            }

            numDFAStates--;

            if (Options.DEBUG)
            {
                Out.debug("DFA start states are :" + Out.NL + dfaStates + Out.NL + Out.NL + "ordered :" + Out.NL + dfaVector);
            }

            currentDFAState = 0;

            StateSet           tempStateSet = NFA.tempStateSet;
            StateSetEnumerator states       = NFA.states;

            // will be reused
            newState = new StateSet(numStates);

            while (currentDFAState <= numDFAStates)
            {
                currentState = (StateSet)dfaVector[currentDFAState];

                for (char input = (char)0; input < numInput; input++)
                {
                    // newState = DFAEdge(currentState, input);

                    // inlining DFAEdge for performance:

                    // Out.debug("Calculating DFAEdge for state set "+currentState+" and input '"+input+"'");

                    tempStateSet.clear();
                    states.reset(currentState);
                    while (states.hasMoreElements())
                    {
                        tempStateSet.add(table[states.nextElement()][input]);
                    }

                    newState.copy(tempStateSet);

                    states.reset(tempStateSet);
                    while (states.hasMoreElements())
                    {
                        newState.add(epsilon[states.nextElement()]);
                    }

                    // Out.debug("DFAEdge is : "+newState);


                    if (newState.containsElements())
                    {
                        // Out.debug("DFAEdge for input "+(int)input+" and state set "+currentState+" is "+newState);

                        // Out.debug("Looking for state set "+newState);
                        Integer nextDFAState = (Integer)dfaStates[newState];

                        if (nextDFAState != null)
                        {
                            // Out.debug("FOUND!");
                            dfa.addTransition(currentDFAState, input, nextDFAState.intValue());
                        }
                        else
                        {
                            if (Options.progress)
                            {
                                Out.print(".");
                            }
                            // Out.debug("NOT FOUND!");
                            // Out.debug("Table was "+dfaStates);
                            numDFAStates++;

                            // make a new copy of newState to store in dfaStates
                            StateSet storeState = new StateSet(newState);

                            dfaStates[storeState] = new Integer(numDFAStates);
                            dfaVector.Add(storeState);

                            dfa.addTransition(currentDFAState, input, numDFAStates);
                            dfa.setFinal(numDFAStates, containsFinal(storeState));
                            dfa.setPushback(numDFAStates, containsPushback(storeState));
                            dfa.setAction(numDFAStates, getAction(storeState));
                        }
                    }
                }

                currentDFAState++;
            }

            if (Options.verbose)
            {
                Out.println("");
            }

            return(dfa);
        }
 public StateSet(StateSet set)
 {
     bits = new long[set.bits.Length];
     Array.Copy(set.bits, 0, bits, 0, set.bits.Length);
 }