# ROSE Compiler Framework/How to visualize AST
### Overview

Three things are needed to visualize ROSE AST:
* Sample input code: you provide it
* a dot graph generator to generate a dot file from AST: ROSE provides dot graph generators
* a visualization tool to open the dot graph: ZGRViewer and Graphviz are used by ROSE developers
If you don't want to install ROSE+ZGRview + Graphvis from scratch, you can directly use ROSE virtual machine image, which has everything you need installed and configured so you can just visualize your sample code.

### Sample input code

Please prepare simplest input code without including any headers so you can get a small enough AST to digest.

### Dot Graph Generator

We provide ROSE_INSTALLATION_TREE/bin/dotGeneratorWholeASTGraph (complex graph) and dotGenerator (a simpler version) to generate a dot graph of the detailed AST of input code.
Tools to generate AST graph in dot format. There are two versions
* dotGenerator: simple AST graph generator showing essential nodes and edges
* dotGeneratorWholeASTGraph: whole AST graph showing more details. It provides filter options to show/hide certain AST information.
command line:
To skip builtin functions
* dotGeneratorWholeASTGraph -DSKIP_ROSE_BUILTIN_DECLARATIONS yourcode.c

### Dot Graph Visualization

To visualize the generated dot graph, you have to install
* Graphviz: http://www.graphviz.org/Download.php .
* ZGRViewer: http://zvtm.sourceforge.net/zgrviewer.html#download. (Version 0.8.x is recommended since 0.9.x has a bugs like inversed (reversed) direction to drag a graph around.)
Please note that you have to configure ZGRViewer to have correct paths to some commands it uses. You can do it from its configuration/setting menu item. Or directly modify the text configuration file (.zgrviewer).
One example configuration is shown below (cat .zgrviewer)
You have to configure the run.sh script to have correct path also
cat run.sh

### Example session

A complete example

### example output

We put some example source files and their AST dump files into: https://github.com/chunhualiao/rose-ast
* for example: https://github.com/chunhualiao/rose-ast/blob/master/func1.c_WholeAST.dot.png
* https://github.com/chunhualiao/rose-ast/blob/master/parallelfor.c_WholeAST.dot.png

### Print AST as horizontal tree

SageInterface functions
A translator (textASTGenerator) is also available, with its source code under exampleTranslators/defaultTranslator .
* make install-tools will install this tool
* textASTGenerator input.c will generate a text output of the entire AST

#### Example use inside of gdb

* to print a portion of AST to the screen
* to print a portion of AST into a text file

#### textASTGenerator

Example command line use:
textASTGenerator -c test_qualifiedName.cpp
cat test_qualifiedName.cpp.AST.txt

### Render the AST in HTML

The repo errington1/ast-to-html contains a tool for rendering the Rose abstract syntax "graph" as collapsible HTML with shared nodes and cycles represented by HTML links. For now, it's available only from the command line. The plan is to add command-line options to omit parts of the tree and to make the tool available as a library. For now, it somewhat arbitrarily omit portions of the tree that originate from the file rose_edg_required_macros_and_functions.h.
The command:
will produce file.C.html which can be viewed with a browser: