コード例 #1
0
ファイル: Parse.cs プロジェクト: kd97/Compile
        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);
        }
コード例 #2
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;
                            }
                        }
                    }
                }
            }
        }