如何消除 ANTLR3 语法中的以下多个替代警告?

发布于 2024-12-15 17:47:49 字数 1197 浏览 2 评论 0原文

[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

我希望能够将函数嵌套在其他函数中。

myfunction(x) ->
  sqr(a) -> a * a,
  y -> sqr(x).

这是它抱怨的行

function : ID '(' args ')' '->' statement (',' statement)* ;

,这是它正在考虑的替代方案,

statement : ATOM
          | expression
          | assignment
          | function
          ;

我使用 . 作为我的语句结束规则

program : (statement'.')*;

这是 ANTLRWorks 中的 synatx 图的样子

语法图表
(来源:vertigerated.com

我真的很喜欢东西编译/工作时没有任何警告。如何解决此警告情况?

[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).

here is the line it is complaining about

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'.')*;

Here is what the synatx diagram looks like in ANTLRWorks

syntax diagram
(source: vertigrated.com)

I really like things to compile/work without any warnings. How do I resolve this warning condition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2024-12-22 17:47:49

贾罗德·罗伯森写道:

我真的很喜欢在没有任何警告的情况下编译/工作。如何解决此警告情况?

您的解析器可以解析以下输入:

f(x)-> g(y)-> y*y, x=y

在两个不同的解析树中:

在此处输入图像描述

和:

在此处输入图像描述

您可以通过强制解析器向前查看并确保前面有 ',' 语句 来解决此问题在实际匹配这些规则之前。您可以通过使用语法谓词((...)=> 部分)并在其中包含所述规则来做到这一点:

function
  :  ID '(' args ')' '->' statement ((',' statement)=> ',' statement)* 
  ;

但是,如果您的函数 规则有某种“结束”标记,您尚未定义。从您之前的问题和示例来看:

myfunction(x) ->
  sqr(a) -> a * a,
  y = sqr(x).

您似乎正在使用 '.' 作为 function 的结尾。如果您将其添加到您的 function 规则中:

function
  :  ID '(' args ')' '->' statement (',' statement)* '.'
  ;

您根本不需要谓词。

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:

enter image description here

and:

enter image description here

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)* 
  ;

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).

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)* '.'
  ;

you don't need a predicate at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文