示例#1
0
        private TreeNode helpTree(Tiny.TINYNode root)
        {
            TreeNode node = new TreeNode(root.NTData);

            foreach (var child in root.Children)
            {
                node.Nodes.Add(helpTree(child));
            }
            return(node);
        }
示例#2
0
 private void ViewGrammarTree(Tiny.TINYNode root)
 {
     GrammarResultTreeView.Nodes.Clear();
     GrammarResultTreeView.Nodes.Add(helpTree(root));
     GrammarResultTreeView.ExpandAll();
 }
示例#3
0
        /// <summary>
        /// 分析输入串
        /// 若输入串能构成正确语法树,返回True,否则返回False
        /// </summary>
        /// <param name="TokenList">输入串</param>
        /// <param name="Root">语法树根节点</param>
        /// <returns>若输入串能构成正确语法树,返回True,否则返回False</returns>
        public bool Analyse(List <Token> TokenList, out TINYNode Root, out Token ErrorToken)
        {
            if (TokenList.Count == 0)
            {
                throw new Exception("输入穿中不不包含任何token");
            }
            if (TokenList.Any(token => token.Type == TokenType.Error))
            {
                throw new Exception("请先处理词法分析中发现的错误");
            }

            Stack <NTVTType> NTVTStack = new Stack <NTVTType>();
            Stack <TINYNode> NodeStack = new Stack <TINYNode>();

            NTVTStack.Push(toNTVT(Start));
            Root       = new TINYNode(Start.ToString());
            ErrorToken = null;
            NodeStack.Push(Root);

            int      index = 0;
            NTVTType X;          //栈顶元素
            TINYNode curNode;    //栈顶节点
            VTType   a;          //当前输入

            while (NTVTStack.Count != 0)
            {
                X       = NTVTStack.Peek();          //栈顶元素
                curNode = NodeStack.Peek();          //栈顶节点

                if (index < TokenList.Count)
                {
                    a = toVT(TokenList[index].Type.ToString());
                }
                else
                {
                    a = VTType.θ;
                }

                if (X.ToString() == a.ToString())                //当前元素等于栈顶元素
                {
                    NTVTStack.Pop();
                    NodeStack.Pop();
                    index++;
                }
                else if (isNT(X))                //当前元素不等于栈顶元素X,但是栈顶元素X是非终结符
                {
                    LL1Item L = find(toNT(X), a);
                    if (L == null)                    //没有找到[X,a]
                    {
                        if (existX(toNT(X)))          //X可以归结为ε
                        {
                            curNode.Children.Add(new TINYNode("ε"));
                            NTVTStack.Pop();
                            NodeStack.Pop();
                        }
                        else
                        {
                            curNode.Children.Add(new TINYNode(string.Format("Error")));
                            ErrorToken = TokenList[index];
                            return(false);
                        }
                    }
                    else                     //L!=null
                    {
                        NTVTStack.Pop();
                        NodeStack.Pop();
                        if (L.Rule.Contains(NTVTType.ε))                        // A -> ε
                        {
                            curNode.Children.Add(new TINYNode("ε"));
                            continue;
                        }
                        for (int i = L.Rule.Count - 1; i >= 0; i--)
                        {
                            NTVTStack.Push(L.Rule[i]);

                            TINYNode newNode = new TINYNode(L.Rule[i].ToString());
                            NodeStack.Push(newNode);
                            curNode.Children.Insert(0, newNode);
                        }
                    }
                }
                else
                {
                    ErrorToken = TokenList[index];
                    return(false);
                }
            }
//#if DEBUG
//			Root.Show(1);
//#endif
            return(true);
        }