ANTLR 错误:决策可以使用多种替代方案来匹配输入
我不明白如何处理 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在以下情况中放错了括号:
使您的语法不明确:解析器无法决定何时看到
- IDENT
它是否应该是add
- 或的一部分>一元
-规则。例如,您的规则
表达式
现在与此匹配:即,两个
关系
规则直接放置在彼此之后。如果解析器现在偶然发现这样的输入:解析器“看到”解析此输入的两种可能性:
1(一元表达式和一元表达式)
2(一元并添加表达式)
它应该be:
相反,所以永远不可能有 2 个连续的表达式(并且没有歧义!)。
You've misplaced a parenthesis in:
making your grammar ambiguous: the parser cannot decide when it sees a
- IDENT
if it should be a part of anadd
- orunary
-rule.For example, your rule
expression
now matches this:i.e., two
relation
rules directly placed after each other. If the parser now stumbles upon input like this:the parser "sees" two possibilities to parse this input:
1 (unary expression & unary expression)
2 (unary & add expression)
It should be:
instead, so the there can never be 2 successive expressions (and no ambiguity!).
这是因为你复制错了。
而不是如下所述的正确代码:
您已复制:
您能够发现错误吗?例如,将
'('
放置在关系之前。因此,您会收到错误Decision can match input such as "{'+', '-' } IDENT”使用多种替代方案
:解决方案
将表达式规则替换为斯科特样本中给出了什么。
This is because you copied incorrectly.
Instead of correct code as mentioned below:
You have copied:
Are you able to spot the error? For example the placement of
'('
just before the relation. And because of that you are getting the errorDecision can match input such as "{'+', '-'} IDENT" using multiple alternatives
:Solution
Replace you expression rule with what is given in scotts sample.