-
Notifications
You must be signed in to change notification settings - Fork 134
How to visualize AST
The goal of this tutorial is to learn how to use a few pre-built tools of ROSE to generate visualizations of the ROSE 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. It is best to use programs that do not include header files as these will significantly increase the size of the tree and make it harder to understand. All example ASTs can be found in the repo.
There are two kinds of tools covered here, (1) the produces dot graphs and (2) produces ASCII output.
Note that the dot generation tools we initially suggest do not visualize the entire tree, because that's usually too large to be useful, but if you need the whole thing, that will be covered in dotGeneratorWholeASTGraph.
First you need to build the tools, you can do this either by building them all together at once:
make -C $ROSE_BUILD install-tools
or separately:
make -C $ROSE_BUILD/exampleTranslators/PDFGenerator install
make -C $ROSE_BUILD/exampleTranslators/DOTGenerator install
make -C $ROSE_BUILD/exampleTranslators/defaultTranslator install
If you already have a source file you want to visualize, you can use that. But if you're learning you can use some of our examples by cloning the rose-ast git repo.
git clone https://github.com/chunhualiao/rose-ast.git
cd rose-ast
You need to then change a script which sets the environment variables for using ROSE. Inside set.rose replace ROSE_INS’s value to be the ROSE install directory. Once the script has been changed source it to setup the rose environment.
vim set.rose
source ./set.rose
You can test if rose-compiler is in your path with the which command.
which rose-compiler
The command line above should generate an output like
/opt/install/rose_install/bin/rose-compiler
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 int the repo or generate them yourself.
You can use make to clean up all previously generated graphs or to make them all again..
make distclean #To clean up all grphs
make all #To generale all graphs
Generate a simple dot graph for func1.c using dotGenerator which.
dotGenerator -c func1.c
A whole AST dot graph contains more information related to symbol tables, types, etc. Generate a whole dot graph for func1.c using dotGeneratorWholeASTGraph. The result can be seen here.
dotGeneratorWholeASTGraph -c func1.c
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 using pdfGenerator. The result can be found in the repo
pdfGenerator -c class.cpp
textASTGenerator will pretty print AST horizontally into a text format. This is easier to view within a terminal and sometimes clearer for larger trees.
Note that after the AST, it prints "Types encountered ...." and lists all the types referenced in the AST. So, you are most likely looking for information in the top of the file.
Again, you first need to install the tool by typing : make -C $ROSE_BUILD install-tools .
More information at
textASTGenerator -c test_qualifiedName.cpp
cat test_qualifiedName.cpp.AST.txt
└──@0x7fe9f1916010 SgProject
└──@0xb45730 SgFileList
└──@0x7fe9f17be010 SgSourceFile
├──@0x7fe9fdf19120 SgGlobal test_qualifiedName.cpp 0:0
│ ├──@0x7fe9f159a010 SgTypedefDeclaration rose_edg_required_macros_and_functions.h 0:0
│ │ └── NULL
│ ├──@0x7fe9f159a390 SgTypedefDeclaration rose_edg_required_macros_and_functions.h 0:0
│ │ └── NULL
│ ├──@0x7fe9f0f59010 SgFunctionDeclaration rose_edg_required_macros_and_functions.h 0:0 "::feclearexcept"
│ │ ├──@0x7fe9f1391010 SgFunctionParameterList rose_edg_required_macros_and_functions.h 0:0
│ │ │ └──@0x7fe9f1258010 SgInitializedName rose_edg_required_macros_and_functions.h 0:0 "::__excepts"
│ │ │ └── NULL
│ │ ├── NULL
│ │ └── NULL
│ ├──@0x7fe9f0f59540 SgFunctionDeclaration rose_edg_required_macros_and_functions.h 0:0 "::fegetexceptflag"
│ │ ├──@0x7fe9f1391630 SgFunctionParameterList rose_edg_required_macros_and_functions.h 0:0
│ │ │ ├──@0x7fe9f1258420 SgInitializedName rose_edg_required_macros_and_functions.h 0:0 "::__flagp"
│ │ │ │ └── NULL
│ │ │ └──@0x7fe9f1258628 SgInitializedName rose_edg_required_macros_and_functions.h 0:0 "::__excepts"
│ │ │ └── NULL
│ │ ├── NULL
│ │ └── NULL
...
│ └──@0x7fe9eff218c0 SgFunctionDeclaration test_qualifiedName.cpp 14:1 "::foo"
│ ├──@0x7fe9ef5e0320 SgFunctionParameterList test_qualifiedName.cpp 14:1
│ │ ├──@0x7fe9ef495278 SgInitializedName test_qualifiedName.cpp 14:13 "x"
│ │ │ └── NULL
│ │ └──@0x7fe9ef495480 SgInitializedName test_qualifiedName.cpp 14:20 "y"
│ │ └── NULL
│ ├── NULL
│ └──@0x7fe9ee8f3010 SgFunctionDefinition test_qualifiedName.cpp 15:1
│ └──@0x7fe9ee988010 SgBasicBlock test_qualifiedName.cpp 15:1
│ ├──@0x7fe9eee1ba90 SgVariableDeclaration test_qualifiedName.cpp 16:3
│ │ ├── NULL
│ │ └──@0x7fe9ef495688 SgInitializedName test_qualifiedName.cpp 16:3 "z"
│ │ └── NULL
│ ├──@0x7fe9ee7ad010 SgExprStatement test_qualifiedName.cpp 17:3
│ │ └──@0x7fe9ee7dc010 SgAssignOp test_qualifiedName.cpp 17:5
│ │ ├──@0x7fe9ee8c0010 SgVarRefExp test_qualifiedName.cpp 17:3
│ │ └──@0x7fe9ee813010 SgAddOp test_qualifiedName.cpp 17:9
│ │ ├──@0x7fe9ee8c0078 SgVarRefExp test_qualifiedName.cpp 17:7
│ │ └──@0x7fe9ee84a010 SgMultiplyOp test_qualifiedName.cpp 17:12
│ │ ├──@0x7fe9ee8c00e0 SgVarRefExp test_qualifiedName.cpp 17:11
│ │ └──@0x7fe9ee881010 SgIntVal test_qualifiedName.cpp 17:13
│ └──@0x7fe9ee77e010 SgReturnStmt test_qualifiedName.cpp 18:3
│ └──@0x7fe9ee8c0148 SgVarRefExp test_qualifiedName.cpp 18:10
├── NULL
├── NULL
└── NULL