简体   繁体   中英

Initialising my Lexer throws an error in Antlr4

Hi Team,
I'm new to Antlr and I have spent 4 days trying to learn, install, run tutorials and integrate with my IDE. :(

I can run this [tutorial][1] in the Terminal successfully. My goal now is to run the same tutorial in Netbeans with AntlrWorks2 I have cannibalised the Main from [Here][2].

The code compiles, but when I run I get an "java.lang.ExceptionInInitializerError" from init of the Lexer.

1: http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4
2: http://www.certpal.com/blogs/2011/01/antlr-tutorial-hello-antlr/ )

Grammar:

grammar Split;

@header {
    package PlayGround.AutoGen;    
}

hi      :   HELLO ID ;         // match keyword hello followed by an identifier
ID      :   [a-z]+ | [A-Z]+;     // match lower-case identifiers
WS      :   [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines
HELLO   :   '[H|h]ello';

Main:

public class MyMain {

public static void main(String args[]) {
    new MyMain().MyAttempt();
}

public void MyAttempt() {
    try {
        String string = "Hello World";
        CharStream charStream = new ANTLRInputStream(string);
/*Line 28*/ SplitLexer lex = new SplitLexer(charStream);  /*Line 28*/
        org.antlr.v4.runtime.CommonTokenStream tokens;
        tokens = new org.antlr.v4.runtime.CommonTokenStream(lex);
        SplitParser parser = new SplitParser(tokens);
        SplitParser.HiContext split = parser.hi();
        String toString = split.toString();
        System.out.println(toString);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Error:

run:
Exception in thread "main" java.lang.ExceptionInInitializerError
    at PlayGround.MyMain.MyAttempt(MyMain.java:28)
    at PlayGround.MyMain.main(MyMain.java:21)
Caused by: java.lang.UnsupportedOperationException: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 2 (expected 3).
    at org.antlr.v4.runtime.atn.ATNSimulator.deserialize(ATNSimulator.java:132)
    at PlayGround.AutoGen.SplitLexer.<clinit>(SplitLexer.java:78)
    ... 2 more
Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 2 (expected 3).
    ... 4 more
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

ANSWER: antlr4: ATN version 2 expected 3

It sounds like there might be a version issue. ANTLR generates serialized ATN (augmented transition networks) that have a special format that can change from version to version like 4.0 to 4.1. it's possible that your loading source code generated from the command line in one version and the latest AW2 in NetBeans is trying to read it with a different version.

"Your parser was generated with ANTLR 4.0, but you are trying to execute it with ANTLR 4.1. The most likely cause of this is using ANTLRWorks 2.0 to generate the parser, which internally uses ANTLR 4.0. I'm in the process of releasing ANTLRWorks 2.1 which will correct this mismatch." - 280Z28

Answer is Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM