解析器规则和词法分析器规则之间的替代 [英] Alternative between parser rule and lexer rule
问题描述
该问题埋在另一个问题的更新部分中,现在专门问这个问题.
The question is buried in the update section of another question, now specifically ask it.
我正在使用antlr3.4.
I am using antlr3.4.
我有一个简单的语法试图解析2种类型的文本,该行以"#include"开头,还有其他.这是我的语法:
I have a simple grammar trying to parse 2 types of text, the line started with "#include" and others. Here is my grammar:
cmds
: cmd+
;
cmd
: include_cmd | other_cmd
;
include_cmd
: INCLUDE DOUBLE_QUOTE FILE_NAME DOUBLE_QUOTE
;
other_cmd
: (~INCLUDE)+
;
INCLUDE
: '#include'
;
DOUBLE_QUOTE
: '"'
;
FILE_NAME
: ('a'..'z' | 'A'..'Z' | '0'..'9' | '_')+
;
New_Line
: ('\r' | '\n')+
;
WS
: ('\t' | ' ')+ {$channel = HIDDEN;}
;
但是我得到这样的警告:
But I get such warning:
Decision can match input such as "{DOUBLE_QUOTE..FILE_NAME, New_Line..WS}" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
我想这是因为双引号可以同时匹配other_cmd规则和DOUBLE_QUOTE规则,但是我想知道在这里,一个是解析器规则,另一个是lexer规则,这个警告有意义吗?
I guess this is because a double quote can match both other_cmd rule and DOUBLE_QUOTE rule, but I am wondering here, one is parser rule and the other is lexer rule, does this warning make sense?
是否有帮助清除此警告?
Any help to clear this warning?
一个附带的问题-警告消息只说替代项1,2,但是我现在还不清楚我是1,什么是2,是否有一种方法可以使antlr提供更多直接替代项?
A side question - the warning message just says alternative 1,2, but it is not immediately clear to me what is 1 and what is 2, is there a way to render antlr to give more direct alternatives?
推荐答案
我想这是因为双引号可以同时匹配other_cmd规则和DOUBLE_QUOTE规则,...
I guess this is because a double quote can match both other_cmd rule and DOUBLE_QUOTE rule, ...
否,这不是问题,因为include_cmd
以other_cmd
无法匹配的开头.
No, that is not the issue, since include_cmd
starts with something that other_cmd
cannot match.
决策可以使用多种选择来匹配输入,例如"{DOUBLE_QUOTE..FILE_NAME,New_Line..WS}":1,2
Decision can match input such as "{DOUBLE_QUOTE..FILE_NAME, New_Line..WS}" using multiple alternatives: 1, 2
该警告表示解析器可以通过多种方式匹配诸如foo"
(后跟DOUBLE_QUOTE
的输入)的:
The warning means that input like foo"
(a FILE_NAME
followed by a DOUBLE_QUOTE
) can be matched by the parser in more than one way:
ANTLR将选择贪婪解析,但是由于可能出现贪婪,因此会生成警告.如果您明确告诉解析器贪婪地进行匹配,则不会再发出警告:
ANTLR will choose the greedy parse, but since an ungreedy is possible, a warning is generated. If you explicitly tell the parser to match greedily, the warning would not be issued anymore:
other_cmd
: (options {greedy=true;} : ~INCLUDE)+
;
一个附带的问题-警告消息只说替代项1,2,但是我现在还不清楚我是1,什么是2,是否有一种方法可以使antlr提供更多直接替代项?
A side question - the warning message just says alternative 1,2, but it is not immediately clear to me what is 1 and what is 2, is there a way to render antlr to give more direct alternatives?
不,据我所知.这个警告确实是个神秘的事物.替代方法通常表示解析器可以遵循的分支:
No, not as far as I know. This warning is indeed rather cryptic. Alternatives usually denote the branches the parser can follow:
parser_rule
: alternative_1
| alternative_2
| alternative_3
;
但是在您的情况下,ANTLR似乎在谈论令牌范围是替代方案:DOUBLE_QUOTE..FILE_NAME
是替代方案,New_Line..WS
是第二替代方案.
But in your case, it seems ANTLR is talking about token ranges being the alternatives: DOUBLE_QUOTE..FILE_NAME
being an alternative and New_Line..WS
being the 2nd.
这篇关于解析器规则和词法分析器规则之间的替代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!