初始化Lexer会在Antlr4中引发错误 [英] Initialising my Lexer throws an error in Antlr4

查看:609
本文介绍了初始化Lexer会在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. :(

我可以在终端中成功运行此[tutorial] [1].我现在的目标是使用[这里] [2]蚕食了Mainl的AntlrWorks2在Netbeans中运行相同的教程.

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的初始化中得到了一个"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/)

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.

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

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