[antlr-interest] 1pass cannot add/subtract more than two consecutive integers

Michael West quagly at me.com
Fri Mar 25 15:52:14 PDT 2011


There seems to be a problem in the definitive guide example code
templates/generator/1pass/Expr.g

If more that two integers are added then the middle ones drop out, that is
1+2+3+4 becomes 1+4 in the generated jasmin.  Seems to be that ops is not
treated as an array in the expr rule.  My fix for this is:

Original Expr.g expr rule:


expr
    :   multExpr ( ops+=addOp | ops+=subOp )*
        -> expr(firstExpr={$multExpr.st}, ops={ops})
    ;


Modified Expr.g expr rule


expr
    :   multExpr ( ops+=addOp | ops+=subOp )*
        -> expr(firstExpr={$multExpr.st}, ops={$ops})
    ;


by adding the $ so that ops becomes $ops it gets treated as an array as it
should.

then since the template is getting an array now, need to add a separator to
the expr template in ByteCode.stg

Original expr template


expr(firstExpr, ops) ::= <<




Modified expr template

expr(firstExpr, ops) ::= <<

&lt;ops; separator=&quot;\n&quot;&gt;
>>

I didn't see a post on this previously.  This works for me.  Hope this helps
someone.

--
View this message in context: http://antlr.1301665.n2.nabble.com/1pass-cannot-add-subtract-more-than-two-consecutive-integers-tp6209416p6209416.html
Sent from the ANTLR mailing list archive at Nabble.com.


More information about the antlr-interest mailing list