如何在我的 ANTLR3 语法中摆脱以下多个替代警告? [英] How to get rid of the following multiple alternatives warnings in my ANTLR3 grammar?

查看:22
本文介绍了如何在我的 ANTLR3 语法中摆脱以下多个替代警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

Jarrod Roberson 写道:

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

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:

和:

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆