Beispiel #1
0
/*
 * Function: machine
 * Description: Recursive descent regular expression parser.
 */
        private static Nfa machine()
        {
            Nfa    start;
            Nfa    p;
            BitSet states;

#if DESCENT_DEBUG
            Utility.enter("machine", spec.lexeme, spec.current_token);
#endif

            start = Alloc.NewNfa(spec);
            p     = start;

            states = gen.GetStates();

            /* Begin: Added for states. */
            spec.current_token = Gen.EOS;
            gen.Advance();
            /* End: Added for states. */

            if (Gen.END_OF_INPUT != spec.current_token)
            {
                p.SetNext(rule());
                ProcessStates(states, p.GetNext());
            }

            while (Gen.END_OF_INPUT != spec.current_token)
            {
                /* Make state changes HERE. */
                states = gen.GetStates();

                /* Begin: Added for states. */
                gen.Advance();
                if (Gen.END_OF_INPUT == spec.current_token)
                {
                    break;
                }
                /* End: Added for states. */

                p.SetSib(Alloc.NewNfa(spec));
                p = p.GetSib();
                p.SetNext(rule());

                ProcessStates(states, p.GetNext());
            }

            /*
             * add pseudo-rules for BOL and EOF
             */
            p.SetSib(Alloc.NewNfa(spec));
            p = p.GetSib();
            p.SetNext(Alloc.NewNfa(spec));
            Nfa pnext = p.GetNext();
            pnext.SetEdge(Nfa.CCL);
            pnext.SetNext(Alloc.NewNfa(spec));
            pnext.SetCharSet(new CharSet());
            pnext.GetCharSet().add(spec.BOL);
            pnext.GetCharSet().add(spec.EOF);

            // do-nothing accept rule
            pnext.GetNext().SetAccept(new Accept(null, input.line_number + 1));

            /* add the pseudo rules */
            for (int i = 0; i < spec.states.Count; i++)
            {
                ArrayList srule = spec.state_rules[i];
                srule.Add(pnext);
            }

#if DESCENT_DEBUG
            Utility.leave("machine", spec.lexeme, spec.current_token);
#endif

            return(start);
        }