来自 antlr 2.7.6 的虚假不确定性警告
我正在尝试使用 antlr 2.7.6 创建一个简单的表达式解析器,并在编译过程中收到一些不确定性警告。生成的java源似乎完全按照我想要的方式工作,但我想知道是否可以抑制此警告,或者我是否在语法中做错了什么。
该解析器将在已经依赖于这个旧 antlr 版本的项目中使用,因此升级到 3.4 可能不是一个选择。语法本身与此答案中的语法类似。
这是编译期间的输出:
Using Antlr grammar: expr.g
ANTLR Parser Generator Version 2.7.6 (2005-12-22) 1989-2005
expr.g:15: warning:nondeterminism upon
expr.g:15: k==1:OR
expr.g:15: between alt 1 and exit branch of block
expr.g:19: warning:nondeterminism upon
expr.g:19: k==1:AND
expr.g:19: between alt 1 and exit branch of block
这是显示问题的简化语法:
header {
package net.jhorstmann.i18n.tools;
import net.jhorstmann.i18n.tools.*;
import net.jhorstmann.i18n.tools.expr.*;
}
class ExprParser extends Parser;
expression returns [Expression r = null]
: r=or_expr
;
or_expr returns [Expression r = null] { Expression e = null; }
: r=and_expr (OR e=and_expr { r = new OrExpression(r, e); })*
;
and_expr returns [Expression r = null] { Expression e = null; }
: r=prim_expr (AND e=prim_expr { r = new AndExpression(r, e); })*
;
prim_expr returns [Expression r = null] { Expression e = null; }
: b:BOOL { r = new ConstantExpression(Integer.parseInt(b.getText())); }
| NOT e=expression { r = new NotExpression(e); }
| OPEN e=expression CLOSE { r = e; }
;
class ExprLexer extends Lexer;
options {
k=2;
}
WS : (' ' | '\t')+ { $setType(Token.SKIP); };
BOOL : '0' | '1';
NOT : '!';
OPEN : '(';
CLOSE : ')';
OR : '|' '|';
AND : '&' '&';
I'm trying to create a simple expression parser with antlr 2.7.6 and am getting some nondeterminism warnings during compilation. The generated java source seems to work exactly as i want it, but I would like to know if its possible to suppress this warnings or if I'm doing something wrong in the grammar.
The parser is to be used in a project that already has a dependency on this old antlr version, so upgrading to 3.4 is probably not an option. The grammar itself look similar to the one in this answer.
Here is the output during compilation:
Using Antlr grammar: expr.g
ANTLR Parser Generator Version 2.7.6 (2005-12-22) 1989-2005
expr.g:15: warning:nondeterminism upon
expr.g:15: k==1:OR
expr.g:15: between alt 1 and exit branch of block
expr.g:19: warning:nondeterminism upon
expr.g:19: k==1:AND
expr.g:19: between alt 1 and exit branch of block
And here is the simplified grammar exhibiting the problem:
header {
package net.jhorstmann.i18n.tools;
import net.jhorstmann.i18n.tools.*;
import net.jhorstmann.i18n.tools.expr.*;
}
class ExprParser extends Parser;
expression returns [Expression r = null]
: r=or_expr
;
or_expr returns [Expression r = null] { Expression e = null; }
: r=and_expr (OR e=and_expr { r = new OrExpression(r, e); })*
;
and_expr returns [Expression r = null] { Expression e = null; }
: r=prim_expr (AND e=prim_expr { r = new AndExpression(r, e); })*
;
prim_expr returns [Expression r = null] { Expression e = null; }
: b:BOOL { r = new ConstantExpression(Integer.parseInt(b.getText())); }
| NOT e=expression { r = new NotExpression(e); }
| OPEN e=expression CLOSE { r = e; }
;
class ExprLexer extends Lexer;
options {
k=2;
}
WS : (' ' | '\t')+ { $setType(Token.SKIP); };
BOOL : '0' | '1';
NOT : '!';
OPEN : '(';
CLOSE : ')';
OR : '|' '|';
AND : '&' '&';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
奇怪的是,ANTLR 3 对于这样的语法没有任何问题。
因为它有关于“块的退出分支”的警告,请尝试使用文件结束标记(
EOF
)锚定您的条目规则:编辑
我会包含一个一元否定表达式如下:
Odd, ANTLR 3 has no problems with such a grammar.
Because its warning about an "exit branch of block", try anchoring your entry rule with an end-of-file token (
EOF
):EDIT
I'd include an unary negate-expression like this: