Exemplo n.º 1
0
        /** Optimize an alternative (list of grammar elements).
         *
         *  Walk the chain of elements (which can be complicated loop blocks...)
         *  and throw away any epsilon transitions used to link up simple elements.
         *
         *  This only removes 195 states from the java.g's NFA, but every little
         *  bit helps.  Perhaps I can improve in the future.
         */
        public virtual void OptimizeAlternative(StateCluster alt)
        {
            NFAState s = alt.left;

            while (s != alt.right)
            {
                // if it's a block element, jump over it and continue
                if (s.endOfBlockStateNumber != State.INVALID_STATE_NUMBER)
                {
                    s = nfa.GetState(s.endOfBlockStateNumber);
                    continue;
                }
                Transition t = s.transition[0];
                if (t is RuleClosureTransition)
                {
                    s = ((RuleClosureTransition)t).followState;
                    continue;
                }
                if (t.label.IsEpsilon && !t.label.IsAction && s.NumberOfTransitions == 1)
                {
                    // bypass epsilon transition and point to what the epsilon's
                    // target points to unless that epsilon transition points to
                    // a block or loop etc..  Also don't collapse epsilons that
                    // point at the last node of the alt. Don't collapse action edges
                    NFAState epsilonTarget = (NFAState)t.target;
                    if (epsilonTarget.endOfBlockStateNumber == State.INVALID_STATE_NUMBER &&
                        epsilonTarget.transition[0] != null)
                    {
                        s.SetTransition0(epsilonTarget.transition[0]);
                        //System.Console.Out.WriteLine( "### opt " + s.stateNumber + "->" + epsilonTarget.transition[0].target.stateNumber );
                    }
                }
                s = (NFAState)t.target;
            }
        }