[antlr-interest] AST generation: EXPRESSION TREE example.

Monty Zukowski monty at codetransform.com
Fri Jun 4 10:38:01 PDT 2004


On Jun 3, 2004, at 7:37 AM, Bharath Sundararaman wrote:

> Hi Monty,
>
> I need the AST for semantic analysis, constant folding and also code
> generation. To be more specific, I have written the grammar for a
> PASCAL-like (not OO based) language, from scratch. Right now, my syntax
> analysis is complete. Instead of having semantic analysis within the 
> parser,
> I would like to build an AST and and write a tree parser that will 
> walk the
> tree structure to perform various analyses. For example, to get a good 
> idea
> of what I'm dealing with, I'm starting off with type checking and this 
> will
> drive the structure of my AST.
>
Code generation to a virtual machine, assembler, C or what?

> - The AST will always be manipulated by the grammar.
> - Comment tokens will be skipped.
So you don't have to worry about formatting preservation.
>
> Some questions:
>
> 1. Can you give me an example situation where I would need to preserve
> formatting? In my grammar, I ignore whitespace, newlines, tabs and 
> feeds.
> What kind of language would require comments to be tokens?
If you are doing source to source translation you probably want to 
preserve the information from formatting.  If you are making a compiler 
you can discard it.

>
> 2. Original possibly extraneous parenthesis -- I'm banging(!) all
> punctuations and paranthesis from the AST. I'm not clear about your 
> question
> but I hope I gave the answer.
Yeah, you're not doing source to source translation.

>
> 3. Are there any examples of how scope of variables can be checked by
> walking an AST?
The C grammar has a symbol table you might want to study.

>
> I will look at your C grammar, Java grammar and also PASCAL grammar to 
> gain
> more insight. I just stepped into the world of ASTs and I'm willing to 
> help
> with you with the AST guide in any way possible.
>
> Thanks a bunch,
>
> Bharath.
>
> -----Original Message-----
> From: Monty Zukowski [mailto:monty at codetransform.com]
> Sent: Wednesday, June 02, 2004 11:33 PM
> To: antlr-interest at yahoogroups.com
> Cc: Monty Zukowski
> Subject: Re: [antlr-interest] AST generation: EXPRESSION TREE example.
>
>
> On Jun 2, 2004, at 2:58 PM, Bharath Sundararaman wrote:
>
>> To generate
>> an AST with a decent structure (based on my needs), my understanding
>> is that
>> I have to add qualifiers (^, !) to each rule and modify existing rules
>> to
>> make them AST friendly. Is this a cumbersome process?
>
> Well, it can be.  Loring and I were recently debating what makes a good
> tree, and some of that ended up here a month or so ago in a dialog
> between me and John Mitchell about the NULL node placeholder.  I
> believe the java grammar builds a tree, my C grammar is also a good
> example.  In my latest ANTLR contract I was building a SQL tree but I
> had special considerations because everything had to be accessible from
> an embedded language.  I concluded that there are very many different
> reasons for wanting a tree that will guide a strategy toward building
> them.
>
> So, are you doing source to source translation?  Are you translating to
> a different language, or to the same language (like my C toolkit does)?
>   Are you only building a tree for analysis?  Are you building a tree 
> to
> be manipulated directly by raw code or will it always be manipulated by
> a tree grammar (my recommendation.)  Do you have to preserve
> formatting?  Comments?  Original possibly extraneous parenthesis?
>
> Let's work this through and maybe I can write up a guide for it when
> I've got some "free" time.
>
> Please, anybody join in and tell me what you're doing and let me know
> what's worked for you and what hasn't and what your questions are.
> I'll probably only get to this once or twice a day but hopefully we can
> ferret out some real life questions and solutions over the next week or
> so.  I know we've got plenty of experts out there too.  This is the
> kind of stuff that will help shape ANTLR 3.  Loring's got some amazing
> tree integration stuff on the horizon but there is always a place for
> understanding why people want trees and how to use them most
> effectively.
>
> Monty Zukowski
>
> ANTLR & Java Consultant -- http://www.codetransform.com
> ANSI C/GCC transformation toolkit --
> http://www.codetransform.com/gcc.html
> Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>
Monty Zukowski

ANTLR & Java Consultant -- http://www.codetransform.com
ANSI C/GCC transformation toolkit -- 
http://www.codetransform.com/gcc.html
Embrace the Decay -- http://www.codetransform.com/EmbraceDecay.html



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list