[antlr-interest] Best practice for ASTs with expression types / annotation / pattern matching on annotations

Barry Kelly barry.j.kelly at gmail.com
Fri Oct 22 09:11:24 PDT 2004



I'm writing what will, essentially, be a functional language
interpreter. This means that the program is formed from expression
trees. My strategy looks something like this:

1) Use buildAST=true to create the initial AST in the grammar.
2) Use a tree parser to rewrite adding in type information. At first I
thought of using a 'prefix' node to indicate type, so that, for every
subtree where it logically plugs into another tree grammar production,
has a new root node which indicates the type of the subtree
expression:

"if x then y else z" => (IF x y z) initially
- Perform type lookup on y discovering it is an int.
- Perform type lookup on z discovering it is a float.
- Rewrite the tree so that it looks something like:

  (FLOAT (IF_FLOAT (BOOL x) (FLOAT (INT_TO_FLOAT (INT y))) (FLOAT z))).

The idea behind these prefixes is that tree grammar rewriting
productions can be written that match on the types, doing:

ifFloat
  :! #(IF a:BOOL b:FLOAT c:INT)
     { #ifFloat = #([FLOAT] #([IF_FLOAT] a b #([FLOAT] #([INT_TO_FLOAT] c))); }
  ;

(Excuse syntactical inaccuracies, I'm evaluating strategies, I haven't
decided to try to build it this way yet.)

Is this path destined to disaster? Should I use some kind of
attribute-based system instead, possibly using syntactical predicates?
Will I run into problems with k=1 lookahead in the AST and matching?

-- Barry Kelly





 
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