ANTLR ClassCastException

时间:2011-08-19 13:25:14

标签: java antlr

为什么在ANTLR中生成代码时会出现此错误?

[16:06:38] error(10):  internal error: C:\Users\user\Desktop\test.g : java.lang.ClassCastException: org.antlr.runtime.tree.CommonTree cannot be cast to org.antlr.tool.GrammarAST
org.antlr.grammar.v3.CodeGenTreeWalker.rules(CodeGenTreeWalker.java:1467)
org.antlr.grammar.v3.CodeGenTreeWalker.grammarSpec(CodeGenTreeWalker.java:1441)
org.antlr.grammar.v3.CodeGenTreeWalker.grammar_(CodeGenTreeWalker.java:509)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:421)
org.antlr.Tool.generateRecognizer(Tool.java:655)
org.antlr.Tool.process(Tool.java:468)
org.antlr.works.generate.CodeGenerate.generate(CodeGenerate.java:104)
org.antlr.works.generate.CodeGenerate.run(CodeGenerate.java:185)
java.lang.Thread.run(Unknown Source)

语法非常简单,仅用于测试:

grammar test;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

有什么想法吗?

1 个答案:

答案 0 :(得分:16)

您指定了混合语法(词法分析器和解析器),但未指定解析器规则。

添加一个或使用lexer grammar test;