Tips:

Code snippets are shown in one of three ways throughout this environment:

  1. Code that looks like this is sample code snippets that is usually part of an explanation.
  2. Code that appears in box like the one below can be clicked on and it will automatically be typed in to the appropriate terminal window:
    vim readme.txt
    
  3. Code appearing in windows like the one below is code that you should type in yourself. Usually there will be a unique ID or other bit your need to enter which we cannot supply. Items appearing in <> are the pieces you should substitute based on the instructions.
    Add your name here - <name>
    

Features

This is a tutorial to use a few tools to visualize AST


A. 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.

B. Build/Install AST Tools

Build and install the AST text generator

make -C $ROSE_BUILD/exampleTranslators/defaultTranslator install

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

C. 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

D. Generate a text output of AST (horizontally printed)

Try to process func1.c

textASTGenerator -c func1.c

It should generate a text file. The entire AST text file has many builtin functions in the beginning. Let’s view the last 15 lines of it.

tail -n 15 func1.c.AST.txt

You should see something like the following:

            │   └──@0x7f1c4e998410 SgFunctionDeclaration func1.c 1:1 "::foo"
            │       ├──@0x7f1c4ec59500 SgFunctionParameterList func1.c 1:1
            │       │   └──@0x7f1c4e479bf8 SgInitializedName func1.c 1:13 "i"
            │       │       └── NULL
            │       ├── NULL
            │       └──@0x7f1c4e1b9010 SgFunctionDefinition func1.c 2:1
            │           └──@0x7f1c4e252010 SgBasicBlock func1.c 2:1
            │               └──@0x7f1c4e157010 SgReturnStmt func1.c 3:3
            │                   └──@0x7f1c4e186010 SgVarRefExp func1.c 3:10
            ├── NULL
            ├── NULL
            └── NULL

The text output clearly shows the tree representing the function foo in the input file. Besides the tree structure, each line starts with memory address of an AST node, its source file name, line:column numbers, and qualified names, if applicable. There are NULL pointers in the AST. You can safely ignore them.

int foo(int i)
{
  return i;
}

E. 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

F. 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

G. Generate 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

Download the generated PDF from the Sandbox here.

H. 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

References

For more information about AST visualization, please check

  • https://en.wikibooks.org/wiki/ROSE_Compiler_Framework/How_to_visualize_AST
  • https://github.com/rose-compiler/rose/wiki/How-to-visualize-AST