/// <summary> /// Gets the LR(0) graph /// </summary> /// <returns>The corresponding LR(0) graph</returns> private Graph GetGraphLR0() { // Create the base LR(0) graph Variable axiom = grammar.GetVariable(Grammar.GENERATED_AXIOM); ItemLR0 item = new ItemLR0(axiom.Rules[0], 0); StateKernel kernel = new StateKernel(); kernel.AddItem(item); State state0 = kernel.GetClosure(); Graph result = new Graph(state0); // Construct the graph foreach (State state in result.States) { state.BuildReductions(new StateReductionsLR0()); } return(result); }
/// <summary> /// Closes this item to a set of items /// </summary> /// <param name="closure">The list to close</param> /// <param name="map">The current helper map</param> public override void CloseTo(List <Item> closure, Dictionary <Rule, Dictionary <int, List <Item> > > map) { // the item was of the form [Var -> alpha .] (reduction) // nothing to do if (Action == LRActionCode.Reduce) { return; } // Get the next symbol in the item Symbol next = GetNextSymbol(); // Here the item is of the form [Var -> alpha . next beta] // If the next symbol is not a variable : do nothing // If the next symbol is a variable : Variable nextVar = next as Variable; if (nextVar == null) { return; } foreach (Rule rule in nextVar.Rules) { if (!map.ContainsKey(rule)) { map.Add(rule, new Dictionary <int, List <Item> >()); } Dictionary <int, List <Item> > sub = map[rule]; if (sub.ContainsKey(0)) { continue; } List <Item> list = new List <Item>(); sub.Add(0, list); ItemLR0 child = new ItemLR0(rule, 0); closure.Add(child); list.Add(child); } }