[antlr-interest] Antlr dropping tokens?

John B. Brodie jbb at acm.org
Mon Jan 21 08:15:34 PST 2008


Jon Schewe wrote (in part):
>Here's a piece of my grammar that I'm testing with junit.  It appears
>that some tokens are getting dropped.
>The input is this:
>b[1 ... (param1 - 5 - 1) * 4]
...snipped...
>/**
>* @param negate if true, negate all expressions by multiplying by -1
>*/
>subscriptMultExpr[boolean negate]
>:
>  a+=subscriptAtom (a+=subscriptMultHelp)* -> {negate}? ^(PRODUCT
>^(NUMBER NUM_INT["-1"]) $a)
>                                           ->           ^(PRODUCT $a)
>;
>
>subscriptMultHelp : PRODUCT subscriptAtom -> subscriptAtom ;

I have not tested the following but I believe it to be the truth...

You are accumulating a list of nodes in `a` on the left hand side of
the rewrite (e.g. using the a+=... form).  Yet on the right hand side
you only use `$a` which I believe resolves to be just the first
element of the accumulated list.  Thus only the first operand is
placed into the tree, dropping the second, third, and subsequent
operands.

Use $a+ on the right hand side of the rewrite in subscriptMultExpr.

Hope this helps...
   -jbb


More information about the antlr-interest mailing list