I wrote a simple mathematical expression converter with Jay: Yacc for Java for my study.
http://www.cs.rit.edu/~ats/lp-2002-2/html/skript-23.html
The sample code in this page is a mathematical expression interpreter. You can calculate four arithmetic operations and more like 1 + 2 * 3
to 7
. My code which will be described in this post converts expressions into reverse Polish notation ones like 1 + 2 * 3
to 1 2 + 3 *
.
You can test this with the following commands if you already have Jay.
$ /path-to-jay/jay -v -t ./Expr.jay < /path-to-jay/java/skeleton.java > Expr.java
$ javac -classpath /path-to-jay/yydebug/yydebug.jar:. Expr.java
$ echo '(1+2)*3 / (10 % 3) - (1 + 2 - 1 * 2.1)
(((10/3)))' | java -classpath /path-to-jay/yydebug/yydebug.jar:. Expr
And you can ascertain the results are correct.
1 2 + 3 * 10 3 % / 1 2 + 1 2.1 * - -
10 3 /
Acknowledge and References
- Jay home page http://www.informatik.uni-osnabrueck.de/alumni/bernd/jay/
- a lecture note http://www.cs.rit.edu/~ats/lp-2002-2/html/skript-23.html
I succeeded in achieving this with a great help by Masahiro Kimoto. Thanks!
No comments:
Post a Comment