Skip to content

How to visualize AST

Nathan Pinnow edited this page Aug 29, 2019 · 3 revisions

Overview

The goal of this tutorial is to learn how to use a few pre-built tools of ROSE to generate dot graphs of AST. AST stands for Abstract Syntax Tree, which is the internal tree representation of an input source code. Getting familiar with AST is the first step to develop program analysis and translation tools operating on AST.

Build/Install AST Tools

Build the pdf generator

make -C $ROSE_BUILD/exampleTranslators/PDFGenerator install

Build and install the dot graph generators

make -C $ROSE_BUILD/exampleTranslators/DOTGenerator install

Obtain Example Input Codes

Clone the rose-ast git repo

git clone https://github.com/chunhualiao/rose-ast.git

Enter rose-ast

cd rose-ast

Change a script which sets the environment variables for using ROSE

vim set.rose

Please replace ROSE_INS’s value to be /opt/install/rose_install Finally, source it to set the environment variables.

source ./set.rose

Test if rose-compiler is in your path

which rose-compiler

The command line above should generate an output like

/opt/install/rose_install/bin/rose-compiler

Generate a simple dot graph for an input file

rose-ast already stores dot graphs and their pdf and png versions for a set of C/CPP/Fortran files. You can browse these files.

Alternatively, you can clean up everything and generate a dot graph by yourself.

Clean up all previously generated dot files.

make distclean

Generate a simple dot graph for func1.c

dotGenerator -c func1.c

Check the generated dot file

ls func1.c.dot

Generate a whole dot graph for an input file

A whole AST dot graph contains more information related to symbol tables, types, etc. Generate a whole dot graph for func1.c

dotGeneratorWholeASTGraph -c func1.c

Check the generated whole ast dot file

ls func1.c_WholeAST.dot

Generator a pdf output of AST

Dot graphs cannot be easily visualized. So there is another tool to generate pdf file from large input files. Generate a pdf output for class.cpp

pdfGenerator -c class.cpp

Check the generated pdf file

ls class.cpp.pdf

Rebuild all dot graphs

There is a makefile within rose-ast. You can use it to rebuild all the files. Rebuild all dot files

make all
You can’t perform that action at this time.