Example #1
0
        private static FOLLOW FindFollow(FOLLOWCollection followCollection, TreeNodeType target)
        {
            foreach (var item in followCollection)
            {
                if (item.Target == target)
                {
                    return(item);
                }
            }

            return(null);
        }
Example #2
0
        public static void GetFollowCollection(
            this RegulationList grammar,
            out FOLLOWCollection followCollection,
            Dictionary <TreeNodeType, bool> nullableDict = null,
            FIRSTCollection firstCollection = null)
        {
            if (nullableDict == null)
            {
                grammar.GetNullableDict(out nullableDict);
            }
            if (firstCollection == null)
            {
                grammar.GetFirstCollection(out firstCollection, nullableDict);
            }

            FIRSTCollection firstList4Node;

            grammar.GetFirstCollection4Node(out firstList4Node, nullableDict);
            FIRSTCollection firstList4Regulation;

            grammar.GetFirstCollection4Regulation(out firstList4Regulation, nullableDict, firstList4Node);
            grammar.DoGetFollowList(out followCollection, nullableDict, firstList4Node);
        }
Example #3
0
 public FirstListAndFollowList(FIRSTCollection firstList, FOLLOWCollection followList)
 {
     this.FirstList  = firstList;
     this.FollowList = followList;
 }
Example #4
0
        /// <summary>
        /// 计算文法的FOLLOW集
        /// </summary>
        /// <param name="grammar"></param>
        /// <param name="nullableDict"></param>
        /// <param name="firstList4Node"></param>
        /// <returns></returns>
        private static void DoGetFollowList(
            this RegulationList grammar,
            out FOLLOWCollection followCollection,
            Dictionary <TreeNodeType, bool> nullableDict, FIRSTCollection firstList4Node)
        {
            // 初始化Follow list
            followCollection = new FOLLOWCollection();
            foreach (var item in grammar.GetAllTreeNodeNonLeaveTypes())
            {
                followCollection.TryInsert(new FOLLOW(item));
            }

            // 迭代到不动点
            bool changed = false;

            do
            {
                changed = false;
                foreach (var regulation in grammar)
                {
                    int count = regulation.RightPart.Count();
                    for (int index = 0; index < count; index++)
                    {
                        // 准备为target添加follow元素
                        TreeNodeType target = regulation.RightNode(index);
                        if (target.IsLeave)
                        {
                            continue;
                        }                                                     // 叶结点没有follow
                        FOLLOW follow = FindFollow(followCollection, target); // 找到follow对象
                        for (int checkCount = 0; checkCount < count - (index + 1); checkCount++)
                        {
                            if (Nullable(regulation.RightPart, index + 1, checkCount, nullableDict))
                            {
                                // nullable之后的FIRST是target的follow的一部分
                                FIRST first = FindFirst(
                                    firstList4Node, regulation.RightNode(index + 1 + checkCount));
                                foreach (var value in first.Values)
                                {
                                    if (value != TreeNodeType.NullNode)
                                    {
                                        changed = follow.TryInsert(value) || changed;
                                    }
                                }
                            }
                        }
                        {
                            // 如果target之后的全部结点都是nullable,说明此regulation.Left的folow也是target的一部分。
                            if (Nullable(regulation.RightPart, index + 1, count - (index + 1), nullableDict))
                            {
                                // 找到此regulation.Left的folow
                                FOLLOW refFollow = FindFollow(followCollection, regulation.Left);
                                if (refFollow != follow)
                                {
                                    foreach (var item in refFollow.Values)
                                    {
                                        changed = follow.TryInsert(item) || changed;
                                    }
                                }
                            }
                        }
                    }
                }
            } while (changed);
        }