第 5 章 ANLTR常见问题及解答

目录

5.1. ANTLR的语法方面常见问题及解决办法
5.1.1. The following token definitions can never be matched because prior tokens match the same input
5.1.2. Decision can match input such as xxx using multiple alternatives
5.1.3. MismatchedSetException和mismatched input xxx expecting set yyy
5.1.4. syntax error: unterminated rule detected at xxx while looking for lexer rule element
5.1.5. NoViableAltException和no viable alternative at input xxx
5.1.6. Cannot generate the grammar because, duplicate token type xxx when collapsing subrule into set
5.1.7. rewrite syntax or operator with no output option xxx
5.1.8. UnwantedTokenException(found=xxx)
5.1.9. org.antlr.runtime.EarlyExitException
5.1.10. reference to rewrite element include without reference on left of ->
5.2. ANTLRWorks方面常见问题及解决办法
5.2.1. Compiler exception: java.io.IOException Cannot run program javac
5.2.2. Cannot launch the debuggerTab. Time-out waiting to connect to the remote parser
5.2.3. xxxParser.java error: <xxx> expected
5.2.4. Compiler failed with result code 1

ANTLR开发期间,常常会遇到各种错误,现在总结如下:

5.1. ANTLR的语法方面常见问题及解决办法

5.1.1. The following token definitions can never be matched because prior tokens match the same input

在写ANTLR的grammar时,经常会遇到:

The following token definitions can never be matched because prior tokens match the same input

对于这样的问题,之前遇到过很多。

后来,算是有点明白了,所以专门整理出来其原因和一般的解决思路:

【整理】关于antlr中出错"The following token definitions can never be matched because prior tokens match the same input"的原因和解决思路

另外,之前遇到过很多次,有需要的可以去参考:

5.1.2. Decision can match input such as xxx using multiple alternatives

在写ANTLR的grammar时,经常会遇到,多重匹配的问题:

一般的小问题,都可以自己慢慢修改代码,而搞定的。

比如:

【已解决】antrl代码编译出错:Decision can match input such as "’#include’" using multiple alternatives: 1, 2

其他的,也有之前不够熟悉antlr时,没有解决的问题:

【未解决】antlr语法警告:warning: Decision can match input such as "WS" using multiple alternatives: 1, 2

但是实际上,经过后来的折腾,早就解决了的。

但是,有时候,也会遇到比较难处理而始终搞不定的,比如:

【未解决】antlr中STRING出现多重匹配的问题:Decision can match input such as "STRING" using multiple alternatives: 1, 2

5.1.3. MismatchedSetException和mismatched input xxx expecting set yyy

写antlr语法期间,调试期间,遇到最多的,可能就属不匹配的问题了。

对于不匹配的问题,其原因有各种各样,而导致不匹配的。

目前已经遇到的,大致有这几类:

ANTLR v4中,由于一些语法的变化,导致语法检查更严格,有时候也会导致此类mismatch的问题:

【已解决】antlr v4的语法出错:line 1:0 mismatched input ‘a’ expecting A

5.1.4. syntax error: unterminated rule detected at xxx while looking for lexer rule element

详见:

【已解决】antlr v4测试Hello结果出错:syntax error: unterminated rule (missing ‘;’) detected at ‘c ‘ while looking for lexer rule element

5.1.6. Cannot generate the grammar because, duplicate token type xxx when collapsing subrule into set

如果ANTLR语法写的有问题的话,则是无法生成对应的代码的。

错误有多种可能:

比如是由于符号类型冲突而无法生成代码,详见:

【已解决】Antlr语法编译出错:Cannot generate the grammar because, duplicate token type COMPOSITE_OPERATOR when collapsing subrule into set

5.1.7. rewrite syntax or operator with no output option xxx

有时候,是在ANTLR的option中,添加了一些不支持的参数或选项,则导致此错误。

比如:

【已解决】antlr语法出错:rewrite syntax or operator with no output option; setting output=AST

5.1.8. UnwantedTokenException(found=xxx)

比如:

【已解决】antlr调试解析出错:UnwantedTokenException(found=xxx)

5.1.10. reference to rewrite element include without reference on left of ->

比如:

【已解决】antlr代码编译出错:reference to rewrite element include without reference on left of ->