ANTLR 错误:决策可以使用多种替代方案来匹配输入

发布于 2024-12-12 19:54:13 字数 1346 浏览 0 评论 0原文

我不明白如何处理 ANTLR 语法的错误:

****************error message*********
Decision can match input such as "{'+', '-'} IDENT" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
 |---> add: mult (('+'|'-') mult)*;
***************************************

这主要是从 Scott 的示例中复制的,但我不明白为什么他的效果很好,但我的却卡住了,如何摆脱它?

---------------以下 Sample.g----------------

grammar Sample;

options { 
  language = Java;
}


 program
      :  'program' IDENT '='
          (constant| variable)*
          'begin'
          (statement)*
          'end' IDENT '.'
      ; 

constant:
       'constant' IDENT ':' type ':=' expression ';' 
      ;

type: 'integer';

variable: 'var' IDENT (',' IDENT)* ':' type  ';';


statement: 'var' IDENT ':=' INTEGER ';' ;

//expression

term: IDENT |'(' expression ')'|INTEGER;
negation: 'not'* term;
unary: ('+'|'-')* negation;
mult: unary (('*'|'/'|'mod') unary)*;

[以下行的 XXX 错误]

add: mult (('+'|'-') mult)*;
relation: add (('='|'/='|'<'|'<=') add)*;
expression: (relation ('and'|'or') relation)*;


END : 'end';

CONSTANT : 'constant';

INTEGER: '0'| (('1'..'9') ('0'..'9')*);   

IDENT: ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9')*;

WS: ('\n'|' '|'\t'|'r'|'\f')+ {$channel=HIDDEN;};

I do not see how to handle the error of a ANTLR grammar:

****************error message*********
Decision can match input such as "{'+', '-'} IDENT" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input
 |---> add: mult (('+'|'-') mult)*;
***************************************

This is mostly copied from the example of Scott, but I do not see why his works well but mine got stuck and how to get out of it?

---------------following Sample.g----------------

grammar Sample;

options { 
  language = Java;
}


 program
      :  'program' IDENT '='
          (constant| variable)*
          'begin'
          (statement)*
          'end' IDENT '.'
      ; 

constant:
       'constant' IDENT ':' type ':=' expression ';' 
      ;

type: 'integer';

variable: 'var' IDENT (',' IDENT)* ':' type  ';';


statement: 'var' IDENT ':=' INTEGER ';' ;

//expression

term: IDENT |'(' expression ')'|INTEGER;
negation: 'not'* term;
unary: ('+'|'-')* negation;
mult: unary (('*'|'/'|'mod') unary)*;

[XXX Errorfor the following line]

add: mult (('+'|'-') mult)*;
relation: add (('='|'/='|'<'|'<=') add)*;
expression: (relation ('and'|'or') relation)*;


END : 'end';

CONSTANT : 'constant';

INTEGER: '0'| (('1'..'9') ('0'..'9')*);   

IDENT: ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9')*;

WS: ('\n'|' '|'\t'|'r'|'\f')+ {$channel=HIDDEN;};

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

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

发布评论

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

评论(2

瞄了个咪的 2024-12-19 19:54:15

您在以下情况中放错了括号:

expression: (relation ('and'|'or') relation)*;

使您的语法不明确:解析器无法决定何时看到 - IDENT 它是否应该是 add- 或 的一部分>一元-规则。

例如,您的规则表达式 现在与此匹配:

relation ('and'|'or') relation relation ('and'|'or') relation

即,两个关系 规则直接放置在彼此之后。如果解析器现在偶然发现这样的输入:

- A - B

解析器“看到”解析此输入的两种可能性:

1(一元表达式和一元表达式)

在此处输入图像描述

2(一元并添加表达式)

在此处输入图像描述

它应该be:

expression: relation (('and'|'or') relation)*;

相反,所以永远不可能有 2 个连续的表达式(并且没有歧义!)。

You've misplaced a parenthesis in:

expression: (relation ('and'|'or') relation)*;

making your grammar ambiguous: the parser cannot decide when it sees a - IDENT if it should be a part of an add- or unary-rule.

For example, your rule expression now matches this:

relation ('and'|'or') relation relation ('and'|'or') relation

i.e., two relation rules directly placed after each other. If the parser now stumbles upon input like this:

- A - B

the parser "sees" two possibilities to parse this input:

1 (unary expression & unary expression)

enter image description here

2 (unary & add expression)

enter image description here

It should be:

expression: relation (('and'|'or') relation)*;

instead, so the there can never be 2 successive expressions (and no ambiguity!).

千柳 2024-12-19 19:54:15
  1. 最好提及您从中获取代码的源网页。我认为这里是 Antlr3xtut

这主要是从 Scott 的例子中复制的,但我不明白为什么
他的工作很好,但我的卡住了,如何摆脱它?

这是因为你复制错了。

而不是如下所述的正确代码:

expression
    :   relation (('and' | 'or') relation)*
    ;

您已复制:

expression: (relation ('and'|'or') relation)*;

您能够发现错误吗?例如,将 '(' 放置在关系之前。因此,您会收到错误 Decision can match input such as "{'+', '-' } IDENT”使用多​​种替代方案

在此处输入图像描述

解决方案

将表达式规则替换为斯科特样本中给出了什么。

  1. It is good to mention the source web page from where you have picked up the code. I think here it is Antlr3xtut.

This is mostly copied from the example of Scott, but I do not see why
his works well but mine got stuck and how to get out of it?

This is because you copied incorrectly.

Instead of correct code as mentioned below:

expression
    :   relation (('and' | 'or') relation)*
    ;

You have copied:

expression: (relation ('and'|'or') relation)*;

Are you able to spot the error? For example the placement of '(' just before the relation. And because of that you are getting the error Decision can match input such as "{'+', '-'} IDENT" using multiple alternatives:

enter image description here

Solution

Replace you expression rule with what is given in scotts sample.

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