Created
November 4, 2012 16:04
-
-
Save karadaisy/4012424 to your computer and use it in GitHub Desktop.
export AST to GraphViz DOT file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.InputStreamReader; | |
import java.io.PrintStream; | |
import java.lang.reflect.Field; | |
public class AstToDot { | |
/** | |
* @param args | |
* @throws Exception | |
*/ | |
public static void main(String[] args) throws Exception { | |
ASTLexer lexer = new ASTLexer(new InputStreamReader(System.in)); | |
ASTParser parser = new ASTParser(lexer); | |
TreeNode root = (TreeNode) parser.parse().value; | |
exportAstDigraph(root); | |
} | |
private static int idCounter = 0; | |
/** | |
* Export a DOT file representing the abstract syntax tree to ast.dot | |
*/ | |
private static void exportAstDigraph(TreeNode root) { | |
try { | |
PrintStream out = System.out; | |
out.println("digraph AST {"); | |
exportAstDigraph(root, out); | |
out.println("}"); | |
out.close(); | |
} | |
catch(Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static String exportAstDigraph(TreeNode root, PrintStream out) throws IllegalArgumentException, IllegalAccessException { | |
String nodeId = Integer.toString(idCounter++); | |
StringBuilder label = new StringBuilder(); | |
Class<?> rootClass = root.getClass(); | |
label.append(rootClass.getSimpleName()); | |
while(rootClass != null) { | |
for(Field field : rootClass.getDeclaredFields()) { | |
field.setAccessible(true); | |
String fieldName = field.getName(); | |
Class<?> type = field.getType(); | |
if(AbstractSymbol.class.isAssignableFrom(type)) { | |
AbstractSymbol sym = (AbstractSymbol) field.get(root); | |
label.append("\\n"); | |
label.append(fieldName + ": " + sym); | |
} | |
else if(ListNode.class.isAssignableFrom(type)) { | |
ListNode children = (ListNode) field.get(root); | |
Iterable<TreeNode> childIt = Utilities.iterable(children); | |
int counter = 0; | |
for(TreeNode child : childIt) { | |
String childId = exportAstDigraph(child, out); | |
out.println(nodeId + " -> " + childId + " [label=\"" + fieldName + "_" + (counter++) + "\"];"); | |
} | |
} | |
else if(TreeNode.class.isAssignableFrom(type)) { | |
TreeNode child = (TreeNode) field.get(root); | |
String childId = exportAstDigraph(child, out); | |
out.println(nodeId + " -> " + childId + " [label=\"" + fieldName + "\"];"); | |
} | |
out.println(nodeId + "[label=\"" + label + "\"];"); | |
} | |
rootClass = rootClass.getSuperclass(); | |
} | |
return nodeId; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment