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());
            }
        }
Beispiel #2
0
        public override int Execute(ParsingContext context)
        {
            Regulation regulation = context.Grammar[RegulationId - 1];
            var        newNode    = new SyntaxTree();
            int        tokenCount = 0;
            int        count      = regulation.RightPart.Count();

            for (int i = 0; i < count; i++)
            {
                var item = context.TreeStack.Pop();
                context.StateIdStack.Pop();//只弹出,不再使用。
                newNode.Children.Insert(0, item);
                item.Parent = newNode;
                tokenCount += item.MappedTokenLength;
                if (i == 0)
                {
                    newNode.MappedTokenStartIndex = item.MappedTokenStartIndex;
                }
            }
            newNode.MappedTokenLength    = tokenCount;
            newNode.MappedTotalTokenList = context.TokenList;
            newNode.NodeType             = regulation.Left;
            context.TreeStack.Push(newNode);
            int             stateId    = context.StateIdStack.Peek();
            LRParsingAction gotoAction = context.ParsingMap.GetAction(stateId, regulation.Left);

            gotoAction.Execute(context);

            return(context.CurrentTokenIndex);
        }
        private void PrintParsingProgress(ParsingContext context)
        {
#if DEBUG
            if (!print)
            {
                return;
            }                       // 调试时快速取消print

            Debug.WriteLine("=======================");
            {
                Debug.WriteLine("Current Stack:");
                Debug.Write("    ");
                var stateIdArray = context.StateIdStack.ToArray();
                var treeArray    = context.TreeStack.ToArray();
                for (int i = stateIdArray.Length - 1; i > 0; i--)
                {
                    Debug.Write(stateIdArray[i]);
                    Debug.Write('['); Debug.Write(treeArray[i - 1].NodeType.Nickname); Debug.Write(']');
                }
                Debug.Write(stateIdArray[0]); Debug.Write(' ');
                Debug.WriteLine("");
            }
            {
                Debug.WriteLine("Current token list:");
                Debug.Write("    ");
                for (int i = context.CurrentTokenIndex; i < context.TokenList.Count; i++)
                {
                    Debug.Write(context.TokenList[i].TokenType.Content); Debug.Write(' ');
                }
                Debug.WriteLine("");
            }
            {
                Debug.WriteLine("Next action:");
                TreeNodeType    nodeType = context.CurrentNodeType();
                int             stateId  = context.StateIdStack.Peek();
                LRParsingAction action   = context.ParsingMap.GetAction(stateId, nodeType);
                Debug.Write("    "); Debug.WriteLine(action);
            }
            {
                Debug.WriteLine("Current syntax tree:");
                SyntaxTree virtualParent = new SyntaxTree();
                virtualParent.NodeType = new TreeNodeType("VirtualParent", "", "Virtual Parent");
                var treeArray = context.TreeStack.ToArray();
                for (int i = treeArray.Length - 1; i >= 0; i--)
                {
                    virtualParent.Children.Add(treeArray[i]);
                    treeArray[i].Parent = virtualParent;
                }
                Debug.WriteLine(virtualParent);
                foreach (var item in context.TreeStack)
                {
                    item.Parent = null;
                }
            }
            Debug.WriteLine("=======================");
#endif
        }
        private void PrintLastState(ParsingContext context)
        {
#if DEBUG
            if (!print)
            {
                return;
            }                       // 调试时快速取消print

            Debug.WriteLine("=======================");
            Debug.WriteLine("***********************");
            {
                Debug.WriteLine("Last Stack:");
                Debug.Write("    ");
                var stateIdArray = context.StateIdStack.ToArray();
                var treeArray    = context.TreeStack.ToArray();
                for (int i = stateIdArray.Length - 1; i > 0; i--)
                {
                    Debug.Write(stateIdArray[i]);
                    Debug.Write('['); Debug.Write(treeArray[i - 1].NodeType.Nickname); Debug.Write(']');
                }
                Debug.Write(stateIdArray[0]); Debug.Write(' ');
                Debug.WriteLine("");
            }
            {
                Debug.WriteLine("Last token list:");
                Debug.Write("    ");
                for (int i = context.CurrentTokenIndex; i < context.TokenList.Count; i++)
                {
                    Debug.Write(context.TokenList[i].TokenType.Content); Debug.Write(' ');
                }
                Debug.WriteLine("");
            }
            {
                Debug.WriteLine("Last action:");
                TreeNodeType    nodeType = context.CurrentNodeType();
                int             stateId  = context.StateIdStack.Peek();
                LRParsingAction action   = context.ParsingMap.GetAction(stateId, nodeType);
                Debug.Write("    "); Debug.WriteLine(action);
            }
            {
                Debug.WriteLine("Last syntax tree:");
                Debug.WriteLine(context.TreeStack.Peek());
                foreach (var item in context.TreeStack)
                {
                    item.Parent = null;
                }
            }
            Debug.WriteLine("***********************");
            Debug.WriteLine("=======================");
#endif
        }