/// <summary>
        /// 经过优化的LR(1)Edge列表。插入新元素用二分法,速度更快,但使用者不能控制元素的位置。
        /// </summary>
        /// <param name="stateCollection"></param>
        /// <param name="edges"></param>
        public LALR1EdgeCollection(LALR1StateCollection stateCollection, params LALR1Edge[] edges)
            : base(Environment.NewLine)
        {
            this.StateCollection = stateCollection;

            if (edges != null)
            {
                foreach (var item in edges)
                {
                    this.TryInsert(item);
                }
            }
        }
        /// <summary>
        /// 用LALR(1)分析法计算分析表
        /// </summary>
        /// <param name="grammar"></param>
        /// <returns></returns>
        public static void GetLALR1ParsingMap(this RegulationList grammar,
                                              out LRParsingMap map,
                                              out LALR1StateCollection stateCollection,
                                              out LALR1EdgeCollection edgeCollection, TextWriter originalOut)
        {
            // 给文法添加一个辅助的开始产生式 S' -> S $
            // 如何添加一个外来的结点类型?用Enum是无法做到的。
            var decoratedS          = new TreeNodeType("__S2", "S'", "<S'>");
            var decoratedEnd        = TreeNodeType.endOfTokenListNode;
            var decoratedRegulation = new Regulation(
                decoratedS, grammar[0].Left, decoratedEnd);
            var decoratedGrammar = new RegulationList(decoratedRegulation);

            decoratedGrammar.AddRange(grammar);
            // 初始化T为{ Closure(S' -> S $) }
            var firstItem  = new LR1Item(decoratedGrammar[0], 0, decoratedEnd);
            var firstState = new LALR1State(firstItem);
            Dictionary <TreeNodeType, bool> nullableDict;

            decoratedGrammar.GetNullableDict(out nullableDict);
            FIRSTCollection firstCollection;

            decoratedGrammar.GetFirstCollection(out firstCollection, nullableDict);
            firstState      = decoratedGrammar.Closure(firstState, nullableDict, firstCollection);
            stateCollection = new LALR1StateCollection(firstState);
            edgeCollection  = new LALR1EdgeCollection(stateCollection);
            var queue = new Queue <LALR1State>();

            queue.Enqueue(firstState);
            int lastOutputLength = 0;
            int stateListCount   = 1;
            int queueCount       = 1;

            while (queue.Count > 0)
            {
                LALR1State fromState  = queue.Dequeue(); queueCount--;
                int        itemIndex  = 0;
                int        groupCount = fromState.GroupCount;
                foreach (var group in fromState.GetGroups())
                {
                    {
                        TextWriter currentWriter = Console.Out;
                        if (Console.Out != originalOut)
                        {
                            Console.SetOut(originalOut);
                        }
                        for (int i = 0; i < lastOutputLength; i++)
                        {
                            Console.Write('\u0008');
                        }
                        string output = string.Format("Calculating LALR(1) State List: {0} <-- {1}, working on group {2}/{3} ...",
                                                      stateListCount, queueCount, 1 + itemIndex++, groupCount);
                        Console.Write(output);
                        lastOutputLength = output.Length;
                        Console.SetOut(currentWriter);
                    }
                    TreeNodeType x = group.Item1.GetNodeNext2Dot();
                    if (x == null || x == decoratedEnd)
                    {
                        continue;
                    }
                    LALR1State toState = decoratedGrammar.Goto(fromState, x, nullableDict, firstCollection);
                    if (stateCollection.TryInsert(toState))//融入组织之中吧
                    {
                        int index = stateCollection.IndexOf(toState);
                        toState = stateCollection[index];
                        queue.Enqueue(toState);
                        stateListCount++;
                        queueCount++;
                        var edge = new LALR1Edge(fromState, x, toState);
                        edgeCollection.TryInsert(edge);
                    }
                    else
                    {
                        int index = stateCollection.IndexOf(toState);
                        toState = stateCollection[index];
                        var edge = new LALR1Edge(fromState, x, toState);
                        edgeCollection.TryInsert(edge);
                    }
                }
            }
            {
                TextWriter currentWriter = Console.Out;
                if (Console.Out != originalOut)
                {
                    Console.SetOut(originalOut);
                }
                Console.WriteLine();
                Console.SetOut(currentWriter);
            }

            map = new LRParsingMap();
            foreach (var edge in edgeCollection)
            {
                if (edge.X.IsLeave)
                {
                    int stateId = edge.From.ParsingMapIndex + 1; // stateCollection.IndexOf(edge.From) + 1;
                    int gotoId  = edge.To.ParsingMapIndex + 1;   //stateCollection.IndexOf(edge.To) + 1
                    map.SetAction(stateId, edge.X, new LR1ShiftInAction(gotoId));
                }
                else
                {
                    int stateId = edge.From.ParsingMapIndex + 1; // stateCollection.IndexOf(edge.From) + 1;
                    int gotoId  = edge.To.ParsingMapIndex + 1;   //stateCollection.IndexOf(edge.To) + 1
                    map.SetAction(stateId, edge.X, new LR1GotoAction(gotoId));
                }
            }
            // TODO: not implemented
            var endItem = new LR1Item(decoratedRegulation, 1, decoratedEnd);

            foreach (var state in stateCollection)
            {
                if (state.Contains(endItem))
                {
                    int stateId = state.ParsingMapIndex + 1;// stateCollection.IndexOf(state) + 1;
                    map.SetAction(stateId, decoratedEnd, new LR1AceptAction());
                }
                foreach (var LR1Item in state)
                {
                    if (LR1Item.GetNodeNext2Dot() == null)
                    {
                        int stateId = state.ParsingMapIndex + 1;// stateCollection.IndexOf(state) + 1;
                        map.SetAction(stateId, LR1Item.LookAheadNodeType,
                                      new LR1ReducitonAction(decoratedGrammar.IndexOf(LR1Item.Regulation)));
                    }
                }
            }
        }