Initialising my Lexer throws an error in Antlr4

醉酒当歌 提交于 2019-12-12 05:30:06

问题


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


回答1:


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.




回答2:


"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



来源:https://stackoverflow.com/questions/19527137/initialising-my-lexer-throws-an-error-in-antlr4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!