A lightweight, high-performance math expression evaluator built in C++. It uses a custom Lexer, Recursive Descent Parser, and an Abstract Syntax Tree (AST) to process mathematical expressions with proper operator precedence.
- Dual Mode: Use it as a one-off command-line tool or an interactive shell.
- AST-Based: Correctly handles operator precedence (multiplication before addition).
- Clean Output: Uses standard base-10 formatting for results.
- Zero Dependencies: Pure C++ using only the standard library.
- Operators support:
+-*/!(factorial)^(power)%(mod)()-5(negation supported)
Run the program without arguments to enter the REPL (Read-Eval-Print Loop).
$ ./calc
> 5 + 5
= 10
> 10 * 2 + 3
= 23
> [Press Enter to Exit]
Pass an expression as a string argument for a quick calculation.
$ ./calc "20 / 4 + 7"
12
The calculator processes your input through three distinct stages:
- Lexer: Breaks the raw string into tokens (Numbers, Operators, Parentheses).
- Parser: Organizes tokens into an AST based on mathematical grammar.
- Evaluator: Recursively traverses the tree to produce the final
floatresult.
This project uses CMake for a portable build process. Follow these steps to build the executable:
# 1. Create a build directory
mkdir build && cd build
# 2. Configure the project
cmake ..
# 3. Build the executable
cmake --build .
The resulting calc binary will be located in the build directory.
- Precision: Uses
floatfor evaluations. - Error Handling: Returns
NaN(Not a Number) if the parser fails to generate a valid root. - Exiting: In interactive mode, simply press
Enteron an empty line to quit.
Maks Makuta © 2026
No License - fell free to use that code in any way you need