/// <summary> /// LR(1)的Closure操作。 /// 补全一个状态。 /// </summary> /// <param name="grammar"></param> /// <param name="state"></param> /// <returns></returns> static SmallerLR1State Closure(this RegulationList grammar, SmallerLR1State state, Dictionary <TreeNodeType, bool> nullableDict = null, FIRSTCollection firstCollection = null) { if (nullableDict == null) { grammar.GetNullableDict(out nullableDict); } if (firstCollection == null) { grammar.GetFirstCollection(out firstCollection, nullableDict); } Queue <LR1Item> queue = new Queue <LR1Item>(); foreach (var item in state) { queue.Enqueue(item); } while (queue.Count > 0) { LR1Item item = queue.Dequeue(); TreeNodeType node = item.GetNodeNext2Dot(); if (node == null || node.IsLeave) { continue; } List <TreeNodeType> betaZ = item.GetBetaZ(); FIRST first = grammar.GetFirst(firstCollection, nullableDict, betaZ); List <Regulation> regulations = grammar.GetRegulations(node); foreach (var regulation in regulations) { foreach (var value in first.Values) { LR1Item newItem = new LR1Item(regulation, 0, value); if (state.TryInsert(newItem)) { queue.Enqueue(newItem); } } } } return(state); }