Ejemplo n.º 1
0
Archivo: Help.cs Proyecto: kd97/Compile
 //格式化终结符与非终结符
 public static String name(WordStruct w)
 {
     if (w.getId() < 300)
     {
         return(w.getName());
     }
     else
     {
         return("<" + w.getName() + ">");
     }
 }
Ejemplo n.º 2
0
        void readNTerminal()
        {
            StreamReader r = new StreamReader(@"..\\..\\tool\\nTerminals.txt");
            String       temp;

            for (int i = 0; i < 100; i++)
            {
                temp = r.ReadLine();
                if (temp == null || temp.Equals(""))
                {
                    break;
                }
                nTerminals[temp] = new WordStruct(temp, 300 + i, temp);
            }
            r.Close();
        }
Ejemplo n.º 3
0
 void getSelect()
 {
     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();
             if (right.Count == 1 && right[0].Equals("空"))
             {
                 p.addSelect(n.getFollow());
             }
             else
             {
                 p.addSelect(getFirstXs(right));
             }
         }
     }
 }
Ejemplo n.º 4
0
        private void readIn(String phrase)
        {
            Stack <WordStruct> sw = new Stack <WordStruct>();

            MatchCollection mc = Regex.Matches(phrase, "line [0-9]+\t(.+)\t[(] (.+), [0-9]+, (.+)");

            foreach (Match m in mc)
            {
                String a = m.Value;
                a = a.Substring(5, a.Length - 5);
                String[] s = a.Split(new char[] { '\t' });
                s[2] = s[2].Substring(0, s[2].Length - 2);
                s[2] = s[2].Substring(2, s[2].Length - 2);
                String[] t = s[2].Split(new char[] { ',', ' ' });


                WordStruct w;
                try
                {
                    w = new WordStruct(t[0], int.Parse(t[2]), t[4], s[1], int.Parse(s[0]));
                }
                catch
                {
                    w = new WordStruct(",", int.Parse(t[3]), t[5], s[1], int.Parse(s[0]));
                }
                sw.Push(w);
            }

            input.Push((WordStruct)terminals["结束"]);
            while (sw.Count > 0)
            {
                input.Push(sw.Pop());
            }
            derivating.Push((WordStruct)terminals["结束"]);
            derivating.Push((WordStruct)nTerminals["开始"]);
        }
Ejemplo n.º 5
0
        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;
                                }
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        ArrayList getFirstXs(ArrayList a)
        {
            ArrayList b = new ArrayList();

            if (a.Count < 1)
            {
                return(b);
            }

            Object o = nTerminals[(String)a[0]];

            if (o != null)
            {
                WordStruct w = (WordStruct)o;
                foreach (String s in w.getFirst())
                {
                    if (!b.Contains(s))
                    {
                        b.Add(s);
                    }
                }
            }
            else
            {
                o = terminals[(String)a[0]];
                if (o != null)
                {
                    WordStruct w = (WordStruct)o;
                    foreach (String s in w.getFirst())
                    {
                        if (!b.Contains(s))
                        {
                            b.Add(s);
                        }
                    }
                }
            }
            //for
            for (int i = 0; i < a.Count - 1 && !((String)(a)[i]).Equals("空") && nTerminals[(String)(a)[i]] != null; i++)
            {
                Boolean isNull = false;
                foreach (Product ap in (ArrayList)products[(String)(a)[i]])
                {
                    if (ap.getRight().Count == 1 && ap.getRight()[0].Equals("空"))
                    {
                        isNull = true;
                        break;
                    }
                }
                if (isNull)
                {
                    Object oo = nTerminals[(String)(a)[i + 1]];
                    if (oo != null)
                    {
                        WordStruct w = (WordStruct)oo;
                        foreach (String s in w.getFirst())
                        {
                            if (!b.Contains(s))
                            {
                                b.Add(s);
                            }
                        }
                    }
                    else
                    {
                        oo = terminals[(String)(a)[i + 1]];
                        if (oo != null)
                        {
                            WordStruct w = (WordStruct)oo;
                            foreach (String s in w.getFirst())
                            {
                                if (!b.Contains(s))
                                {
                                    b.Add(s);
                                }
                            }
                        }
                    }
                }
                else
                {
                    break;
                }
            }
            return(b);
        }
Ejemplo n.º 7
0
        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;
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
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);
        }