[antlr-interest] Example code with multiple AST passes?

Don Caton dcaton at shorelinesoftware.com
Mon Jun 20 11:21:23 PDT 2005


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org 
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Bryan Ewbank
> Sent: Monday, June 20, 2005 12:57 PM
> To: ANTLR Interest
> Subject: Re: [antlr-interest] Example code with multiple AST passes?
> 
> Thank you, Don.  That's a very interesting solution to a 
> problem that I've been encountering as I have more and more 
> passes, especially when I need to add a new token to *every* 
> pass.  *Groan*.

Brian:

It works, but it's not what I'd call an elegant solution.  You need code in
each action to make sure that the action only occurs in the desired pass,
and if the rule has default tree construction turned off, you can't forget
to return a tree in an action, even if you don't have anything else to do,
Ex:

  foo! : bar
  {
     if ( pass(1) )
     {
         ## = ...;
     }
  }

This will crash very nicely during an pass other than #1 because no tree is
returned.  It's a pain when adding new passes.

I wish Antlr implemented true parser inheritance when inheriting from a
grammar.  Ideally, Antlr should generate a tree parser class that subclasses
the parser created by the superclass grammar, which just overrides the
action code.  Otherwise, you have huge amounts of code duplication, which is
what led me to seek an alternate solution.  It works well enough but I
really wish we could override antlr-generated parsers with classes that just
implemented actions.  I really, really hate mixing rules and executable code
in the same file.

I hope Antlr 3 addresses this somehow; it's my only real complaint with
Antlr 2.

Don




More information about the antlr-interest mailing list