产生以下输出:$ 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.RainFor 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 Mainv1=5.0, v2=6.0falseusing 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/语法问题:计算器语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!