[antlr-interest] Making multiple passes over a tree

Bryan Ewbank ewbank at gmail.com
Thu Jul 14 10:44:48 PDT 2005


For example, assuming DECL and ASSIGN tokens for the two statements,
the tree walkers for the first and second pass would be:

    // pass one - build the symbol table
    program_pass1: ( stmt )* ;
    stmt: decl | ASSIGN;
    decl : #( DECL ... ) { take-declaration-action; };

    // pass two - do the assignment operations
    program_pass2: ( stmt )* ;
    stmt: DECL | assign;
    assign : #(ASSIGN ... ) { take-assign-action; };

Note that the ASSIGN node is bypassed in the first pass, and the DECL
node is bypassed in the second pass.

On 7/14/05, Ric Klaren <ric.klaren at gmail.com> wrote:
> On 13 Jul 2005 11:51:36 -0000, Subhobroto  Sinha
> <subhobrotosinha at rediffmail.com> wrote:
> >  firstPass : This pass will walk JUST ONLY OVER the variable declaration
> > rules and build up a symbol table
> >  secondPass : This will walk over the assignment statements and detect
> > semantic errors
> >  etc...
> >
> >  Right now, I have each of the passes walking over the full AST again and
> > again, altough each pass has actually a very specific task to do.
> >
> >  So I was thinking if I could somehow make the treeparser such that it will
> > skip over those portions of the AST with which it's not concerned.
> >
> >  For example, in the first pass, I would like to parse JUST THOSE statements
> > which are declaring new variables but not assignent statements...
> 
> An antlr treeparser always goes down from the top. So you'd be stuck
> with the solution you now have or alternatively you can build parts of
> the symbol table during the normal parsing e.g. collect the subtrees
> with the variable declarations and run over them separately.
> 
> Using the wildcard match symbol '.' you can skip the variable bits
> during the normal run of the tree parser.
> 
> Cheers,
> 
> Ric
>


More information about the antlr-interest mailing list