Beispiel #1
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);
        }
Beispiel #2
0
        private void makeLL1List()
        {
            LL1ItemList = new List <LL1Item>();
            foreach (var rule in TINYRules)
            {
                if (rule.Words.Contains(NTVTType.ε))                //规则包含ε,即 A -> ε
                {
                    foreach (var word in followSet[rule.Start])
                    {
                        LL1Item cell = new LL1Item();
                        cell.NT   = rule.Start;
                        cell.meet = word;
                        cell.Rule.AddRange(rule.Words);
                        LL1ItemList.Add(cell);
                    }
                }
                else if (isNT(rule.Words[0]))                // A -> BC
                {
                    foreach (var word in firstSet[toNT(rule.Words[0])].Where(v => v != VTType.ε))
                    {
                        LL1Item cell = new LL1Item();
                        cell.NT   = rule.Start;
                        cell.meet = word;
                        cell.Rule.AddRange(rule.Words);
                        LL1ItemList.Add(cell);
                    }
                    if (firstSet[toNT(rule.Words[0])].Contains(VTType.ε))
                    {
                        foreach (var word in followSet[rule.Start])
                        {
                            LL1Item cell = new LL1Item();
                            cell.NT   = rule.Start;
                            cell.meet = word;
                            cell.Rule.AddRange(rule.Words);
                            LL1ItemList.Add(cell);
                        }
                    }
                }
                else                // A -> bC
                {
                    LL1Item cell = new LL1Item();
                    cell.NT   = rule.Start;
                    cell.meet = toVT(rule.Words[0]);
                    cell.Rule.AddRange(rule.Words);
                    LL1ItemList.Add(cell);
                }
            }
//#if DEBUG
//			Debug.WriteLine("-----------------以下展示LL1List------------------------");
//			Debug.WriteLine("LL1List共有{0}条记录", LL1ItemList.Count);
//			foreach (var item in LL1ItemList)
//			{
//				Debug.Write(string.Format("[{0},{1}] -> ", item.NT.ToString(), item.meet.ToString()));
//				foreach (var subitem in item.Rule)
//				{
//					Debug.Write(subitem);
//					Debug.Write(" ");
//				}
//				Debug.Write(Environment.NewLine);
//			}
//			Debug.WriteLine("-----------------结束展示LL1List------------------------");
//#endif
        }