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

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

ANTLR crifan 2385浏览 0评论

【问题】

对于antlr v3的代码:

startParse	:	(includes | id_info | variable | collection)+;
//startParse	:	(singleInclude | id_info | variable | collection)+;

includes	:	singleInclude+;
//includes	:	singleInclude;
singleInclude	:	'#include' STRING;

用antlrworks编译出错:

[16:13:05] warning(200): HartEddlParser.g:63:12:

Decision can match input such as "’#include’" using multiple alternatives: 1, 2

【解决过程】

1.很明显,就是说对于#include,有多种匹配的可能。

即有歧义。

2.后来自己看了半天,终于霸气改为:

//startParse	:	(includes | id_info | variable | collection)+;
startParse	:	(singleInclude | id_info | variable | collection)+;

//includes	:	singleInclude+;
//includes	:	singleInclude;
singleInclude	:	'#include' STRING;

而解决了此警告。

 

【总结】

如果出现:

Decision can match input such as xxx using multiple alternatives: 1, 2

的警告,那说明,自己的语法的确是写的有歧义。

即对于同样的输入,语法有多种匹配的可能。

此处,经过分析明白了此处的歧义:

对于原先的代码:

startParse	:	(includes | id_info | variable | collection)+;

includes	:	singleInclude+;
singleInclude	:	'#include' STRING;

对于两行include的测试内容:

#include "macros.ddl"

#include "bi_codes.h"

可以被识别为

startParse内的,多个的includes的形式,即startParse内的多个includes,每个includes是对应的一行include代码;

也可以被识别为

startParse内的,单个includes,而单个includes内,是singleInclude+,即多行include代码;

 

所以,结果就是:

对于同样的输入的测试文件,可以有多种匹配方法,如此,就产生了歧义。

所以才有警告,而把代码改为:

startParse	:	(singleInclude | id_info | variable | collection)+;

singleInclude	:	'#include' STRING;

后,同样的输入:

#include "macros.ddl"

#include "bi_codes.h"

则只有一种匹配的可能了:

只能是,startParse内的,单个的singleInclude,

通过startParse最外层的加号,表示可能有多个include的行。

如此,就一种匹配,就不会有错误警告了。

转载请注明:在路上 » 【已解决】antrl代码编译出错:Decision can match input such as "’#include’" using multiple alternatives: 1, 2

发表我的评论
取消评论

表情

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

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