Example #1
0
        public GrammarList GetAllTuidaoShi(string str)    //返回文法中所有以str为推导式左部的推导式
        {
            GrammarList NeedList = new GrammarList();

            foreach (var i in this)
            {
                if (i.Keys.Contains <string>(str))//&& NeedList.Contains(i) == false)
                {
                    if (NeedList.Count == 0)
                    {
                        NeedList.Add(i);
                    }
                    else
                    {
                        for (int k = 0; k < NeedList.Count; k++)
                        {
                            TuidaoShi t = NeedList[k];
                            if (t.Equals(i) == false)
                            {
                                NeedList.Add(i);
                            }
                        }
                    }
                    // NeedList.Add(i);
                }
            }
            return(NeedList);
        }
Example #2
0
File: SLR.cs Project: gu0o00/gSQL
        public List <string> GetFirst(string x)
        {
            List <string> first = new List <string>();

            if (zhongjiefu.Contains(x) == true && first.Contains(x) == false)                //如果x是终结符,直接将x放入first
            {
                first.Add(x);
                return(first);
            }
            GrammarList allGarmmar = garmmarList.GetAllTuidaoShi(x);

            foreach (Dictionary <string, string[]> i in allGarmmar)
            {
                if (i.ElementAt(0).Value[0].Equals(x))
                {
                    continue;
                }
                List <string> res = GetFirst(i.ElementAt(0).Value[0]);
                foreach (string t in res)
                {
                    if (first.Contains(t) == false)
                    {
                        first.Add(t);
                    }
                }
            }
            return(first);
        }
Example #3
0
File: SLR.cs Project: gu0o00/gSQL
        public void Closure(Item start = null)
        {
            Item aItem;

            if (start == null)
            {
                aItem = new Item(garmmarList[0].ElementAt(0).Key, garmmarList[0].ElementAt(0).Value);
            }
            else
            {
                aItem = start;
            }

            ClosureList.Add(aItem);
            for (int i = 0; i < ClosureList.Count; i++)
            {
                Item        a            = ClosureList[i];
                int         len          = a.rStr.Length;
                String      aFuhao       = a.rStr[0];
                GrammarList allTuidaoShi = garmmarList.GetAllTuidaoShi(aFuhao);
                if (allTuidaoShi.Count == 0)
                {
                    continue;
                }
                foreach (TuidaoShi aTuidaoShi in allTuidaoShi)
                {
                    Item tmpItem = new Item(aTuidaoShi.ElementAt(0).Key, aTuidaoShi.ElementAt(0).Value);
                    if (ClosureList.Contains(tmpItem) == false)
                    {
                        ClosureList.Add(tmpItem);
                    }
                }
            }
        }
Example #4
0
File: SLR.cs Project: gu0o00/gSQL
        public Closure Goto(Closure I, String X)        //闭包就是项目的表
        {
            Closure list = new Closure();

            foreach (Item a in I)
            {
                if (a.point >= a.rStr.Length)                   //如果点已经移到最后,则处理闭包中的下一个项目
                {
                    continue;
                }
                if (a.rStr[a.point] == X)                       //如果X刚好在点后,则开始求闭包
                {
                    list.Add(new Item(a.lStr, a.rStr, a.point + 1));

                    if ((a.point + 1) >= a.rStr.Length)
                    {
                        continue;
                    }
                    GrammarList allTuidaoShi = garmmarList.GetAllTuidaoShi(a.rStr[a.point + 1]);
                    if (allTuidaoShi.Count == 0)
                    {
                        continue;
                    }
                    foreach (TuidaoShi aTuidaoShi in allTuidaoShi)                          //把符合的推导式变成项目放入list
                    {
                        Item aItem = new Item(aTuidaoShi.ElementAt(0).Key, aTuidaoShi.ElementAt(0).Value, 0);
                        if (list.Contains(aItem) == false)
                        {
                            list.Add(aItem);
                        }
                    }

                    for (int i = 1; i < list.Count; i++)
                    {
                        Item        aItem         = list[i];
                        int         len           = aItem.rStr.Length;
                        String      aFuhao        = aItem.rStr[0];
                        GrammarList all_TuidaoShi = garmmarList.GetAllTuidaoShi(aFuhao);
                        if (all_TuidaoShi.Count == 0)
                        {
                            continue;
                        }
                        foreach (TuidaoShi aTuidaoShi in all_TuidaoShi)
                        {
                            //TuidaoShi to Item
                            Item tmpItem = new Item(aTuidaoShi.ElementAt(0).Key, aTuidaoShi.ElementAt(0).Value);
                            if (list.Contains(tmpItem) == false)
                            {
                                list.Add(tmpItem);
                            }
                        }
                    }
                }
            }
            return(list);
        }
Example #5
0
File: SLR.cs Project: gu0o00/gSQL
        public List <string> GetFollow(string x)
        {
            if (zhongjiefu.Contains(x) == true)                 //如果x是终结符
            {
                return(follow);
            }
            GrammarList allGarmmar = garmmarList.GetYoubuHad(x);

            foreach (TuidaoShi i in allGarmmar)
            {
                int index = GetIndexFromArry(i.ElementAt(0).Value, x);
                if (index >= i.ElementAt(0).Value.Length - 1 &&
                    i.ElementAt(0).Key.Equals(i.ElementAt(0).Value[i.ElementAt(0).Value.Length - 1]) == false &&
                    jilu.Contains(i.ElementAt(0).Key) == false)                          //x的后面没有东西了
                {
                    if (jilu.Contains(i.ElementAt(0).Key))
                    {
                        continue;
                    }
                    jilu.Add(i.ElementAt(0).Key);
                    List <string> res = GetFollow(i.ElementAt(0).Key);
                    foreach (string t in res)
                    {
                        if (follow.Contains(t) == false)
                        {
                            follow.Add(t);
                        }
                    }
                }
                else if (index < i.ElementAt(0).Value.Length - 1)                   //x的后面还有符号
                {
                    string        nextFuhao = i.ElementAt(0).Value[index + 1];
                    List <string> res       = new List <string>();
                    if (Nzhongjiefu.Contains(nextFuhao) == true)
                    {
                        firstList.TryGetValue(nextFuhao, out res);
                    }
                    else
                    {
                        res.Add(nextFuhao);
                    }
                    if (res != null)
                    {
                        foreach (string t in res)
                        {
                            if (follow.Contains(t) == false)
                            {
                                follow.Add(t);
                            }
                        }
                    }
                }
            }
            return(follow);
        }
Example #6
0
        public GrammarList GetYoubuHad(string str)
        {
            GrammarList NeedList = new GrammarList();

            foreach (TuidaoShi i in this)
            {
                if (i.ElementAt(0).Value.Contains(str) == true && NeedList.Contains(i) == false)
                {
                    NeedList.Add(i);
                }
            }
            return(NeedList);
        }