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

【已解决】antlr解析字符串STRING出错:no viable alternative at input,对应的错误是NoViableAltException(0@[null])

ANTLR crifan 20109浏览 0评论

【问题】

用antlrworks-1.5rc2.jar,去调试如下antlr v3的语法:

fragment
STRING
    :  '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
    ;
 
     
 
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
    ;
     
//DECIMAL_VALUE :   DIGIT*;
DECIMAL_VALUE   :   DIGIT+;
//DECIMAL_VALUE :   '1'..'9' DIGIT*;
 
//HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;
//HEX_DIGIT : (DIGIT|'a'..'f'|'A'..'F') ;
HEX_DIGIT : ('a'..'f'|'A'..'F' | DIGIT) ;
 
//fragment
ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;
 
define_value    :   ID;
imported_value  :   '[' ID ']'; /* normal ID with [] */
define_imported_value
        :   define_value | imported_value;  
direct_value    :   (DECIMAL_VALUE | HEX_VALUE);
string_value    :   (STRING | define_imported_value);
 
triplet_description
        :   string_value;
triplet_help    :   string_value;
triplet     :   '{' triplet_interger ',' triplet_description ',' triplet_help '}' ; 
enumerate_line  :   (define_value | triplet);
type_struct :   '{' enumerate_line (',' enumerate_line)* '}';
type        :   'TYPE' single_type_value '(' DECIMAL_VALUE ')' type_struct? ';' ;
 
variable_field  :   class_line | label | type | help | constant_unit | handling;
variable    :   'REDEFINE'? 'VARIABLE' ID '{' variable_field+ '}';

去匹配如下内容:

TYPE            ENUMERATED (4)
{
    {0x1E6D11, "|en|Softing", [mfr_id_help]}
}
CONSTANT_UNIT   [blank] ;

结果调试期间,出错:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:29 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:30 no viable alternative at character '|'

对应的异常是:

NoViableAltException(0@[null])

NoViableAltException(0@[null]) no viable alternative at input

【解决过程】

1.很明显,其中是,对于字符串:

"|en|Softing"

去解析的时候,使用定义:

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

然后在STRING的位置,出错了,无法解析对应的

"|en|Softing"

中的第一个双引号和竖线,因此才报错:

no viable alternative at input '"'

no viable alternative at character '|'

的。

但是,问题很奇怪的是,此处,已经通过STRING,去定义了对应的字符串了啊。

而:

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

本身的含义,也很明确了,就是:

以双引号开头,其中可能包含0或多个,

转移序列,或者是,除了反斜杠和双引号,之外的,任何字符

再以双引号结尾

而此处的:

"|en|Softing"

完全符合要求的。

即,以双引号开头和结束,中间只是字母和竖杠,完全包含在前面的STRING的定义范围内。

完全符合要求。

但是不知道,此处为何会说无法找到双引号和竖杠。

2.参考了:

All ANTLR grammars produce error “no viable alternative at input '<EOF>'”

没帮助。

3.这里:

ANTLR no viable alternative at input '/'

提到了:

  • antlr的第三方的工具,比如Eclipse的antlr插件,经常会出问题的;

  • antlrworks本身,有时候也是出问题的;

  • 一般调试之前,最好都试试命令行去执行,看看是否可行。

但是,我此处就是由于之前命令行无法正常使用antlr,所以才用antlrworks的。。。。

4.参考:

no viable alternative at input

感觉还是自己的语法写的有问题。

但是却还是不知道如何改正确。

5.参考:

ANTLR 4 : Bad grammar and 'no viable alternative at input'

感觉和我的问题很像。

但是还是不懂。

6.随便把之前的ID加上fragment:

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

试试,结果问题依旧,而且其他地方,错的更多了:

D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 45:28 mismatched character ',' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 46:7 no viable alternative at input 'DEVICE_TYPE'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 46:34 mismatched character ',' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 47:7 no viable alternative at input 'DEVICE_REVISION'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 47:44 mismatched character ',' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 48:7 no viable alternative at input 'DD_REVISION'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 48:43 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 49:2 no viable alternative at input '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 53:43 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 54:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 55:47 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 55:49 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 56:46 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 56:48 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:29 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:30 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:33 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:41 mismatched character '"' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:56 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 60:57 mismatched input '}' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 62:14 missing ';' at 'CONSTANT_UNIT'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 62:36 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 63:22 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 62:38 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 66:38 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 67:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 68:45 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 68:47 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 69:44 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 69:46 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:29 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:30 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:33 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:37 mismatched character '-' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:43 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:57 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 73:58 mismatched input '}' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 75:14 missing ';' at 'CONSTANT_UNIT'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 75:36 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 76:22 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 75:38 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 78:41 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 79:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 80:45 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 80:47 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 81:44 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 81:46 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 85:31 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 86:14 no viable alternative at input '}'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 87:14 missing ';' at 'CONSTANT_UNIT'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 87:36 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 88:22 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 87:38 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 91:44 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 92:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 93:48 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 93:50 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 94:47 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 94:49 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 98:31 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 99:14 no viable alternative at input '}'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 100:14 missing ';' at 'CONSTANT_UNIT'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 100:36 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 101:22 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 100:38 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 104:40 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 105:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 106:44 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 106:46 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 107:43 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 107:45 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 111:38 mismatched character ',' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 112:41 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 112:42 mismatched input ',' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 112:71 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 112:73 mismatched input '}' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 113:46 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 113:47 mismatched input ',' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 113:76 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 113:78 mismatched input '}' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 114:46 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 114:47 mismatched input ',' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 114:76 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 114:78 mismatched input '}' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 115:47 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 115:48 mismatched input ',' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 115:77 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 115:79 mismatched input '}' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 116:52 mismatched character ',' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 116:82 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 116:83 no viable alternative at input '}'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 118:22 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 118:30 missing ';' at 'READ'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 122:42 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 123:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 124:51 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 124:53 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 125:50 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 125:52 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 128:32 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 128:33 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 128:36 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 128:39 no viable alternative at character '3'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 128:50 mismatched character '"' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 129:24 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 131:44 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 132:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 133:50 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 133:52 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 134:49 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 134:51 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 137:32 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 137:33 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 137:36 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 137:39 no viable alternative at character '3'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 137:50 mismatched character '"' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 138:24 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 140:43 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 141:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 142:49 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 142:51 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 143:48 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 143:50 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 146:32 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 146:33 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 146:36 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 146:39 no viable alternative at character '3'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 146:50 mismatched character '"' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 147:24 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 149:42 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 150:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 151:48 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 151:50 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 152:47 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 152:49 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 155:32 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 155:33 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 155:36 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 155:39 no viable alternative at character '3'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 155:50 mismatched character '"' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 156:24 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 158:42 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 159:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 160:48 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 160:50 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 161:47 mismatched character ']' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 161:49 mismatched input ';' expecting ID          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 164:32 no viable alternative at input '"'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 164:33 no viable alternative at character '|'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 164:36 mismatched character '|' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 164:39 no viable alternative at character '3'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 164:50 mismatched character '"' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 165:24 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 177:31 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 178:10 missing ID at '{'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 179:50 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 179:51 no viable alternative at input ';'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 180:49 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 180:50 no viable alternative at input ';'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 183:16 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 183:34 mismatched character '\r' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 184:10 mismatched input '{' expecting '}'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 185:55 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 186:54 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 189:16 mismatched character ' ' expecting '.'          
D:\DevRoot\IndustrialMobileAutomation\HandheldDataSetter\ANTLR\projects\v1.5\DDParserDemo\DDDemoFile.ddl line 189:37 mismatched character '\r' expecting '.'

7.所以,现在,还是对于之前,感觉像是搞清楚的fragment:

【整理】antlr语法中的fragment

还是没有完全搞懂。

估计是如果彻底搞懂了此fragment,然后就懂了,如何解决此处问题了。

8.再去参考了:

What does “fragment” means in ANTLR?

9.回来,再去试试,把之前的ESC_SEQ加上fragment,变成:

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

试试,结果问题依旧。

10.再去调换一下顺序:

//string_value  :   (STRING | define_imported_value);
string_value    :   (define_imported_value | STRING);

试试,结果问题依旧。

11.参考:

ANTLR common pitfalls

好像,对于是否加fragment,有何区别,有了更深入的了解:

对于输入:

1234

用如下语法的话:

没有加fragment:

grammar number;  
   
number :    INT;  
DIGIT   :   '0'..'9';  
INT :   DIGIT+;

此时,1,2,3,4,被识别为4个单独的,DIGIT。

对应的,DIGIT,也是会被识别为一个token(类似于,变量)

而如果加了fragment,变成:

grammar number;  
   
number :    INT;  
fragment DIGIT   :   '0'..'9';  
INT :   DIGIT+;

那么,由于fragment,就相当于inline,所以本身就相当于:

grammar number;  
   
number :    INT;  
INT :   ('0'..'9')+;

所以,最终,输入1234,会被识别为,一个整体的,“1234”,而不是四个DIGIT,1,2,3,4.

此时,由于DIGIT被“替换”掉了,所以,不会被识别为一个token。

对此,再去回去看看自己的代码,就明白了:

应该写成:

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

这样才表示,符合要求的,连续的字符,组合成为一个整体,表示一个ID。

而不是,多个字符,隔开的形式。

另外,又去学习了:

grammar number;  
   
DIGIT:  '0'..'9';  
INT :   DIGIT;

得知了,此处的INT,就是个多余的东西。

因为对于同样的匹配,即匹配单个数字,之前的DIGIT,已经做了,所以后面的INT,虽然实际上就是等价于DIGIT,但是永远不会被执行到。

又继续学习:

grammar number;  
//number    :   INT | FLOAT;  
DIGIT    :  '0'..'9';  
INT :   DIGIT+;  
FLOAT   :   DIGIT* '.' DIGIT+;

得知,此处是个lexer和parser混合的语法。

此处没有parser rule。

但是,还是始终没有搞懂,什么是lexer,什么是parser。

所以,越加需要去搞懂这两个的区别了。

详见:

【整理】什么是lexer,什么是parser,lexer和parser之间的关系

【总结】

还是没有完全搞懂此处为何出现:

no viable alternative at input

的错误,但是感觉是,等以后真正懂了:

什么是lexer和parser

fragment的真正含义

之后也许就可以自然而然的搞懂当前为何出错的了。

转载请注明:在路上 » 【已解决】antlr解析字符串STRING出错:no viable alternative at input,对应的错误是NoViableAltException(0@[null])

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 不错,我最近也在用ANTLR 4写个Fortran的解析器,也遇到了很多问题。
    董理11年前 (2013-07-05)回复
85 queries in 0.168 seconds, using 22.23MB memory