[antlr-interest] help related to interpreted grammar for calculator

Jim Idle jimi at temporal-wave.com
Mon Aug 18 15:25:42 PDT 2008


On Mon, 2008-08-18 at 18:10 -0400, mark twain wrote:
> Jim,
> Sorry to bug you again but still I am getting the following error
> messages......
> *****************************************************************************************
> ANTLR Parser Generator  Version 3.1b2 (July 17, 2008)  1989-2008
> error(148): bbcalcInterpreter.g:16:13: rule parameters may not have
> init values: v
> error(122): bbcalcInterpreter.g:17:33: label v conflicts with rule
> assignment's return value or parameter with same name
> error(146): bbcalcInterpreter.g:18:12: invalid StringTemplate %
> shorthand syntax: '%d'
> ***********************************************************************************************


OK - well rty and interpret what the erros are telling you:

I did not spot that you were trying to pre-initialize the input
parameter with a defautl value. This isn't valid in ANTLR. However I
suspect now that you didn't mean to have that there at all and meant
this to be a local variable with a default value (which you do not in
fact use). SO, you jsut want:

assignment
  :  <etc>

The ANTLR error was telling you that you had a local parameter called v
and also a label for a rule that you had called v hence it was
ambiguous/conflicting.

The StringTemplate reference is because you must escape the % in any
ANTLR grammar or the v2 parser thinks that it is somethig nto do with
string templates, so you need:

printf("My value:\%d\n", $v.val);


Jim

>         >                    : ^(EQUAL IDENTIFIER v=expr)
>         >                    { printf("My value:%d\n", $v.val); }
>         >                 ;
>         >         
>         >         
>         >         expr returns [int val =0]
>         >                 : z = operand   { $val = $z.val; }
>         >                   |  ^(PLUS x=expr y=expr) { $val = $x.val +
>         >         $y.val; }
>         >                   |  ^(MINUS x=expr y=expr) { $val = $x.val
>         >         - $y.val; }
>         >                   |  ^(MULT x=expr y=expr) { $val = $x.val *
>         >         $y.val; }
>         >                   |  ^(DIV x=expr y=expr) { $val = $x.val /
>         >         $y.val; }
>         >                 ;
>         >         
>         >         operand returns [int val =0]
>         >         : n = NUMBER { $val = atoi($n.text->chars); }
>         >                 ;
>         >         
>         >         
>         >         
>         >         List:
>         >         http://www.antlr.org/mailman/listinfo/antlr-interest
>         >         Unsubscribe:
>         >         http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>         >         
>         >         
>         > 
>         > 
>         
>         
>         
>         List:
>         http://www.antlr.org/mailman/listinfo/antlr-interest
>         Unsubscribe:
>         http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>         
>         
> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080818/1c2c51d4/attachment.html 


More information about the antlr-interest mailing list