初始化我的词法分析器会在 Antlr4 中引发错误 [英] Initialising my Lexer throws an error in Antlr4

查看:35
本文介绍了初始化我的词法分析器会在 Antlr4 中引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

团队,
我是 Antlr 的新手,我花了 4 天时间尝试学习、安装、运行教程并与我的 IDE 集成.:(

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

我可以在终端中成功运行这个 [教程][1].我现在的目标是使用 AntlrWorks2 在 Netbeans 中运行相同的教程,我已经从 [Here][2] 蚕食了 Main.

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].

代码可以编译,但是当我运行时,我从 Lexer 的 init 中得到一个java.lang.ExceptionInInitializerError".

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 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';

主要内容:

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

}

错误:

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)


答案: antlr4:ATN 版本 2 预期 3

推荐答案

听起来可能是版本问题.ANTLR 生成具有特殊格式的序列化 ATN(增强转换网络),该格式可以在版本之间更改,例如 4.0 到 4.1.您从命令行生成的加载源代码可能是一个版本,而 NetBeans 中的最新 AW2 正在尝试使用不同版本读取它.

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.

这篇关于初始化我的词法分析器会在 Antlr4 中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆