Skip to content

Skyy0x/math-tokenizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

🧮 math-tokenizer

Java Maven Build

A high-performance mathematical expression engine written in Java 21, designed as a portfolio-grade project showcasing compiler-like architecture, symbolic computation, and clean API design.


🎯 Project Overview

math-tokenizer is more than a calculator — it's a modular expression engine that mimics how real interpreters and compilers work.
It transforms raw mathematical input into structured representations, enabling both:

  • ⚡ Fast numeric evaluation
  • 🌿 Symbolic differentiation (like a mini CAS)

This project demonstrates skills in:

  • Language design & parsing
  • Abstract Syntax Trees (AST)
  • Algorithmic problem-solving
  • Clean architecture & separation of concerns

✨ Features

  • 🔢 Expression Evaluation – Supports +, -, *, /, ^ with full precedence handling
  • 💾 Variables & State – Assign and reuse variables (e.g., x = 10)
  • 🌿 Symbolic Differentiation – Automatically compute derivatives
  • 🧹 Expression Simplification – Reduce expressions using algebraic identities
  • 🛡️ Validation Layer – Detects invalid states before execution
  • 🧩 Extensible Functions – Easily add new math functions

⚙️ How It Works

The engine is built as a deterministic processing pipeline, where each stage has a single responsibility:

graph TD
    A[Source String] --> B[Lexer]
    B -->|Tokens| C[Parser]
    C -->|AST| D[Validator]
    D --> E[Evaluator]
    D --> F[Differentiator]
    F --> G[Simplifier]
    G --> H[Printer]
    E --> I[Numeric Result]
    H --> J[Readable Expression]
Loading

Pipeline Stages

  1. Lexer – Converts raw input into tokens
  2. Parser – Builds an Abstract Syntax Tree (AST)
  3. Validator – Ensures semantic correctness
  4. Evaluator – Computes numeric results
  5. Differentiator – Applies calculus rules
  6. Simplifier – Reduces expressions
  7. Printer – Converts AST back to readable form

🧠 Supported Grammar

statement      -> assignment | expression
assignment     -> IDENTIFIER "=" expression
expression     -> additive
additive       -> multiplicative ( ("+" | "-") multiplicative )*
multiplicative -> power ( ("*" | "/") power )*
power          -> unary ( "^" power )?
unary          -> "-" unary | primary
primary        -> NUMBER | IDENTIFIER ( "(" argList ")" )? | "(" expression ")"

📁 Project Structure

src/main/java/pl/sky0x/tokenizer/
├── ast/                # AST nodes & operators
├── differentiation/    # Symbolic differentiation logic
├── evaluator/          # Numeric evaluation engine
├── functions/          # Built-in math functions
├── lexer/              # Tokenization
├── parser/             # Recursive-descent parser
├── pipeline/           # Orchestration layer
├── simplifier/         # Algebra simplification
└── validation/         # Error & semantic checks

🧪 Example Usage

Solver Mode

Input:  x = 5
        x ^ 2 + 1

Output: 26.0

Differentiation Mode

Input:  x^2 + 2*x + 1

Output: 2*x + 2

📚 Differentiation Rules

  • Product Rule
  • Quotient Rule
  • Power Rule

The engine applies these rules directly on the AST, ensuring correctness and extensibility.


🧰 Tech Stack

  • Java 21 – Core language
  • Maven – Dependency management & build
  • Recursive Descent Parsing – Custom parser implementation
  • AST-based Architecture – Clean and scalable design

🚀 Getting Started

Requirements

  • JDK 21
  • Maven 3.6+

Run Locally

git clone https://github.com/Skyy0x/math-tokenizer.git
cd math-tokenizer
mvn compile exec:java -Dexec.mainClass="pl.sky0x.tokenizer.Main"

⚠️ Error Handling

Exception Description
LexerException Invalid tokens
ParserException Syntax errors
ValidationException Division by zero, undefined variables
FunctionException Invalid function usage

💡 Why This Project?

This project was built to demonstrate:

  • Deep understanding of parsing and interpreters
  • Ability to design scalable systems
  • Writing clean, maintainable Java code
  • Implementing non-trivial algorithms from scratch

👨‍💻 Author

Developed by sky0x

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages