[antlr-interest] problem with expression evaluation using AST

ankit.mehrotra at tcs.com ankit.mehrotra at tcs.com
Mon Dec 3 22:06:06 PST 2007


Hi,

while evaluating expressions in AST's ,  tried a very simple example ,


tree grammar Eval;
options{
tokenVocab = ASTExpr;
ASTLabelType= CommonTree;
}

@header{
import java.util.HashMap;
}

@members{
HashMap memory = new HashMap();
}

prog    :       stat+;

stat    :       expr {System.out.println($expr.value);}
        |       ^('=' ID expr)
                {memory.put($ID.text,new Integer($expr.value));}
        ;

expr returns [int value]
        :       ^('+' a=expr b=expr) {$value = a+b;}
        |       ^('-' a=expr b=expr) {$value = a-b;}
        |       ^('*' a=expr b=expr) {$value = a*b;}
        |       ID
                {
                Integer v = (Integer)memory.get($ID.text);
                if (v != null) $value=v.intValue();
                else System.err.println("Undefined Variable "+$ID.text);
                }
        |       INT     {$value=Integer.parseInt($INT.text);}
        ;

ID      :       ('a'..'z'|'A'..'Z')+;
INT     :       '0'..'9'+;

and when have compiled it for generating code, it is giving me this error

java org.antlr.Tool Eval.g
ANTLR Parser Generator  Version 3.0.1 (August 13, 2007)  1989-2007
error(10):  internal error: Eval.g : java.lang.NullPointerException
org.antlr.tool.GrammarSanity.traceStatesLookingForLeftRecursion(GrammarSanity.ja
va:105)
org.antlr.tool.GrammarSanity.checkAllRulesForLeftRecursion(GrammarSanity.java:37
)
org.antlr.tool.Grammar.checkAllRulesForLeftRecursion(Grammar.java:1388)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:268)
org.antlr.Tool.processGrammar(Tool.java:347)
org.antlr.Tool.process(Tool.java:268)
org.antlr.Tool.main(Tool.java:70)


Please Help

Ankit 
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071204/163e6ac2/attachment.html 


More information about the antlr-interest mailing list