[antlr-interest] Heterogeneous AST support?

Ric Klaren ric.klaren at gmail.com
Tue Jun 21 07:51:06 PDT 2005


On 6/21/05, Paul Johnson <gt54-antlr at cyconix.com> wrote:
> Following on from the 'Example code with multiple AST passes' thread, my
> feeling is that I should use a single class-per-token in my AST, and
> have virtual over-ridden pass processing code in each token class (the
> homogeneous alternative being a large 2-way switch on the token type and
> current pass).
> 
> However, the comments (from Ric?) in the ASTFactory.cpp source are
> making me think twice about this (the comments at the end give the
> impression that the hetero code may not work). Does anyone have any
> experience of building and manipulating heterogeneous ASTs? Any
> potential problems/pitfalls?

Well the heterogeneous AST stuff for C++ is not the prettiest. Theres
a few ugly hacks in the codegen to make it work most of the time. But
not *all* the time (you may confuse the codegenerator with some AST
construction constructs). Also the support library may have some
pitfalls. It's gotten better over time. I know there's people who have
used it succesfully. But don't be surprised if you run into strange
things. Also some syntax used to override the AST types to be created
does not work 100%.

I may be representing things gloomier than they are.. yet..
YMMV. If you feel young and gung-ho go for it ;) If you never want to
look at the generated code then don't. As usual I'll assist in
bugfixing as much as I can spare time (but that resource has gotten a
bit scarcer nowadays).

> (the
> homogeneous alternative being a large 2-way switch on the token type and
> current pass).

Keep your tokens as dumb as possible is my advice. Heck the only good
token is a number ;) Don't go shove algorithm in your tokens (if you
intended to do that).

In stead of switches on passes you can also have multiple tree walkers
that you call sequentially (pass around some shared data (may get ugly
though..)). Or call treewalkers inside treewalkers to collect
information about a subtree (a more recursive approach). It depends on
what you want to do (didn't follow the other thread) but some things
work better with a treewalker while others work better with a visitor.

Cheers,

Ric


More information about the antlr-interest mailing list