コード例 #1
0
        /// <summary>
        /// 计算所有可能推导出null的结点。
        /// </summary>
        /// <param name="grammar"></param>
        /// <returns></returns>
        public static void GetNullableDict(
            this RegulationList grammar, out Dictionary <TreeNodeType, bool> nullableDict)
        {
            nullableDict = new Dictionary <TreeNodeType, bool>();

            // 初始化nullable
            List <TreeNodeType> allNodeTypes = grammar.GetAllTreeNodeTypes();

            foreach (var item in allNodeTypes)
            {
                nullableDict.Add(item, false);
            }

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

            do
            {
                changed = false;
                foreach (var regulation in grammar)
                {
                    // 如果RightPart的结点全部可为null,就说明此regulation.Left是可推导出"null"的。
                    if (Nullable(regulation.RightPart, 0, regulation.RightPart.Count(), nullableDict))
                    {
                        if (!nullableDict[regulation.Left])
                        {
                            nullableDict[regulation.Left] = true;
                            changed = true;
                        }
                    }
                }
            } while (changed);
        }