简体   繁体   English

初始化我的Lexer会在Antlr4中引发错误

[英]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. 我是Antlr的新手,我花了4天时间尝试学习,安装,运行教程并与我的IDE集成。 :( :(

I can run this [tutorial][1] in the Terminal successfully. 我可以在终端中成功运行这个[tutorial] [1]。 My goal now is to run the same tutorial in Netbeans with AntlrWorks2 I have cannibalised the Main from [Here][2]. 我现在的目标是使用AntlrWorks2在Netbeans中运行相同的教程我从[Here] [2]中蚕食了Main。

The code compiles, but when I run I get an "java.lang.ExceptionInInitializerError" from init of the Lexer. 代码编译,但是当我运行时,我从Lexer的init得到一个“java.lang.ExceptionInInitializerError”。

1: http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4 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/ ) 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 答案: antlr4:ATN版本2预计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. ANTLR生成序列化的ATN(扩充转换网络),其特殊格式可以在4.0到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. 从一个版本的命令行生成的加载源代码和NetBeans中的最新AW2可能会尝试使用不同的版本读取它。

"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." “您的解析器是使用ANTLR 4.0生成的,但您正尝试使用ANTLR 4.1执行它。最可能的原因是使用ANTLRWorks 2.0生成解析器,内部使用ANTLR 4.0。我正在发布ANTLRWorks 2.1这将纠正这种不匹配。“ - 280Z28 - 280Z28

Answer is Here 答案在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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