[antlr-interest] Trying to keep whitespace in an AST

Jamie Penney jpen054 at ec.auckland.ac.nz
Thu Feb 7 19:50:36 PST 2008


Hi all,
I am trying to work out how to create a grammar that will build an AST 
that keeps both comments and some whitespace. Basically the output will 
be formatted code, but we need the semantic information provided by the 
AST for other parts of the system. Any comments and blank lines need to 
be kept in the output code. Is it possible to have rewriting and AST 
generation turned on at the same time, or do I have to write two 
separate grammars? I am new to ANTLR so sorry if I have the wrong idea 
about anything.
To give a concrete example, say I have a language that represents basic 
C style statements like so:

int a    = 0;
int b    = 1;
int c    = 2;

// reassign a
a = b + c;

What I need is the semantic information provided by an AST (whether a 
statement is a declaration, assignment, ect), but I need to transform 
the language partially too. I need to format the individual elements 
consistently, so each would be of the form a = b + c; but I also need to 
retain the newlines and comments between elements.

If anyone could point me in the right direction I would be very grateful.

Thanks,
Jamie Penney


More information about the antlr-interest mailing list