Exemplo n.º 1
0
        public SyntaxTree Parse(TokenList tokenList)
        {
            LRParsingMap   parsingMap = GetParsingMap();
            RegulationList grammar    = GetGrammar();

            //TODO:这个convertor以后是可以配置的。
            var tokenTypeConvertor = new TokenType2TreeNodeType();
            var context            = new ParsingContext(tokenList, grammar, parsingMap, tokenTypeConvertor);

            while (context.CurrentTokenIndex < context.TokenList.Count + 1)
            {
                PrintParsingProgress(context);
                TreeNodeType    nodeType          = context.CurrentNodeType();
                int             stateId           = context.StateIdStack.Peek();
                LRParsingAction action            = parsingMap.GetAction(stateId, nodeType);
                int             currentTokenIndex = action.Execute(context);
                context.CurrentTokenIndex = currentTokenIndex;
            }

            PrintLastState(context);

            if (context.TreeStack.Count > 0)
            {
                return(context.TreeStack.Peek());
            }
            else
            {
                return(new SyntaxTree());
            }
        }
Exemplo n.º 2
0
        public ParsingContext(TokenList tokenList,
                              RegulationList grammar,
                              LRParsingMap parsingMap,
                              TokenType2TreeNodeType tokenTypeConvertor)
        {
            this.StateIdStack = new Stack <int>();
            this.StateIdStack.Push(1);
            this.TreeStack = new Stack <SyntaxTree>();

            this.TokenList          = tokenList;
            this.Grammar            = grammar;
            this.ParsingMap         = parsingMap;
            this.TokenTypeConvertor = tokenTypeConvertor;
        }