[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Output text from AST


I had a very similar problem.  The easiest way I found to do it is to
write a custom lexer that builds up a linked list of whitespace
characters before each token.  It then makes an entry into the hashtable
(using setIn I seem to remember), associating the list with the Token
that follows it.  Then you can simply make the deafaultCase in your
Walker class look up the list for each token passed to it and print that
to the output stream before outputting the token's string value.  This
does not pretty print, but does preserve the original comments and
indentation structure.  

I then rigged a framework over the top of this to appropriately indent
extra code I was emitting from my pre-processor.  Take care if you go
down this route because I've got myself seriously tied up in knots with
this approach!

Hope that's of some help,

Jim Moores

PS.  No disrespect to SableCC (it's the first Compiler-Compiler I've
ever been able to get my head around!), but if you are implementing a
Java pre-processor, you might want to look at EPP which has access to
type information and might be easier (although the documentation is
awful and the learning curve quite steep).  

Also, check out the package that it uses to read class files - the BCEL
(the byte-code engineering library), which you should be able to
integrate with SableCC to build symbol tables etc.   Of course this
might not be relevant to you... 
Dr James Moores, Director, Quickstone Technologies Limited, UK.
-----Original Message-----
From: Nicholas Read [mailto:Nicholas.Read@utas.edu.au] 
Sent: 25 July 2002 09:40
To: SableCC Mailing List
Subject: Output text from AST

Hi again,

I'd like to thank Thomas for his quick responce to my previous question.

Up till now, I have modified the Java grammer slightly, parsed a java
source program with this modified grammer, and then transformed the AST
in to a completely j11.sablecc compatible AST.

I was wondering if any one has previously created a "pretty printer"
adapter that traverses a standard Java AST and outputs the Java code.
Is there any standard way in which this can be done with out having
to write over-riding every production rule's method?  I'm just trying
to save myself from writing a heap of code here :)

Thanks in advance.


Nicholas Read, BSc
Hobart, Tasmania

Email:  Nicholas.Read@utas.edu.au
Mobile: 0429-487-700
ICQ #:  20770226

"Bud, if dynamite was dangerous do you think they'd
 sell it to an idiot like me?"
   - Al Bundy, "Married with Children"