Exemple #1
0
        public Ast Parse(Tokenizer tokenizer, ErrorRecord errorRecord)
        {
            symbolStack.Push(parsingTable.LastSymbol);
            symbolStack.Push(parsingTable.FirstSymbol);

            Token lastToken = null;

            while (symbolStack.Count != 0)
            {
                if (EnableStackTrace)
                {
                    TraceStack(tokenizer.Peek(), symbolStack, semanticStack);
                }

                Symbol symbol = symbolStack.Pop();
                Token  token  = tokenizer.Peek();

                if (token is ErrorToken)
                {
                    errorRecord.AddLexicalError(token as ErrorToken, StackTrace);
                    return(null);
                }

                if (symbol.ToSymbolType() == SymbolType.Token)
                {
                    if (symbol == token.Symbol)
                    {
                        lastToken = tokenizer.Pop();
                    }
                    else
                    {
                        errorRecord.AddSyntaxError(symbol, token, StackTrace);
                        return(null);
                    }
                }
                else if (symbol.ToSymbolType() == SymbolType.NonTerminal)
                {
                    var rule = parsingTable[symbol, token.Symbol];
                    if (rule == null)
                    {
                        errorRecord.AddSyntaxError(symbol, token, StackTrace);
                        return(null);
                    }
                    else
                    {
                        symbolStack.Push(rule.Reverse);
                    }
                }
                else if (symbol.ToSymbolType() == SymbolType.Semantic)
                {
                    astFactory.ProcessAction(semanticStack, symbol, lastToken);
                }
            }
            return(semanticStack.Peek());
        }