ANTLR/语法问题:计算器语言
我正在尝试为个人项目创建布尔表达式语言/语法。用户将能够用类似 Java 的语法编写字符串,并提供变量,这些变量将在变量初始化后进行评估。 雨 例如,用户可能输入字符串
@FOO+7 > 4*(5+@BAR);
Later,当变量FOO被初始化并且等于6并且BAR等于1时,表达式计算结果为13>24并且因此返回假。
我正在使用 ANTLRworks 生成语法,虽然它看起来不错,但它不能正确解释负号。 ANTLRworks 中的输入(由于某种原因)发生了变化:“(8-3)>6”被读取为“(8>6”(由于缺少右括号而无法运行)。我还没有还实现了变量查找,但到目前为止,这是整数的语法:
grammar BooleanCalculator;
@header {
package test;
}
prog : rule+
;
rule : boolean_expr ';' NEWLINE {System.out.println($boolean_expr.b);}
| NEWLINE
;
boolean_expr returns [boolean b]
: v1=num_statement
('<' v2=num_statement {$b = $v1.d < $v2.d;}
|'<=' v2=num_statement {$b = $v1.d <= $v2.d;}
|'=' v2=num_statement {$b = $v1.d == $v2.d;}
|'!=' v2=num_statement {$b = !($v1.d == $v2.d);}
|'>=' v2=num_statement {$b = $v1.d >= $v2.d;}
|'>' v2=num_statement {$b = $v1.d > $v2.d;})
;
num_statement returns [double d]
: v1=mult_statement {$d = $v1.d;}
('+' v2=mult_statement {$d += $v2.d;}
|'-' v2=mult_statement {$d -= $v2.d;})* //HERE IS THE OFFENDING LINE
;
mult_statement returns [double d]
: v1=var {$d = $v1.d;}
('*' v2=var {$d *= $v2.d;}
|'/' v2=var {$d /= $v2.d;}
|'%' v2=var {$d = $d/100*$v2.d;})*
;
var returns [double d]
: NUMBER {$d = Double.parseDouble($NUMBER.text);}
| '(' v1=num_statement ')' {$d = $v1.d;}
;
NUMBER : '0'..'9'+
;
除了“-”符号之外,它对所有内容都可以正常工作。还有人知道解决这个问题的方法吗?
另外(我对 ANTLR 很陌生):我是否正确地进行了评估?或者我应该让语法定义结构并使用另一种方法来确定该陈述是否为真/假?
I'm attempting to create a boolean expression language/grammar for a personal project. The user will be able to write a string in a Java-like syntax, with provision for variables, which will be evaluated at a later time when the variables have been initialised.
Rain
For example, a user might enter the string
@FOO+7 > 4*(5+@BAR);
Later, when the variable FOO is initialised and equal to 6, and BAR is equal to 1, the expression evaluates to 13>24 and thus returns false.
I'm using ANTLRworks to generate the grammar and whilst it LOOKS fine, it doesn't correctly interpret negative signs. The input in the ANTLRworks is (for some reason) changed: "(8-3)>6" is read as "(8>6" (which fails to run as it is missing the closing bracket). I haven't implemented the variable lookups yet, but here is the grammar so far for just integers:
grammar BooleanCalculator;
@header {
package test;
}
prog : rule+
;
rule : boolean_expr ';' NEWLINE {System.out.println($boolean_expr.b);}
| NEWLINE
;
boolean_expr returns [boolean b]
: v1=num_statement
('<' v2=num_statement {$b = $v1.d < $v2.d;}
|'<=' v2=num_statement {$b = $v1.d <= $v2.d;}
|'=' v2=num_statement {$b = $v1.d == $v2.d;}
|'!=' v2=num_statement {$b = !($v1.d == $v2.d);}
|'>=' v2=num_statement {$b = $v1.d >= $v2.d;}
|'>' v2=num_statement {$b = $v1.d > $v2.d;})
;
num_statement returns [double d]
: v1=mult_statement {$d = $v1.d;}
('+' v2=mult_statement {$d += $v2.d;}
|'-' v2=mult_statement {$d -= $v2.d;})* //HERE IS THE OFFENDING LINE
;
mult_statement returns [double d]
: v1=var {$d = $v1.d;}
('*' v2=var {$d *= $v2.d;}
|'/' v2=var {$d /= $v2.d;}
|'%' v2=var {$d = $d/100*$v2.d;})*
;
var returns [double d]
: NUMBER {$d = Double.parseDouble($NUMBER.text);}
| '(' v1=num_statement ')' {$d = $v1.d;}
;
NUMBER : '0'..'9'+
;
It is working correctly for everything except the '-' sign. Does anyone know a way to fix this?
Also (I'm very new to ANTLR): am I doing the evaluation correctly? Or should I just let the grammar define the structure and use another method to determine if the statement is true/false?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的语法:
(请注意,除了稍微重新格式化之外,我没有更改任何其他内容,并添加了一个额外的
println
用于调试!)生成了以下输出:
使用测试类:
所以,一切似乎都很顺利。
几点说明:
==
和!=
比较double
。请小心:舍入错误将导致意外行为(从用户的角度来看......);\%
。Your grammar:
(note that I did not change anything else than reformat it a bit, and added an extra
println
for debugging!)produced the following output:
using the test class:
So, all seems to go fine.
A couple of remarks:
double
s using==
and!=
. Be careful with that: rounding errors will result in unexpected behavior (from a user's perspective...);\%
.