views:

69

answers:

1

I am trying to get the simple expression evaluator example on the ANTLR website working within ActionScript, I have been able to get the java version to work. But my ActionScript version is getting the following error:

   TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at org.antlr.runtime::Lexer/nextToken()[/Users/gscott/antlr/code/antlr/main/runtime/ActionScript/project/src/org/antlr/runtime/Lexer.as:69]
    at org.antlr.runtime::CommonTokenStream/fillBuffer()[/Users/gscott/antlr/code/antlr/main/runtime/ActionScript/project/src/org/antlr/runtime/CommonTokenStream.as:84]
    at org.antlr.runtime::CommonTokenStream/LT()[/Users/gscott/antlr/code/antlr/main/runtime/ActionScript/project/src/org/antlr/runtime/CommonTokenStream.as:227]
    at org.antlr.runtime::CommonTokenStream/LA()[/Users/gscott/antlr/code/antlr/main/runtime/ActionScript/project/src/org/antlr/runtime/CommonTokenStream.as:289]
    at Eval_in_ASParser/prog()[C:\Users\Wayne-VII\Documents\Flex Builder 3\ANTLR_AIR_01\src\Eval_in_ASParser.as:61]
    at ANTLR_AIR_01/runProgram()[C:\Users\Wayne-VII\Documents\Flex Builder 3\ANTLR_AIR_01\src\ANTLR_AIR_01.mxml:11]
    at ANTLR_AIR_01/__bRunSource_click()[C:\Users\Wayne-VII\Documents\Flex Builder 3\ANTLR_AIR_01\src\ANTLR_AIR_01.mxml:20]

Well, since the code is in a SWC file and can not be seen in the debugger, I downloaded the ActionScript source and tried to run the ant build, which failed.

So, here is my ANTLR grammer:

grammar Eval_in_AS;

options {
    language=ActionScript;
}

@header {
//import java.util.HashMap;
import flash.utils.Dictionary;
}

@members {
/** Map variable name to Integer object holding value */
public var memory:Dictionary = new Dictionary();
public var output:String = new String();

public function getOutput():String
{
return output;
}
}

prog:   stat+ EOF;

stat:   expr NEWLINE {output +="\n" + $expr + ":" + $expr.value;}
    |   ID '=' expr NEWLINE
        {memory[$ID.text] = int($expr.value);}
    |   NEWLINE
    ;

expr returns [int value]
    :   e=multExpr {$value = $e.value;}
        (   '+' e=multExpr {$value += $e.value;}
        |   '-' e=multExpr {$value -= $e.value;}
        )*
    ;

multExpr returns [int value]
    :   e=atom {$value = $e.value;} ('*' e=atom {$value *= $e.value;})*
    ; 

atom returns [int value]
    :   INT {$value = int($INT.text);}
    |   ID
        {
        if ( memory.hasOwnProperty($ID.text)) {$value = memory[$ID.text];}
        else {output +="\nundefined variable:"+$ID.text+"\n";$value = 0;}
        }
    |   '(' expr ')' {$value = $expr.value;}
    ;

ID  :   ('a'..'z'|'A'..'Z')+ ;
INT :   '0'..'9'+ ;
NEWLINE:'\r'? '\n' ;
WS  :   (' '|'\t')+ {skip();} ;

And here is my test rig:

var lexer:Eval_in_ASLexer = new Eval_in_ASLexer(taSource.text as CharStream);
var tokens:CommonTokenStream = new CommonTokenStream(lexer);
var parser:Eval_in_ASParser = new Eval_in_ASParser(tokens);
parser.prog();
taOutput.text = parser.getOutput();

In the above code, taOutput is a displayed textArea. My input is:

a=3
a

Can anyone see what I am doing wrong?

Of course I think there is a bug in this version of ANTLR's ActionScript target, but I dislike how other people blame tools when it is their code. So first I am asking if there is something wrong in my coding.

A: 

I think the problem is with the new version of ANTLR 3.2.2 and ANTLRWorks not generating the correct code. Read other places where I have to use the command line tools. Closing this question.

WayneH