Example #1
0
 /// <summary>
 /// Builds the LALR(1) graph
 /// </summary>
 private void BuildGraphLALR1()
 {
     // Build states
     foreach (StateKernel kernelLALR1 in kernels)
     {
         graphLALR1.Add(kernelLALR1.GetClosure());
     }
     // Link and build actions for each LALR(1) set
     for (int i = 0; i != graphLALR1.States.Count; i++)
     {
         State stateLALR1 = graphLALR1.States[i];
         State stateLR0   = graphLR0.States[i];
         // Set ID
         stateLALR1.ID = i;
         // Link
         foreach (Symbol symbol in stateLR0.Transitions)
         {
             State childLALR1 = graphLALR1.States[stateLR0.GetChildBy(symbol).ID];
             stateLALR1.AddChild(symbol, childLALR1);
         }
         // Propagate the contexts
         stateLALR1.CopyContextsOf(stateLR0);
         // Build
         stateLALR1.BuildReductions(new StateReductionsLALR1());
     }
 }
Example #2
0
 /// <summary>
 /// Export the content of the given LR state to the specified writer
 /// </summary>
 /// <param name="writer">The writer to export with</param>
 /// <param name="state">The LR state to export</param>
 /// <param name="grammar">The associated grammar</param>
 private static void ExportLRState(TextWriter writer, Hime.SDK.Grammars.LR.State state, Grammar grammar)
 {
     writer.WriteLine();
     writer.WriteLine("State {0}:", state.ID);
     writer.WriteLine("\tContexts:");
     foreach (Hime.SDK.Grammars.Symbol symbol in state.Transitions)
     {
         Terminal terminal = symbol as Terminal;
         if (terminal != null)
         {
             ROList <int> contexts = state.GetContextsOpenedBy(terminal);
             foreach (int context in contexts)
             {
                 writer.WriteLine("\t\tOn {0} opening context {1}: {2}", terminal, context, grammar.GetContextName(context));
             }
         }
     }
     writer.WriteLine("\tTransitions:");
     foreach (Hime.SDK.Grammars.Symbol symbol in state.Transitions)
     {
         writer.WriteLine("\t\tOn {0} shift to {1}", symbol, state.GetChildBy(symbol).ID);
     }
     writer.WriteLine("\tItems:");
     foreach (Item item in state.Items)
     {
         writer.WriteLine("\t\t" + item.ToString(true));
     }
     writer.WriteLine("\tConflicts:");
     foreach (Conflict conflict in state.Conflicts)
     {
         ExportLRConflict(writer, conflict);
     }
 }
Example #3
0
 /// <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);
                 }
             }
         }
     }
 }