Пример #1
0
 //格式化终结符与非终结符
 public static String name(WordStruct w)
 {
     if (w.getId() < 300)
     {
         return(w.getName());
     }
     else
     {
         return("<" + w.getName() + ">");
     }
 }
Пример #2
0
        public List <Information> parsexe(String phrase, out String sen, out String errIn)
        {
            hopelast.father.Add("程序");
            ArrayList pro = new ArrayList();

            pro.Add("开始");
            hopelast.children.Add(pro);
            sen   = "";
            errIn = "";

            readIn(phrase);
            Meaning.setInit();
            int k = 0;

            //int line = 1;
            while (input.Count > 0)
            {
                if (derivating.Count < 1)
                {
                    errIn += "错误" + "\r\n";
                    break;
                }
                WordStruct x = derivating.Pop();
                derivating.Push(x);
                WordStruct a = input.Pop();
                input.Push(a);


                if (x.getId() > 490)
                {
                    WordStruct y = derivating.Pop();
                    Meaning.run(int.Parse(x.getName()), a.getValue(), "" + a.line);
                }
                else if (x.getId() < 290)       //终结符
                {
                    if (x.getName().Equals(a.getName()))
                    {
                        WordStruct y = derivating.Pop();
                        treelist.Add(y.getName());
                        input.Pop();
                    }
                    else
                    {
                        errIn += ("Error at line " + a.line + "\t输入 " + WForm.name(a) + "输入与推导符号串栈中的不匹配" + "\r\n");

                        WordStruct y = derivating.Pop();
                        treelist.Add(y.getName());
                    }
                }
                else
                {
                    Product p = (Product)(((Hashtable)deriTable[x.getName()])[a.getName()]);
                    if (p == null)
                    {
                        errIn += "系统错误" + "\r\n";
                        input.Pop();
                    }
                    else if (p.getLeft().Equals("synch"))
                    {
                        errIn += "错误:分析栈顶为 <" + x.getName() + "> 进入错误处理 " + a.getName() + "\r\n";
                        WordStruct y = derivating.Pop();
                        treelist.Add(y.getName());
                    }
                    else if (p.getLeft().Equals("error"))
                    {
                        errIn += "错误:输入栈顶为 " + a.getName() + " 不可用分析栈顶文法错误 <" + x.getName() + "> 规约\r\n";
                        input.Pop();
                    }
                    else//输出语法分析结果
                    {
                        sen += "line " + a.line + "\t<" + x.getName() + "> -> ";
                        hopelast.father.Add(x.getName());
                        WordStruct y = derivating.Pop();           //将该符号从推导符号串栈中弹出
                        treelist.Add(y.getName());
                        ArrayList right = p.getRight();
                        //把产生式右部压入栈
                        for (int i = 0; i < right.Count; i++)
                        {
                            Object o = nTerminals[right[right.Count - i - 1]];
                            if (o != null)
                            {
                                WordStruct w = (WordStruct)o;
                                if (!w.getName().Equals("空"))
                                {
                                    derivating.Push(w);
                                }
                            }
                            else
                            {
                                o = terminals[right[right.Count - i - 1]];
                                if (o != null)
                                {
                                    derivating.Push((WordStruct)o);
                                }
                                else
                                {
                                    derivating.Push(new WordStruct((String)(right[right.Count - i - 1]), 500, "代码"));
                                }
                            }
                        }
                        //输出产生式

                        ArrayList tmp = new ArrayList();
                        for (int i = 0; i < right.Count; i++)
                        {
                            Object o = nTerminals[right[i]];
                            if (o != null)
                            {
                                sen += "<" + right[i] + ">";

                                k++;
                                Information one;
                                one = new Information(k, right[i] + "", x.getName());
                                informationlist.Add(one);
                                tmp.Add(right[i] + "");
                            }
                            else
                            {
                                o = terminals[right[i]];

                                if (o != null)
                                {
                                    sen += "" + right[i] + "";
                                    k++;
                                    Information one = new Information(k, right[i] + "", x.getName());


                                    informationlist.Add(one);
                                    tmp.Add(right[i] + "");
                                }
                            }
                        }
                        hopelast.children.Add(tmp);

                        sen += "\r\n";
                        //treeMore.Add(oneTree);
                    }
                }
            }
            errIn += "\r\n语义分析错误:\r\n";
            errIn += Meaning.errIn;
            return(informationlist);
        }