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

Jamie Penney jpen054 at ec.auckland.ac.nz
Thu Feb 7 21:24:56 PST 2008


My apologies - one of the other email lists I use has the reply-to 
address set to the list address.

Matt Benson wrote:
> copying list...
>
> --- Jamie Penney <jpen054 at ec.auckland.ac.nz> wrote:
>
>   
>> Date: Fri, 08 Feb 2008 17:25:04 +1300
>> From: Jamie Penney <jpen054 at ec.auckland.ac.nz>
>> To: Matt Benson <gudnabrsam at yahoo.com>
>> Subject: Re: [antlr-interest] Trying to keep
>> whitespace in an AST
>>
>> What I need to do is use the AST to create an in
>> memory model of the 
>> code using our own model classes. So for instance we
>> have a model to 
>> represent a method, which needs to know everything
>> about the method. It 
>> needs the text of the method, but doesn't need to
>> parse it. I need to be 
>> able to record the amount of whitespace and the
>> comments that come 
>> before the method, so that I can put that into our
>> code model objects. 
>> These code model objects are used for a bunch of
>> different stuff, 
>> modified, then printed out (which does the actual
>> pretty printing).
>>
>> So I guess I need an AST that keeps newlines and
>> comments associated 
>> with particular nodes. The pretty printing is done
>> by our code so the 
>> grammar doesn't need to do that.
>>
>> Thanks,
>> Jamie Penney
>>
>> Matt Benson wrote:
>>     
>>> Are you trying to pretty-print all the source, or
>>>       
>> just
>>     
>>> what you've augmented?  Does this wiki entry
>>>       
>> approach
>>     
>>> what you're looking for:
>>>
>>>
>>>       
> http://www.antlr.org/wiki/pages/viewpage.action?pageId=1752
>   
>>> ?
>>>
>>> -Matt
>>>
>>> --- Jamie Penney <jpen054 at ec.auckland.ac.nz>
>>>       
>> wrote:
>>     
>>>   
>>>       
>>>> 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
>>>>
>>>>     
>>>>         
>>>
>>>      
>>>       
> ____________________________________________________________________________________
>   
>>> Looking for last minute shopping deals?  
>>> Find them fast with Yahoo! Search. 
>>>       
> http://tools.search.yahoo.com/newsearch/category.php?category=shopping
>   
>>>   
>>>       
>>     
>
>
>
>       ____________________________________________________________________________________
> Never miss a thing.  Make Yahoo your home page. 
> http://www.yahoo.com/r/hs
>   

-- 
Jamie Penney

http://www.jamiepenney.co.nz


More information about the antlr-interest mailing list