新的一年,新版本:-)
由于您是 ANTLR 的新手,并且现在已经看到 v3 的一些歧义问题,您一定会欣赏 v4 的强大功能。以下是a解决方案(我是 v4 的新手)将计算保持在表达式级别,但展示了使用 ANTLR4 描述表达式是多么简单。
grammar Calculator;
progr : eval+ EOF ;
eval
: mult ';' {System.out.println($eval.text + " -> " + $mult.value);}
;
mult returns [double value]
: e1 = expr {$value = $e1.value;}
| e1 = expr e2 = mult {$value = $e1.value * $e2.value;}
;
expr returns [double value]
: e1 = expr '^'<assoc=right> e2 = expr {$value = Math.pow($e1.value, $e2.value);}
| '-' e1 = expr {$value = -1 * $e1.value;}
| e1 = expr '*' e2 = expr {$value = $e1.value * $e2.value;}
| e1 = expr '/' e2 = expr {$value = $e1.value / $e2.value;}
| e1 = expr '+' e2 = expr {$value = $e1.value + $e2.value;}
| e1 = expr '-' e2 = expr {$value = $e1.value - $e2.value;}
| 'cos' s = expr {$value = 2; System.out.println("cos" + $s.text + "=2");} // {$value = Math.cos($s.value);}
| 'sin' s = expr {$value = 5; System.out.println("sin" + $s.text + "=5");} // {$value = Math.sin($s.value);}
| FLOAT {$value = Double.parseDouble($FLOAT.text); System.out.println("FLOAT=" + $FLOAT.text);}
| '(' FLOAT ')' {$value = $expr.value;} // just for demo, to avoid printing FLOAT parameters
| '(' e1 = expr ')' {$value = $e1.value; System.out.println("expr(" + $e1.text + ")=" + $e1.value);}
;
FLOAT
: DIGIT+ ( '.' DIGIT* )? EXPONENT?
| '.' DIGIT+ EXPONENT?
;
WS : [ \t\r\n] -> channel(HIDDEN)
;
fragment DIGIT : [0-9] ;
fragment EXPONENT : [Ee] ( '+' | '-' )? DIGIT+ ;
文件输入.txt:
3cos(2)sin(6);
4cos(2)cos(8)sin(6);
sin(1)sin(6);
sin(1)sin(6)cos(8)10sin(8);
sin(1)5cos(6)3;
2^8;
3*-12;
55/sin(0);
55/(2 + 3);
执行 :
$ echo $CLASSPATH
.:/usr/local/lib/antlr-4.0b3-complete.jar
$ alias
alias antlr4='java -jar /usr/local/lib/antlr-4.0b3-complete.jar'
alias grun='java org.antlr.v4.runtime.misc.TestRig'
$ antlr4 Calculator.g4
$ javac Calculator*.java
$ grun Calculator progr input.txt
FLOAT=3
cos(2)=2
sin(6)=5
3cos(2)sin(6); -> 30.0
FLOAT=4
cos(2)=2
cos(8)=2
sin(6)=5
4cos(2)cos(8)sin(6); -> 80.0
sin(1)=5
sin(6)=5
sin(1)sin(6); -> 25.0
sin(1)=5
sin(6)=5
cos(8)=2
FLOAT=10
sin(8)=5
sin(1)sin(6)cos(8)10sin(8); -> 2500.0
sin(1)=5
FLOAT=5
cos(6)=2
FLOAT=3
sin(1)5cos(6)3; -> 150.0
FLOAT=2
FLOAT=8
2^8; -> 256.0
FLOAT=3
FLOAT=12
3*-12; -> -36.0
FLOAT=55
sin(0)=5
55/sin(0); -> 11.0
FLOAT=55
FLOAT=2
FLOAT=3
expr(2 + 3)=5.0
55/(2 + 3); -> 11.0
Notes :
1)我给 cos 和 sin 赋予了固定值,以便能够轻松验证计算。
2)使用 -diagnostics 运行,您将看到不明确的消息:
$ grun Calculator progr -diagnostics input.txt
FLOAT=3
line 1:6 reportAttemptingFullContext d=2, input='(2)'
line 1:6 reportAmbiguity d=2: ambigAlts={5, 6}, input='(2)'
这是因为我添加了以下行:
| '(' FLOAT ')' {$value = $expr.value;} // just for demo
良好地打印(避免将参数打印为 cos/sin)。这是有歧义的'(' e1 = expr ')'
。将其删除,消息就会消失。
3) 查看 ANTLR4 如何标记化 (-tokens) 和解析 (-trace) :
grun Calculator progr -tokens -diagnostics -trace input.txt
4) 站点:http://antlr4.org http://antlr4.org
5)书:http://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference http://pragprog.com/book/tpantlr2/the-definitive-antlr-4-reference
6) 在 OS X 上安装 4.0b3 :http://forums.pragprog.com/forums/206/topics/11231 http://forums.pragprog.com/forums/206/topics/11231
7)SO过滤器:https://stackoverflow.com/questions/tagged/antlr4 https://stackoverflow.com/questions/tagged/antlr4
8) 组别:https://groups.google.com/forum/#!forum/antlr-discussion https://groups.google.com/forum/#!forum/antlr-discussion