北京网站空间域名,电脑网站生成手机网站,网站优化工具,网站域名 空间 是每年都要缴费吗1. 意外的问题 在学习Antlr4的visitor模式时#xff0c;使用IDEA的Antlr插件完成了Hello.g4文件的编译#xff0c;指定的package为com.sunrise.hello 使用visitor模式遍历语法解析树#xff0c;遍历过程中打印hello语句 public class HelloVisitorImpl extends HelloBaseVi…1. 意外的问题 在学习Antlr4的visitor模式时使用IDEA的Antlr插件完成了Hello.g4文件的编译指定的package为com.sunrise.hello 使用visitor模式遍历语法解析树遍历过程中打印hello语句 public class HelloVisitorImpl extends HelloBaseVisitorString {Overridepublic String visitR(HelloParser.RContext ctx) {System.out.printf(hello %s\n, ctx.NAME().getText());return super.visitR(ctx);}// main方法使用visitor模式遍历语法解析树以打印hello语句public static void main(String[] args) {String input hello lucy\n hello wold\n by sunrise;// 词法解析CharStream stream CharStreams.fromString(input);HelloLexer lexer new HelloLexer(stream);CommonTokenStream tokens new CommonTokenStream(lexer);// 语法解析HelloParser parser new HelloParser(tokens);ParseTree parseTree parser.r();// 遍历语法解析树HelloVisitorImpl visitor new HelloVisitorImpl();visitor.visit(parseTree);}
}运行main()方法执行报错 ANTLR Tool version 4.11.1 used for code generation does not match the current runtime version 4.8Exception in thread main java.lang.ExceptionInInitializerErrorat com.sunrise.hello.visitor.HelloVisitorImpl.main(HelloVisitorImpl.java:26)
Caused by: java.lang.UnsupportedOperationException: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 4 (expected 3).at org.antlr.v4.runtime.atn.ATNDeserializer.deserialize(ATNDeserializer.java:187)at com.sunrise.hello.HelloLexer.clinit(HelloLexer.java:127)... 1 more
Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 4 (expected 3).... 3 more分析异常栈的栈顶错误信息代码是由4.11.1版本的Antrl Tool生成的运行时的时候antlr runtime是4.8版本的二者不匹配 查看IDEA Antlr插件的描述以及maven中配置的antlr-runtime后发现确实如此
2. 错误原因分析
从错误提示大致可以猜出Antlr Tool版本与antlr runtime应该保持一致若不了解Antlr Tool和antlr runtime的分工则无法理解为何要保持一致。回看Antlr4的官网发现有如下介绍 complete jar里面包含Antlr Tool和Java runtimeJava runtime jar负责编译、执行Java语言的parser/lexer 总结起来Antlr Tool负责将.g4文件编译为指定语言的代码例如Java语言。Java语言的parser或lexer的编译、执行则需要依靠antlr runtime4.x版本的Antlr Tool生成的Java语言的parser和lexer需要使用4.x的org.antlr:antlr4-runtime
3. 解决办法
要么使用4.8版本的Antlr Tool重新编译.g4文件要么将org.antlr:antlr4-runtime的版本更新为4.11.1为了方便这里选择更新org.antlr:antlr4-runtime的版本
4. 更离奇的错误 现在使用4.11.1版本完成了.g4文件的编译、parser/lexer的Java代码编译 突发奇想使用grun命令测试语法规则报错如下 $ grun com.sunrise.hello.Hello r -gui
ANTLR Tool version 4.11.1 used for code generation does not match the current runtime version 4.8ANTLR Runtime version 4.11.1 used for parser compilation does not match the current runtime version 4.8Exception in thread main java.lang.ExceptionInInitializerErrorat sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at org.antlr.v4.gui.TestRig.process(TestRig.java:144)at org.antlr.v4.gui.TestRig.main(TestRig.java:119)
Caused by: java.lang.UnsupportedOperationException: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 4 (expected 3).at org.antlr.v4.runtime.atn.ATNDeserializer.deserialize(ATNDeserializer.java:187)at com.sunrise.hello.HelloLexer.clinit(HelloLexer.java:127)... 6 more
Caused by: java.io.InvalidClassException: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 4 (expected 3).... 8 more又是版本不一致导致的 .g4文件的编译、parser的Java代码编译都是使用4.11.1版本但是却使用4.8的antlr runtime运行字节码 总结 .g4文件的编译、parser/lexer的代码编译、parser/lexer字节码的运行需要保持版本一致 这也是为什么开源组件喜欢使用maven plugin实现.g4文件的编译并使用maven property保证maven plugin和antlr-runtime的版本一致的原因