最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】ANTLRWorks 1.5编译代码出错:Compiler failed with result code 1

ANTLR crifan 3051浏览 0评论

【问题】

折腾:

【记录】尝试使用ANTLRWorks 1.5开发

的过程中,使用代码:

grammar Expr;

options {
	output = AST;
	ASTLabelType = CommonTree; // type of $stat.tree ref etc...
}

INT	:	'0'..'9'+ ;
NEWLINE	:	'\r'? '\n' ;
WS	:	(' '|'\t')+ {skip();} ;

prog	:	stat+ ;

stat	:	expr NEWLINE	-> expr;
	
expr	:	multExpr (('+'^|'-'^) multExpr)*;

multExpr:	atom ('*'^ atom)* ;

atom	:	INT |	'('! expr ')'!;

 

然后用ANTLRWorks 1.5去debug,结果出错:

error Compiler failed with result code 1

【解决过程】

1.点击Show Console后,可以看到具体错误:

[14:35:57] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\ExprSimple\output\ExprSimpleParser.java:104: <identifier> expected

[14:35:57] public final ExprSimpleParser.prog_return prog() throws {

[14:35:57] ^

[14:35:57] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\ExprSimple\output\ExprSimpleParser.java:210: <identifier> expected

[14:35:57] public final ExprSimpleParser.stat_return stat() throws {

[14:35:57] ^

[14:35:57] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\ExprSimple\output\ExprSimpleParser.java:302: <identifier> expected

[14:35:57] public final ExprSimpleParser.expr_return expr() throws {

[14:35:57] ^

[14:35:57] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\ExprSimple\output\ExprSimpleParser.java:464: <identifier> expected

[14:35:57] public final ExprSimpleParser.multExpr_return multExpr() throws {

[14:35:57] ^

[14:35:57] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\ExprSimple\output\ExprSimpleParser.java:577: <identifier> expected

[14:35:57] public final ExprSimpleParser.atom_return atom() throws {

[14:35:57] ^

[14:35:57] 5 errors

 

public final ExprSimpleParser.prog_return prog  throws

 

2.然后就去看了看对应的代码:

很明显,对应的:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\ExprSimple\output\ExprParser.java

中对应的代码:

	// $ANTLR start "prog"
	// D:\\DevRoot\\IndustrialMobileAutomation\\HandheldDataSetter\\ANTLR\\projects\\v1.5\\ExprSimple\\Expr.g:12:1: prog : ( stat )+ ;
	public final ExprParser.prog_return prog() throws  {
		ExprParser.prog_return retval = new ExprParser.prog_return();
		retval.start = input.LT(1);

after throw missing exception

throw缺少了对应的exception,所以会出错。

3.感觉貌似是JAVA版本不匹配所导致的,

所以,打算把当前的x64 win7上已经安装的x64 的JDK6删掉,重新安装上JDK7:

jdk-7u11-windows-x64.exe

去看看效果。

然后再去更新对应的JAVA_HOME,以及PATH和CLASSPATH

JAVA_HOME=C:\Program Files\Java\jdk1.7.0_11

CLASSPATH=.;%JAVA_HOME%\jre\lib\rt.jar;D:\DevTool\DD_Parser\Parser\ANTLR\antlr\antlr-4.0-complete.jar;

PATH中包含%JAVA_HOME%\bin;

然后再去cmd中验证是否生效了:

C:\Users\CLi>java -version

java version "1.7.0_11"

Java(TM) SE Runtime Environment (build 1.7.0_11-b21)

Java HotSpot(TM) 64-Bit Server VM (build 23.6-b04, mixed mode)

4.然后再重新运行

结果问题依旧。。。

5.后来参考:

http://www.antlr.org/wiki/display/ANTLR3/Five+minute+introduction+to+ANTLR+3

中的代码,去试试:

grammar SimpleCalc;
 
add : NUMBER PLUS NUMBER;
 
NUMBER  : ('0'..'9')+ ;
 
PLUS    : '+';

结果编译还是同样的错误:

[17:42:44] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\SimpleCalc\output\SimpleCalcParser.java:73: error: <identifier> expected

[17:42:44]     public final void add() throws  {

[17:42:44]                                   ^

[17:42:44] 1 error

[17:43:11] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\SimpleCalc\output\SimpleCalcParser.java:73: error: <identifier> expected

[17:43:11]     public final void add() throws  {

[17:43:11]                                   ^

[17:43:11] 1 error

6.即使手动去用javac编译,也还是出错:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\SimpleCalc\output>ls

SimpleCalc.tokens      SimpleCalcLexer.java   SimpleCalcParser.java  __Test__.java          classes

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\SimpleCalc\output>javac Simp*.java

SimpleCalcParser.java:39: error: <identifier> expected

        public final void add() throws  {

                                      ^

1 error

很明显,的确都是语法错误,无法正常编译。

7.最后,经过一番折腾。

发现了一个事情:

昨天antlr的官网貌似在调整,导致很多页面,比如:

http://www.antlr3.org/download/

打不开,导致无法下载东西,无法查看很多页面。

现在已经恢复可用了。

所以去下载了其他几个版本的antlrworks:

antlrworks-1.4.3.jar

然后发现是可以正常使用,不会出错的。

 

【总结】

目前的结果是:

当前环境:

  • x64 win7
  • x64 JDK 7

使用

antlrworks-1.5.jar

会出现错误:

Compiler failed with result code 1

[13:25:55] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\SimpleCalc\output\SimpleCalcParser.java:73: error: <identifier> expected

[13:25:55]     public final void add() throws  {

[13:25:55]                                   ^

[13:25:55] 1 error

其中,对应的信息是:

antlrworks 1.5 help about information

即:

  • antlrworks: 1.5
  • antlr: 3.5
  • StringTemplate v3: 3.2.1
  • StringTemplate v4: 4.0.7-SNAPSHOT

(JAVA为,自动检测出来的,当前系统所安装的JDK7)

 

而换用之前的版本,比如:

antlrworks-1.4.3.jar

结果就是可以正常运行的,不会出现上述错误:

antlrworks 1.4.3 can run debug ok

(也不会出现后面的另外一个错误:

【已解决】ANTLRWorks调试出错:Cannot launch the debuggerTab. Time-out waiting to connect to the remote parser

对应的信息是:

antlrworks 1.4.3 help about information

即:

  • antlrworks: 1.4.3
  • antlr: 3.4
  • StringTemplate: 3.2.1

(JAVA为,自动检测出来的,当前系统所安装的JDK7)

 

另外,也去试了试其他几个更低的版本,结果是,如下版本都是可以正常运行的:

 

所以,至少看起来是,最新的1.5和当前的系统环境不兼容。

但是问题错误根源,至今未知。

转载请注明:在路上 » 【已解决】ANTLRWorks 1.5编译代码出错:Compiler failed with result code 1

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.172 seconds, using 22.18MB memory