Exemplo n.º 1
0
        public bool Parse()
        {
            Initialize(); // allow derived classes to instantiate rules, states and nonTerminals

            next          = 0;
            current_state = states[0];

            state_stack.Push(current_state);
            value_stack.Push(yyval);
            location_stack.Push(yyloc);

            while (true)
            {
                int action = current_state.defaultAction;

                if (current_state.parser_table != null)
                {
                    if (next == 0)
                    {
                        // We save the last token span, so that the location span
                        // of production right hand sides that begin or end with a
                        // nullable production will be correct.
                        lastL = scanner.yylloc;
                        next  = scanner.yylex();
                    }

                    int vnext;
                    if (current_state.parser_table.TryGetValue(next, out vnext))
                    {
                        action = vnext;
                    }
                }

                if (action > 0) // shift
                {
                    Shift(action);
                }
                else if (action < 0) // reduce
                {
                    Reduce(-action);

                    if (action == -1) // accept
                    {
                        return(true);
                    }
                }
                else if (action == 0) // error
                {
                    if (!ErrorRecovery())
                    {
                        return(false);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public bool Parse()
        {
            Initialize();               // allow derived classes to instantiate rules, states and nonTerminals

            next          = 0;
            current_state = states[0];

            state_stack.Push(current_state);
            value_stack.Push(yyval);
            location_stack.Push(yyloc);

            while (true)
            {
                if (Trace)
                {
                    Console.Error.WriteLine("Entering state {0} ", current_state.num);
                }

                int action = current_state.defaultAction;

                if (current_state.parser_table != null)
                {
                    if (next == 0)
                    {
                        if (Trace)
                        {
                            Console.Error.Write("Reading a token: ");
                        }

                        next = scanner.yylex();
                    }

                    if (Trace)
                    {
                        Console.Error.WriteLine("Next token is {0}", TerminalToString(next));
                    }

                    if (current_state.parser_table.ContainsKey(next))
                    {
                        action = current_state.parser_table[next];
                    }
                }

                if (action > 0)                         // shift
                {
                    Shift(action);
                }
                else if (action < 0)                   // reduce
                {
                    Reduce(-action);

                    if (action == -1)                           // accept
                    {
                        return(true);
                    }
                }
                else if (action == 0)                   // error
                {
                    if (!ErrorRecovery())
                    {
                        return(false);
                    }
                }
            }
        }