[antlr-interest] Re: Please help me with homogeneous versus heterogeneous trees!

Paul Johnson gt54-antlr at cyconix.com
Mon Aug 1 01:48:41 PDT 2005


There's no right answer - just choose one, get on with it, and regret it 
at your leisure afterwards.

If you wanted a poll, then I (currently):

- use heterogeneous

- have about 120 node types

- don't use tree grammars

- 7 passes; 1 creates the tree, 2 further ones modify the tree structure

- do manual tree walks

- (very) occasionally have to look back up the tree for context

- the tree structures should be correct by design, but I still have lots 
of assertion checks in my walkers to manually validate the structure

This seems to work well.

Note that I *don't* have 120 source files for all these classes; that 
would be ridiculous. Very few classes need to override methods, and I 
combine 'functionality' into files, rather than 'types'. Sometimes I 
also find it simpler, clearer, and more maintainable just to 'switch' on 
a node's type than to use polymorphism; the hetero approach certainly 
doesn't exclude this.

The only difficult part of the process is that I have 3 files which 
define the basic node types, define the AST subclasses themselves, and 
list the factory methods. This is messy, easy to get wrong, and 
difficult to maintain. On the other hand, it's trivial and a lot of it 
could be automated with a script.

HTH

Paul



More information about the antlr-interest mailing list