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

Don Caton dcaton at shorelinesoftware.com
Mon Jun 20 07:15:38 PDT 2005


-----Original Message-----
From: antlr-interest-bounces at antlr.org [
<mailto:antlr-interest-bounces at antlr.org>
mailto:antlr-interest-bounces at antlr.org] On Behalf Of Paul Johnson
Sent: Monday, June 20, 2005 5:51 AM
To: ANTLR Interest
Subject: [antlr-interest] Example code with multiple AST passes?

> Anyone know if there's a code example which does multiple passes through a
single AST?

I just maintain a pass count so the actions know what pass is current.  In
the .g file:

class MyTreeParser;

{
public:

   MyTreeParser( void )
   {
      m_pass = 0;
   }
  
private:

   int m_pass;
  
   const bool pass( const int passNum ) const
   {
      return m_pass == passNum;
   }
}

And in your top level rule:

topLevelRule : { m_pass++; } ( secondLevelRules )*;

If you don't have a simple top-level rule in your tree parser, just create
one that does nothing but increment the pass count, then calls your existing
top-level rule.

Now, in all of your actions you'll have to do something like this:

foo : bar
{
  if ( pass( 1 ) )
  {
     ...
  }
  else if ( pass ( 2 ) )
  {
     ...
  }
}

and so on.  The only alternative I could come up with is to create multiple
tree parsers that inherit from a single .g file, but that results in
excessive code bloat for any non-trivial tree walker.

--
Don


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050620/245f3986/attachment.html


More information about the antlr-interest mailing list