代码之家  ›  专栏  ›  技术社区  ›  WayneH

语法有什么问题

  •  0
  • WayneH  · 技术社区  · 14 年前

    我正在尝试获取ANTLR上的简单表达式求值器示例 版本开始工作。但是我的ActionScript版本得到了以下信息

       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]
    

    好吧,因为代码在SWC文件中,在调试器中看不到,所以我下载了 ActionScript源代码并尝试运行ant构建,但失败。

    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();} ;
    

    这是我的试验台:

    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();
    

    a=3
    a
    

    有人能看出我做错了什么吗?

    当然,我认为这个版本的ANTLR的ActionScript目标中有一个bug,但是我不喜欢其他人指责工具是他们的代码。所以首先我要问的是,我的代码是否有问题。

    1 回复  |  直到 14 年前
        1
  •  1
  •   WayneH    14 年前

    我认为问题在于新版本的antlr3.2.2和ANTLRWorks没有生成正确的代码。阅读我必须使用命令行工具的其他地方。结束这个问题。