ANTLR/语法问题:计算器语言 [英] ANTLR/Grammar issue: calculator language

查看:27
本文介绍了ANTLR/语法问题:计算器语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为个人项目创建布尔表达式语言/语法.用户将能够用类似 Java 的语法编写一个字符串,并提供变量,这些变量将在稍后初始化变量时进行评估.雨例如,用户可能会输入字符串

@FOO+7 >4*(5+@BAR);

之后,当变量 FOO 被初始化并等于 6,而 BAR 等于 1 时,表达式的计算结果为 13>24,因此返回 false.

我正在使用 ANTLRworks 生成语法,虽然看起来不错,但它不能正确解释负号.ANTLRworks 中的输入(出于某种原因)已更改:"(8-3)>6" 被读为 "(8>6"(由于缺少右括号而无法运行).我还没有实现变量查找尚未完成,但这里是到目前为止仅用于整数的语法:

语法布尔计算器;@header {包装测试;}编:规则+;规则:boolean_expr ';'NEWLINE {System.out.println($boolean_expr.b);}|新队;boolean_expr 返回 [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 返回 [double d]: v1=mult_statement {$d = $v1.d;}('+' v2=mult_statement {$d += $v2.d;}|'-' v2=mult_statement {$d -= $v2.d;})*//这里是违规行;mult_statement 返回 [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 返回 [double d]: NUMBER {$d = Double.parseDouble($NUMBER.text);}|'(' v1=num_statement ')' {$d = $v1.d;};数字:'0'..'9'+;

它对除-"符号外的所有内容都正常工作.有人知道解决这个问题的方法吗?

另外(我对 ANTLR 很陌生):我的评估是否正确?或者我应该让语法定义结构并使用另一种方法来确定语句是否为真/假?

解决方案

你的语法:

语法布尔计算器;编: 规则+;规则: boolean_expr {System.out.println($boolean_expr.b);};boolean_expr 返回 [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;} {System.out.println("v1=" + $v1.d + ", v2=" + $v2.d);});num_statement 返回 [double d]: v1=mult_statement {$d = $v1.d;} ( '+' v2=mult_statement {$d += $v2.d;}|'-' v2=mult_statement {$d -= $v2.d;})*;mult_statement 返回 [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 返回 [double d]: NUMBER {$d = Double.parseDouble($NUMBER.text);}|'(' v1=num_statement ')' {$d = $v1.d;};数字:'0'..'9'+;

(请注意,除了稍微重新格式化之外,我没有没有更改任何其他内容,并添加了额外的 println 用于调试!)

产生以下输出:

$ java -cp antlr-3.2.jar org.antlr.Tool BooleanCalculator.g$ javac -cp antlr-3.2.jar *.java$ java -cp .:antlr-3.2.jar Mainv1=5.0, v2=6.0错误的

使用测试类:

import org.antlr.runtime.*;公共课主要{public static void main(String[] args) 抛出异常 {ANTLRStringStream in = new ANTLRStringStream("(8-3)>6");BooleanCalculatorLexer lexer = new BooleanCalculatorLexer(in);CommonTokenStream 令牌 = new CommonTokenStream(lexer);BooleanCalculatorParser parser = new BooleanCalculatorParser(tokens);parser.prog();}}

所以,一切似乎都很顺利.

几点说明:

  • 您正在使用 ==!= 比较 double .小心:舍入错误将导致意外行为(从用户的角度来看......);
  • 在语法操作中使用模运算符可以通过用反斜杠转义来完成:\%.

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?

解决方案

Your grammar:

grammar BooleanCalculator;

prog    
  :  rule+
  ;

rule
  :  boolean_expr {System.out.println($boolean_expr.b);}
  ;

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;}  {System.out.println("v1=" + $v1.d + ", v2=" + $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;}
                                     )* 
  ;

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'+
  ;

(note that I did not change anything else than reformat it a bit, and added an extra println for debugging!)

produced the following output:

$ java -cp antlr-3.2.jar org.antlr.Tool BooleanCalculator.g 
$ javac -cp antlr-3.2.jar *.java
$ java -cp .:antlr-3.2.jar Main

v1=5.0, v2=6.0
false

using the test class:

import org.antlr.runtime.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ANTLRStringStream in = new ANTLRStringStream("(8-3)>6");
        BooleanCalculatorLexer lexer = new BooleanCalculatorLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        BooleanCalculatorParser parser = new BooleanCalculatorParser(tokens);
        parser.prog();
    }
}

So, all seems to go fine.

A couple of remarks:

  • you're comparing doubles using == and !=. Be careful with that: rounding errors will result in unexpected behavior (from a user's perspective...);
  • using the modulo operator in your grammar actions can be done by escaping it with a backslash: \%.

这篇关于ANTLR/语法问题:计算器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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