如何摆脱我的ANTLR3语法中的以下多个替代警告? [英] How to get rid of the following multiple alternatives warnings in my ANTLR3 grammar?
问题描述
[11:45:19] warning(200): mygrammar.g:14:57: Decision can match input such as "','" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
[11:45:19] warning(200): C:\Users\Jarrod Roberson\mygrammar.g:14:57: Decision can match input such as "','" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
我希望能够将功能嵌套在其他功能中.
I want to be able to nest functions inside other functions.
myfunction(x) ->
sqr(a) -> a * a,
y -> sqr(x).
这是它抱怨的那一行
function : ID '(' args ')' '->' statement (',' statement)* ;
这就是它正在考虑的替代方案
and here is what it is considering the alternative
statement : ATOM
| expression
| assignment
| function
;
我正在使用.
作为语句的最终规则
I am using .
as my statement end rule
program : (statement'.')*;
这是ANTLRWorks中synatx图的样子
Here is what the synatx diagram looks like in ANTLRWorks
(来源: vertigrated.com )
(source: vertigrated.com)
我真的很喜欢在没有任何警告的情况下进行编译/工作.如何解决此警告条件?
I really like things to compile/work without any warnings. How do I resolve this warning condition?
推荐答案
Jarrod Roberson写道:
Jarrod Roberson wrote:
我真的很喜欢在没有任何警告的情况下进行编译/工作.如何解决此警告条件?
I really like things to compile/work without any warnings. How do I resolve this warning condition?
您的解析器可以解析以下输入:
Your parser can parse the following input:
f(x)-> g(y)-> y*y, x=y
在两个不同的分析树中:
in two different parse trees:
和:
您可以通过强制解析器向前看并确保在实际匹配这些规则之前确保前面有',' statement
来解决此问题.您可以通过在内部使用上述规则的句法谓词((...)=>
部分)来做到这一点:
You can fix this by forcing the parser to look ahead and make sure there is ',' statement
ahead before actually matching these rules. You can do that by using a syntactic predicate (the (...)=>
part) with said rule inside:
function
: ID '(' args ')' '->' statement ((',' statement)=> ',' statement)*
;
但是,如果您的function
规则具有未定义的某种结束"标记,则不需要谓词.根据您之前的问题和示例:
However, you don't need the predicate if your function
rule has some sort of an "end" token, which you haven't defined. From your earlier questions, and your example:
myfunction(x) ->
sqr(a) -> a * a,
y = sqr(x).
看来您正在使用'.'
作为function
的结尾.如果您将其添加到function
规则:
it seems you're using the '.'
as the end of a function
. If you add that to your function
rule:
function
: ID '(' args ')' '->' statement (',' statement)* '.'
;
您根本不需要谓词.
这篇关于如何摆脱我的ANTLR3语法中的以下多个替代警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!