/// <summary> /// Exports the given LR automaton to a DOT graph in the specified file /// </summary> /// <param name="graph">The LR automaton to export</param> /// <param name="file">DOT file to export to</param> public static void ExportDOT(Graph graph, string file) { DOTSerializer serializer = new DOTSerializer("LR", file); foreach (Hime.SDK.Grammars.LR.State state in graph.States) { List <string> items = new List <string>(); foreach (Item item in state.Items) { if (item.Action == Hime.Redist.Parsers.LRActionCode.Reduce) { items.Add(item.ToString()); } } serializer.WriteStructure("state" + state.ID, state.ID.ToString(), items.ToArray()); } foreach (Hime.SDK.Grammars.LR.State state in graph.States) { foreach (Hime.SDK.Grammars.Symbol symbol in state.Transitions) { serializer.WriteEdge("state" + state.ID, "state" + state.GetChildBy(symbol).ID, symbol.ToString()); } } serializer.Close(); }
/// <summary> /// Exports the given DFA to a DOT graph in the specified file /// </summary> /// <param name="dfa">The DFA to export</param> /// <param name="file">DOT file to export to</param> public static void ExportDOT(DFA dfa, string file) { DOTSerializer serializer = new DOTSerializer("DFA", file); foreach (DFAState state in dfa.States) { if (state.IsFinal) { ROList <FinalItem> items = state.Items; StringBuilder builder = new StringBuilder(state.ID.ToString()); builder.Append(" : "); for (int i = 0; i != items.Count; i++) { if (i != 0) { builder.Append(", "); } builder.Append(items[i].ToString()); } serializer.WriteNode("state" + state.ID, builder.ToString(), DOTNodeShape.doubleoctagon); } else { serializer.WriteNode("state" + state.ID, state.ID.ToString()); } } foreach (DFAState state in dfa.States) { foreach (CharSpan value in state.Transitions) { serializer.WriteEdge("state" + state.ID, "state" + state.GetChildBy(value).ID, value.ToString()); } } serializer.Close(); }
/// <summary> /// Exports the given AST node with the given serializer /// </summary> /// <param name="serializer">The DOT serializer</param> /// <param name="parent">The parent node ID</param> /// <param name="nextID">The next available ID for the generated DOT data</param> /// <param name="node">The node to serialize</param> /// <returns>The next available ID for the generate DOT data</returns> private static int ExportNode(DOTSerializer serializer, string parent, int nextID, ASTNode node) { string name = "node" + nextID; string label = node.Symbol.ToString(); serializer.WriteNode(name, label, DOTNodeShape.circle); if (parent != null) { serializer.WriteEdge(parent, name, string.Empty); } int result = nextID + 1; foreach (ASTNode child in node.Children) { result = ExportNode(serializer, name, result, child); } return(result); }
/// <summary> /// Exports the given NFA to a DOT graph in the specified file /// </summary> /// <param name="nfa">The NFA to export</param> /// <param name="file">DOT file to export to</param> public static void ExportDOT(NFA nfa, string file) { DOTSerializer serializer = new DOTSerializer("DFA", file); for (int i = 0; i != nfa.States.Count; i++) { NFAState state = nfa.States[i]; if (state.IsFinal) { ROList <FinalItem> items = state.Items; StringBuilder builder = new StringBuilder(i.ToString()); builder.Append(" : "); for (int j = 0; i != items.Count; i++) { if (i != 0) { builder.Append(", "); } builder.Append(items[j].ToString()); } serializer.WriteNode("state" + i, builder.ToString(), DOTNodeShape.doubleoctagon); } else { serializer.WriteNode("state" + i, i.ToString()); } } for (int i = 0; i != nfa.States.Count; i++) { NFAState state = nfa.States[i]; foreach (NFATransition transition in state.Transitions) { int to = nfa.States.IndexOf(transition.Next); serializer.WriteEdge("state" + i, "state" + to, transition.Span.ToString()); } } serializer.Close(); }
/// <summary> /// Exports the given LR automaton to a DOT graph in the specified file /// </summary> /// <param name="automaton">The LR automaton to export</param> /// <param name="file">DOT file to export to</param> public static void ExportDOT(LRAutomaton automaton, string file) { DOTSerializer serializer = new DOTSerializer("LR", file); foreach (LRState state in automaton.States) { string[] items = new string[state.Reductions.Count]; for (int i = 0; i != state.Reductions.Count; i++) { items[i] = state.Reductions[i].ToString(); } string label = state.IsAccept ? state.ID + "=Accept" : state.ID.ToString(); serializer.WriteStructure("state" + state.ID, label, items); } foreach (LRState state in automaton.States) { foreach (LRTransition transition in state.Transitions) { serializer.WriteEdge("state" + state.ID, "state" + transition.Target.ID, transition.Label.ToString()); } } serializer.Close(); }