views:

711

answers:

2

I am studying Compilers course, compilation of program follows below steps

  1. Lexical analysis
  2. Syntax analysis
  3. Semantic analysis
  4. Intermediate code generation
  5. Code optimization
  6. Target code generation.

How can I see output of each step e.g I want to see parse tree after syntax analysis.

I am compiling program on Linux machine with GCC compiler.

We can see assembly code of the program by using -Wa compiler option in gcc, similarly is there options to see Tokens, Parse tree and Inetmediate code.

+2  A: 

You can see the preprocessor output with -E. -fdump-tree-* dumps the tree internal represenation, e.g. -fdump-tree-all. Various -d options exist to dump the RTL intermediate representations, e.g. -fdump-rtl-all (see the manual for the invidual passes that you get dumps of); in addition, -dD dumps all macro definitions.

Martin v. Löwis
+3  A: 

While you can use the -fdump-tree-all and -fdump-rtl-all options in gcc, I don't think that their output is very useful to a compiler student. FWIW, I started working on gcc as part of my PhD studies, having already completed two undergraduate courses, and I found gcc and its debug files to be opaque and hard to follow.

In addition, gcc doesn't really follow the textbook design of compilers. No-one does, really, because it doesn't work well that way. I'm pretty sure gcc doesn't produce a parse tree, or an abstract-syntax-tree. It does build an IR (called gimple) on which to perform its high-level optimizations.

I would suggest to try LLVM instead, which has a reputation for being well designed and easy to follow. Another alternative is to download the code from a textbook, especially the Appel book, assuming its available.

Another suggestion, if I may recommend my own for a moment, is to use phc. With phc, you can see the parse tree as an image, and view the AST and the source code after every single pass in the compiler. Here is a comparison of parts of the AST and the parse tree. They are generated trivially using phc. On the dataflow branch, you can see the compiler IRs, the CFG, SSA form, and debug output of type inference and alias analysis. You can also turn optimizations and passes on and off to see the effect that they have.

I think this could be useful for you.

Paul Biggar