[antlr-interest] Is there an easier way?

Alan D. Cabrera list at toolazydogs.com
Tue Nov 29 06:35:33 PST 2011


I'm using a tree walker that does topdown/bottom up tree pattern matching to generate code from my tree.  I'm having problems wrapping with code expressions that depend on the immediate parent.  For example, I have a nodes

^(VARLIST .+)
^(EXPLIST .+)

where EXPLIST  and VARLIST contains a list of expressions.  Some examples are:

(VARLIST (INDEX (FUNCALL (VAR g) (ARGS (VAR x) (VAR y) (VAR z))) (VAR i)) (VAR i) (INDEX (VAR a) (STRING b))) 
(EXPLIST (VAR i) 2 (FUNCALL (VAR f) (ARGS (VAR x) (VAR y) (VAR z)))

I want to wrap expressions in VARLIST differently than how they would get wrapped in EXPLIST.  I end up with something like:

downVarListExp
@init {CommonTree t = (CommonTree)input.LT(1);}
    : {t.getParent().getType() == VARLIST}? VAR {
        // generate code
    }
    ;

upVarListExp
@init {CommonTree t = (CommonTree)input.LT(1);}
    : {t.getParent().getType() == VARLIST}? VAR {
        // generate code
    }
    ;

The reason that I don't use hasAncestor() is that expressions can be nested and I don't want these to be executed inside of an expression; I'm just trying to individually wrap expressions at the "outermost" level.  Is there an easier/cleaner way to wrap the expressions that are nested in ^(VARLIST .+) and ^(EXPLIST .+) using topdown/bottom up tree walking pattern matching?


Regards,
Alan




More information about the antlr-interest mailing list