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

Jim Idle jimi at temporal-wave.com
Mon Aug 18 14:39:54 PDT 2008


On Mon, 2008-08-18 at 17:05 -0400, mark twain wrote:
> 
> Sorry!! for the mistake..
> I have attached all 3 grammar files once again... Please look into the
> Interpreter grammar file and let me know my mistake....


You are not using references properly. You need to use $xxxx and then
the return element reference. PLease cosinder spending time reading
through the C examples and the Wiki stuff. If you can afford to then
buyig the book woudl be a good resource:

tree grammar bbcalcInterpreter;


options
{
     tokenVocab= bbcalc;
     ASTLabelType = pANTLR3_BASE_TREE;
     language = C;
}

@header {
#include <stdio.h>
}

assignment  { int v = 0; }
           : ^(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); }
        ;

> 
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080818/0a466563/attachment.html 


More information about the antlr-interest mailing list