A calculator application that converts standard mathematical expressions to Reverse Polish Notation (RPN) and evaluates them using ANTLR (ANother Tool for Language Recognition).
This project implements a calculator that processes mathematical expressions, converts them to Reverse Polish Notation (also known as postfix notation), and evaluates them. It uses ANTLR4 for parsing expressions and the Visitor pattern for traversing and evaluating the parse tree.
- Parses standard mathematical expressions
- Converts expressions to Reverse Polish Notation
- Supports basic arithmetic operations:
- Addition
- Subtraction
- Multiplication
- Division
- Handles negative numbers and complex expressions
- Java 8 or higher
- Gradle (or use the included wrapper)
./gradlew buildThis will:
- Generate the ANTLR parser and lexer classes
- Compile the Java code
- Run the tests
./gradlew runThe calculator uses ANTLR4 to define a grammar for mathematical expressions. The process flow is:
- Input expression is parsed by ANTLR-generated lexer and parser
- The resulting parse tree is traversed by a post-order visitor (
ExprPostOrderVisitor) - The visitor converts the expression to Reverse Polish Notation using
Atomobjects - The expression can then be evaluated following RPN rules
src/main/antlr: Contains ANTLR grammar filessrc/main: Java implementation filesantlr: Generated ANTLR codelogic: Core logic includingAtomclass for expression handling
src/test: JUnit tests for verifying functionality
The project uses JUnit Jupiter (JUnit 5) for testing. Various test cases verify correct handling of different mathematical operations and edge cases.
To run the tests:
./gradlew test[Include license information here]
- ANTLR project (https://www.antlr.org/)
- JUnit Testing Framework