質問

I am novice to Antlr and I am really lost at this point. Classic example for ANTLRWorks (my version is 1.5) seems not working.

Here, my main code in Expr.g:

grammar Expr;

@header {
package test;
import java.util.HashMap;
}

@lexer::header {package test;}

@members {
/** Map variable name to Integer object holding value */
HashMap memory = new HashMap();
}

prog:   stat+ ;

stat:   expr NEWLINE {System.out.println($expr.value);}
    |   ID '=' expr NEWLINE
        {memory.put($ID.text, new Integer($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 = Integer.parseInt($INT.text);}
    |   ID
        {
        Integer v = (Integer)memory.get($ID.text);
        if ( v!=null ) $value = v.intValue();
        else System.err.println("undefined variable "+$ID.text);
        }
    |   '(' e=expr ')' {$value = $e.value;}
    ;

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

And while trying to debug it, I'm having this errors in console:

[04:43:42] C:\Users\GreyZ0mbie\Documents\output\ExprParser.java:91: error: <identifier> expected
[04:43:42]  public final void prog() throws  {
[04:43:42]                                 ^
[04:43:42] C:\Users\GreyZ0mbie\Documents\output\ExprParser.java:171: error: <identifier> expected
[04:43:42]  public final void stat() throws  {
[04:43:42]                                 ^
[04:43:42] C:\Users\GreyZ0mbie\Documents\output\ExprParser.java:297: error: <identifier> expected
[04:43:42]  public final int expr() throws  {
[04:43:42]                                ^
[04:43:42] C:\Users\GreyZ0mbie\Documents\output\ExprParser.java:401: error: <identifier> expected
[04:43:42]  public final int multExpr() throws  {
[04:43:42]                                    ^
[04:43:42] C:\Users\GreyZ0mbie\Documents\output\ExprParser.java:488: error: <identifier> expected
[04:43:42]  public final int atom() throws  {
[04:43:42]                                ^
[04:43:42] 5 errors

I think, that problem might be in version of program or version of Java (last Java from site). Most unfortunate, I'm just starting to work with the program and simply can't solve this error.

役に立ちましたか?

解決

Please switch to AntlrWorks 1.4.3,

http://antlr3.org/download/

I met exactly the same errors as AntlrWorks 1.5 generates such broken Java files.

(Update: The above link no longer works. Instead you can download from Google Code)

  1. Use a web browser such as Google Chrome.
  2. Go to http://code.google.com/p/fast-el/source/browse/trunk/lib/antlrworks-1.4.3.jar?r=86
  3. Right click on View raw file link, and choose Save link as... to save this file as antlrworks-1.4.3.jar.

他のヒント

This is a bug in ANTLRWorks 1.5 which only affects the Debug feature of ANTLRWorks and has already been fixed for the next release.
#5: ANTLRworks fails to generate proper Java Code

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top