[antlr-interest] Structure of AST

mzukowski at yci.com mzukowski at yci.com
Wed Apr 9 09:41:25 PDT 2003


I recommend studying my gcc transformation framework
http://codetransform.com/gcc.html.  See, for instance, how I structured the
"for" statement into a tree.  In C the for statement has three optional
parts:

        |!       "for"
                LPAREN ( e1:expr )? SEMI ( e2:expr )? SEMI ( e3:expr )?
RPAREN
                s:statement
                                    {
                                        if ( #e1 == null) { #e1 = #[
NEmptyExpression ]; }
                                        if ( #e2 == null) { #e2 = #[
NEmptyExpression ]; }
                                        if ( #e3 == null) { #e3 = #[
NEmptyExpression ]; }
                                        ## = #( #[LITERAL_for, "for"], #e1,
#e2, #e3, #s );
                                    }

You don't want to have a tree parser with (expr)? (expr)? (expr)? -- it's
ambiguous.  So I force a structure to it and handle the empty expressions
with my own imaginary node NEmptyExpression.  Treeparser rule looks like
this:

        |       #( "for"
                expr expr expr
                statement
                )

Take a look in my grammar to see how I use imaginary nodes as well.  Some
advocate no optional closures -- ()? -- in tree parsers.  Also you should
never need syntactic predicates either since you are building the tree you
can make it unambiguous.

And for sure read
http://www.cs.usfca.edu/~parrt/course/652/lectures/java.ast.html

Monty

 -----Original Message-----
From: Nitin Gupta [mailto:nitin_bitkmc at yahoo.com]
Sent: Tuesday, April 08, 2003 10:59 PM
To: antlr-interest at yahoogroups.com
Subject: [antlr-interest] Structure of AST


I have to build a language translator in java. I have used antlr 2.7.2a2 to
generate parser which is working fine. Now i have to build an AST so that i
can write a tree walker to convert the source language into target.
I have no idea about what structure should my AST have. What is the ideal
structure, and how to go about it?
If someone has any input for this please help me out.
Thanks and Regards
Nitin




Do you Yahoo!?
Yahoo! Tax Center - File online, calculators, forms, and more 
Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service. 

 

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




More information about the antlr-interest mailing list