Please download the scalc.jar
file to run the code.
Then use the following command to use the program: java -jar ./scalc.jar <arg>
. Please use JDK 8
or more recent versions of Java.
The general syntax of an expression is (OP EXPR EXPR)
, where OP
= {add
, multiply
, exponent
} and EXPR
={ (OP EXPR EXPR)
}.
java -jar ./scalc.jar (add 1 (multiply 2 0))
1
For this project I used Java and ANTLR4. ANTLR is used to specify a grammer, which is then used to generate a Java interface which can be used to parse some given text. ANTLR is very powerful as it has extensive tooling such as the ability to generate parse trees. Here is a generated parse tree of the expression (add 1 (multiply 5 (exponent 3 5)) 5 (multiply 4 2) 5)
using the grammer from Calc.g4
:
Normally a grammer is defined in a
*.g4
file and then it is complied using the ANTLR library. The generated classes can vary depending on your choice of having visitor
and or listener
classes.
ANTLR defines an interface for the respective visitor
and/or listener
Java classes and a base implementation which can be extended to do meaningful computation and evaluation of an expression following the specified grammer.
- ANTLR Grammer File:
Calc.g4
- ANTLR Generated Java Files
- Implementation of the Visitor class
CalcVisitor.java
- Instantiating the program
Calc.java
- Jar file of the program
scalc.jar