/// <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 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 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); }