コード例 #1
0
ファイル: Help.cs プロジェクト: kd97/Compile
 //格式化终结符与非终结符
 public static String name(WordStruct w)
 {
     if (w.getId() < 300)
     {
         return(w.getName());
     }
     else
     {
         return("<" + w.getName() + ">");
     }
 }
コード例 #2
0
ファイル: Parse.cs プロジェクト: kd97/Compile
        void getFollowX()
        {
            WordStruct w = (WordStruct)nTerminals["开始"];

            w.addFollow("结束");

            foreach (DictionaryEntry d in products)
            {
                ArrayList  a = (ArrayList)d.Value;
                WordStruct n = (WordStruct)nTerminals[(String)d.Key];
                foreach (Product p in a)//遍历产生式
                {
                    ArrayList right = p.getRight();
                    foreach (String b in right)
                    {
                        if (right.IndexOf(b) < right.Count - 1)
                        {
                            ArrayList aa = new ArrayList();
                            for (int i = right.IndexOf(b) + 1; i < right.Count; i++)
                            {
                                aa.Add(right[i]);
                            }
                            ArrayList fs = getFirstXs(aa);
                            Object    oo = nTerminals[b];
                            if (oo != null)
                            {
                                WordStruct ww = (WordStruct)nTerminals[b];
                                foreach (String c in fs)
                                {
                                    if (!ww.getFollow().Contains(c))
                                    {
                                        ww.addFollow(c);
                                        //tmp += c+" ";
                                        h.Add(ww.getName() + "\t" + c + "\t");
                                    }
                                }
                            }
                        }
                    }
                }
            }

            Boolean end = false;

            while (!end)
            {
                end = true;
                foreach (DictionaryEntry d in products)
                {
                    ArrayList  a = (ArrayList)d.Value;
                    WordStruct n = (WordStruct)nTerminals[(String)d.Key];
                    foreach (Product p in a)
                    {
                        ArrayList right = p.getRight();
                        String    b     = (String)right[right.Count - 1];
                        Object    oo    = nTerminals[b];
                        if (oo != null)
                        {
                            WordStruct ww = (WordStruct)nTerminals[b];
                            foreach (String c in n.getFollow())
                            {
                                if (!ww.getFollow().Contains(c))
                                {
                                    end = false;
                                    ww.addFollow(c);
                                    h.Add(ww.getName() + "\t" + c + "\t");
                                }
                            }

                            for (int i = right.Count - 1; i > 0 && !((String)(right)[i]).Equals("空") && nTerminals[(String)(right)[i]] != null; i--)
                            {
                                Boolean isNull = false;
                                foreach (Product ap in (ArrayList)products[(String)(right)[i]])
                                {
                                    if (ap.getRight().Count == 1 && ap.getRight()[0].Equals("空"))
                                    {
                                        isNull = true;
                                        break;
                                    }
                                }
                                if (isNull)
                                {
                                    String bb  = (String)right[i - 1];
                                    Object ooo = nTerminals[bb];
                                    if (ooo != null)
                                    {
                                        WordStruct www = (WordStruct)ooo;
                                        foreach (String c in n.getFollow())
                                        {
                                            if (!www.getFollow().Contains(c))
                                            {
                                                end = false;
                                                www.addFollow(c);
                                                //tmp += c + " ";
                                                h.Add(www.getName() + "\t" + c + "\t");
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
ファイル: Parse.cs プロジェクト: kd97/Compile
        void getFirstX()
        {
            foreach (DictionaryEntry d in terminals)
            {
                WordStruct w = (WordStruct)d.Value;
                w.addFirst(w.getName());
                m.Add(w.getName() + "\t" + w.getName() + "\t");
            }

            foreach (DictionaryEntry d in products)
            {
                ArrayList  a = (ArrayList)d.Value;
                WordStruct n = (WordStruct)nTerminals[(String)d.Key];
                foreach (Product p in a)
                {
                    String f = (String)((ArrayList)p.getRight())[0];
                    if (terminals[f] != null)
                    {
                        n.addFirst(f);
                        m.Add(n.getName() + "\t" + f + "\t");
                    }
                }
            }
            Boolean end = false;

            while (!end)
            {
                end = true;
                foreach (DictionaryEntry d in products)
                {
                    ArrayList  a = (ArrayList)d.Value;
                    WordStruct n = (WordStruct)nTerminals[(String)d.Key];
                    foreach (Product p in a)
                    {
                        ArrayList right = (ArrayList)p.getRight();
                        String    f     = (String)(right)[0];
                        //X->Y..
                        if (nTerminals[f] != null)
                        {
                            WordStruct w = (WordStruct)nTerminals[f];
                            foreach (String s in w.getFirst())
                            {
                                if (!n.getFirst().Contains(s))
                                {
                                    end = false;
                                    n.addFirst(s);
                                    m.Add(n.getName() + "\t" + s + "\t");
                                }
                            }
                        }

                        for (int i = 0; i < right.Count && !((String)(right)[i]).Equals("空") && nTerminals[(String)(right)[i]] != null; i++)
                        {
                            //判断是否能否退出空
                            Boolean isNull = false;
                            foreach (Product ap in (ArrayList)products[(String)(right)[i]])
                            {
                                if (ap.getRight().Count == 1 && ap.getRight()[0].Equals("空"))
                                {
                                    isNull = true;
                                    break;
                                }
                            }
                            if (isNull)
                            {
                                WordStruct w = (WordStruct)nTerminals[(String)(right)[i]];
                                foreach (String s in w.getFirst())
                                {
                                    if (!n.getFirst().Contains(s))
                                    {
                                        end = false;
                                        n.addFirst(s);
                                        m.Add(n.getName() + "\t" + s + "\t");
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
コード例 #4
0
ファイル: Parse.cs プロジェクト: kd97/Compile
        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);
        }