/// <summary> /// Builds the propagation table /// </summary> /// <remarks> /// The propagation table is a couple of list where /// items in the first list propagate to items in the second list at the same index /// </remarks> private void BuildPropagationTable() { for (int i = 0; i != kernels.Count; i++) { StateKernel kernelLALR1 = kernels[i]; State stateLR0 = graphLR0.States[i]; // For each LALR(1) item in the kernel // Only the kernel needs to be examined as the other items will be discovered and treated // with the dummy closures foreach (ItemLALR1 itemLALR1 in kernelLALR1.Items) { // If ItemLALR1 is of the form [A -> alpha .] // => The closure will only contain the item itself // => Cannot be used to generate or propagate lookaheads if (itemLALR1.Action == LRActionCode.Reduce) { continue; } // Item here is of the form [A -> alpha . beta] // Create the corresponding dummy item : [A -> alpha . beta, dummy] // This item is used to detect lookahead propagation ItemLR1 dummyItem = new ItemLR1(itemLALR1.BaseRule, itemLALR1.DotPosition, Dummy.Instance); StateKernel dummyKernel = new StateKernel(); dummyKernel.AddItem(dummyItem); State dummySet = dummyKernel.GetClosure(); // For each item in the closure of the dummy item foreach (ItemLR1 item in dummySet.Items) { // If the item action is a reduction // => OnSymbol for this item will be created by the LALR(1) closure // => Do nothing if (item.Action == LRActionCode.Reduce) { continue; } // Get the child item in the child LALR(1) kernel State childLR0 = stateLR0.GetChildBy(item.GetNextSymbol()); StateKernel childKernel = kernels[childLR0.ID]; ItemLALR1 childLALR1 = (ItemLALR1)GetEquivalentInSet(childKernel, item.GetChild()); // If the lookaheads of the item in the dummy set contains the dummy terminal if (item.Lookahead == Dummy.Instance) { // => Propagation from the parent item to the child propagOrigins.Add(itemLALR1); propagTargets.Add(childLALR1); } else { // => Spontaneous generation of lookaheads childLALR1.Lookaheads.Add(item.Lookahead); } } } } }
/// <summary> /// Gets the LR(1) graph /// </summary> /// <returns>The corresponding LR(1) graph</returns> private Graph GetGraphLR1() { // Create the first set Variable axiom = grammar.GetVariable(Grammar.GENERATED_AXIOM); ItemLR1 item = new ItemLR1(axiom.Rules[0], 0, Epsilon.Instance); 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 StateReductionsLR1()); } return(result); }
/// <summary> /// Builds the given parent graph /// </summary> /// <param name="graph">The parent graph</param> public void BuildGraph(Graph graph) { // Shift dictionnary for the current set Dictionary <Symbol, StateKernel> shifts = new Dictionary <Symbol, StateKernel>(); // Build the children kernels from the shift actions foreach (Item item in items) { // Ignore reduce actions if (item.Action == LRActionCode.Reduce) { continue; } Symbol next = item.GetNextSymbol(); if (shifts.ContainsKey(next)) { shifts[next].AddItem(item.GetChild()); } else { StateKernel nextKernel = new StateKernel(); nextKernel.AddItem(item.GetChild()); shifts.Add(next, nextKernel); } } // Close the children and add them to the graph foreach (Symbol next in shifts.Keys) { StateKernel nextKernel = shifts[next]; State child = graph.ContainsState(nextKernel); if (child == null) { child = nextKernel.GetClosure(); graph.Add(child); } children.Add(next, child); } // Build the context data foreach (Item item in items) { if (item.BaseRule.Context != 0 && item.DotPosition == 0 && item.Action == LRActionCode.Shift) { // this is the opening of a context List <Terminal> openingTerminals = new List <Terminal>(); Symbol first = item.GetNextSymbol(); if (first is Terminal) { openingTerminals.Add(first as Terminal); } else { openingTerminals.AddRange((first as Variable).Firsts); } foreach (Terminal terminal in openingTerminals) { List <int> contexts; if (openingContexts.ContainsKey(terminal)) { contexts = openingContexts[terminal]; } else { contexts = new List <int>(); openingContexts.Add(terminal, contexts); } if (!contexts.Contains(item.BaseRule.Context)) { contexts.Add(item.BaseRule.Context); } } } } }