A Python-based mathematical expression parser and evaluator built for learning and portfolio purposes.
This project tokenizes mathematical expressions, parses them into an Abstract Syntax Tree (AST), and recursively evaluates expressions using standard operator precedence rules.
Supports the following operators:
+ - * / // ** ^
Additional features:
- Operator precedence handling (PEMDAS)
- Right-associative exponentiation
- Multi-digit integer support
- AST (Abstract Syntax Tree) generation
- Recursive AST evaluation
- Modular project structure
- Supporting modules can run independently
- Includes algorithm explanations and documentation
expression-parser/
│
├── main.py
│
└── math/
├── lexer.py
├── parser.py
├── evaluator.py
└── ast_node.py
Tokenizes mathematical expressions into operators and numbers.
Parses tokens into an Abstract Syntax Tree using operator precedence rules.
Recursively evaluates AST nodes into final numerical results.
Defines AST node structures including:
NumberNodeBinaryOpNode
Handles user input, expression execution, and runtime flow.
2 + 3 * 4
(+)
├── 2
└── (*)
├── 3
└── 4
14
From the project root directory:
python -m mainExample:
python -m math.lexerYou may similarly run:
python -m math.parser
python -m math.evaluator- No parentheses support
- No unary minus / negative numbers
- Integers only
- No variables or functions
This project was created for:
- Learning parsing techniques
- Understanding AST construction
- Practicing recursive evaluation
- Exploring interpreter architecture
- Building portfolio projects
- Understanding compiler and language design concepts
Each file contains:
- Algorithm explanations
- Inline documentation
- Modular function design
Additional docstrings and documentation improvements are planned.
- Parentheses support
- Unary operators
- Variable support
- Function calls
- Better parser architecture
- Improved error handling
- More AST node types
- Better error messages
- More comprehensive docstrings
- Bug fixes and optimizations
This project is actively being improved and expanded as part of the learning process.