【已解决】用antlrworks-1.2.2.jar编译代码出错:error: cannot find symbol,g.NEWLINE();,symbol: method NEWLINE()

【问题】

在折腾:

【已解决】antlr调试时,antlr的语法是对的,但是却识别继续识别输入的内容

的过程中,换用antlrworks-1.2.2.jar去编译同样的代码:

grammar DDParserDemo;

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

//NEWLINE :   '\r'? '\n' ;
//NEWLINE :   '\r' '\n' ;
NEWLINE :   '\r' '\n' ;


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

FLOAT
    :   ('0'..'9')+ '.' ('0'..'9')* EXPONENT?
    |   '.' ('0'..'9')+ EXPONENT?
    |   ('0'..'9')+ EXPONENT
    ;

COMMENT
    :   '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    |   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {skip();}
    ;

STRING
    :  '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
    ;

CHAR:  '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
    ;

fragment
EXPONENT : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;

fragment
DIGIT
	:	'0'..'9';

//FAKE_TOKEN 	:	'1' '2' '3';

/*
DECIMAL_VALUE
	:	'1'..'9' DIGIT*;
*/

DECIMAL_VALUE
	:	DIGIT*;

HEX_VALUE
	:	'0x' HEX_DIGIT+;
/*	
startParse	:	(identification)+;
*/
startParse	:	(identification)+;

identification	:	definiton WS* ','? WS*   -> definiton
		;
	
definiton	:	(ID)^ ('\t'!|' '!)+ (DECIMAL_VALUE | HEX_VALUE)
		;

结果出错了:

[13:13:31] warning(200): DDParserDemo.g:87:28: Decision can match input such as "WS" using multiple alternatives: 1, 2

As a result, alternative(s) 2 were disabled for that input

[13:13:31] warning(200): D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDParserDemo.g:87:28: Decision can match input such as "WS" using multiple alternatives: 1, 2

As a result, alternative(s) 2 were disabled for that input

[13:13:37] D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\output\__Test__.java:14: error: cannot find symbol

[13:13:37] g.NEWLINE();

[13:13:37] ^

[13:13:37] symbol: method NEWLINE()

[13:13:37] location: variable g of type DDParserDemoParser

[13:13:37] 1 error

 

注:

1.同样的代码,在antlrworks-1.5rc2.jar中,却是可以正常编译执行调试的。

2. 关于各个版本的antlrworks所含的antlr等的版本,详见:

【总结】使用ANTLR和ANTLRWorks的开发心得和注意事项

 

【解决过程】

1.参考:

[antlr-interest] Simple Grammar breaks ANTLRWorks Interpreter & Debugger?

去给NEWLINE添加上fragment,变成:

//NEWLINE :   '\r'? '\n' ;
//NEWLINE :   '\r' '\n' ;
fragment NEWLINE :   '\r' '\n' ;

看看结果。结果果然就是可以正常去编译,然后可以去调试了:

can debug after add fragment for newline

 

 

【总结】

貌似,对于,无法再分解的内容,应该加上fragment,才能否正常在,旧的antlr,即antlrworks-1.2.2.jar中通过编译(然后去调试的)

注:

1. 此处的antlrworks-1.2.2.jar包含的是:

  • antlrworks-1.2.2.jar
    • ANTLRWorks: 1.2.2
    • ANTLR: 3.1.1
    • StringTemplate: 3.2
    • XJLibrary: 2.0

2.即使不加fragment,在新的antlr:

  • antlrworks-1.5rc2.jar
    • ANTLRWorks:1.5
    • ANTLR:3.5-rc2
    • StringTemplate v3:3.2.1
    • StringTemplate v4:4.0.7-rc2

中,也是可以正常编译,然后可以去调试的。

3.关于fragment,详见:

【整理】antlr语法中的fragment



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量