コード例 #1
0
        /// <summary>
        /// Main entry point of the Shift-Reduce Parser.
        /// </summary>
        /// <returns>True if parse succeeds, else false for
        /// unrecoverable errors</returns>
        public bool Parse()
        {
            Initialize();       // allow derived classes to instantiate rules, states and nonTerminals

            NextToken = 0;
            FsaState  = states[0];

            StateStack.Push(FsaState);
            valueStack.Push(CurrentSemanticValue);
            LocationStack.Push(CurrentLocationSpan);

            while (true)
            {
#if TRACE_ACTIONS
                Console.Error.WriteLine("Entering state {0} ", FsaState.number);
                DisplayStack();
#endif
                int action = FsaState.defaultAction;

                if (FsaState.ParserTable != null)
                {
                    if (NextToken == 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.
                        LastSpan  = scanner.yylloc;
                        NextToken = scanner.yylex();
#if TRACE_ACTIONS
                        Console.Error.WriteLine("Reading: Next token is {0}", TerminalToString(NextToken));
#endif
                    }
#if TRACE_ACTIONS
                    else
                    {
                        Console.Error.WriteLine("Next token is still {0}", TerminalToString(NextToken));
                    }
#endif
                    if (FsaState.ParserTable.ContainsKey(NextToken))
                    {
                        action = FsaState.ParserTable[NextToken];
                    }
                }

                if (action > 0)         // shift
                {
                    Shift(action);
                }
                else if (action < 0)   // reduce
                {
                    try {
                        Reduce(-action);
                        if (action == -1)       // accept
                        {
                            return(true);
                        }
                    }
                    catch (AbortException) {
                        return(false);
                    }
                    catch (AcceptException) {
                        return(true);
                    }
                    catch (ErrorException) {
                        if (!ErrorRecovery())
                        {
                            return(false);
                        }
                        else
                        {
                            throw;  // Rethrow x, preserving information.
                        }
                    }
                }
                else if (action == 0 && !ErrorRecovery()) // error
                {
                    return(false);
                }
            }
        }
        public bool Parse()
        {
            Initialize();

            NextToken = 0;
            FsaState  = states[0];

            StateStack.Push(FsaState);
            valueStack.Push(CurrentSemanticValue);
            LocationStack.Push(CurrentLocationSpan);

            while (true)
            {
#if TRACE_ACTIONS
                Console.Error.WriteLine("Entering state {0} ", FsaState.number);
#endif
                int action = FsaState.defaultAction;

                if (FsaState.ParserTable != null)
                {
                    if (NextToken == 0)
                    {
#if TRACE_ACTIONS
                        Console.Error.Write("Reading a token: ");
#endif
                        LastSpan  = scanner.yylloc;
                        NextToken = scanner.yylex();
                    }

#if TRACE_ACTIONS
                    Console.Error.WriteLine("Next token is {0}", TerminalToString(NextToken));
#endif
                    if (FsaState.ParserTable.ContainsKey(NextToken))
                    {
                        action = FsaState.ParserTable[NextToken];
                    }
                }

                if (action > 0)
                {
                    Shift(action);
                }
                else if (action < 0)
                {
                    try
                    {
                        Reduce(-action);
                        if (action == -1)
                        {
                            return(true);
                        }
                    }
                    catch (Exception x)
                    {
                        if (x is AbortException)
                        {
                            return(false);
                        }
                        else if (x is AcceptException)
                        {
                            return(true);
                        }
                        else if (x is ErrorException && !ErrorRecovery())
                        {
                            return(false);
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
                else if (action == 0)
                {
                    if (!ErrorRecovery())
                    {
                        return(false);
                    }
                }
            }
        }