| Crates.io | expression_parser |
| lib.rs | expression_parser |
| version | 0.1.0 |
| created_at | 2025-11-12 18:11:42.924044+00 |
| updated_at | 2025-11-12 18:11:42.924044+00 |
| description | A minimal Rust parser and evaluator for arithmetic expressions with variables, built using the pest parsing library. It converts a text formula into an abstract syntax tree (AST) and computes the result based on user-provided variable values. |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1929716 |
| size | 33,866 |
A Rust-based Formula Parsing and Evaluation Engine
Expression Parser is a Rust application that reads, analyzes, and evaluates mathematical formulas written in plain text, using the pest grammar engine.
Main Idea: was to develop a reliable, extensible system for parsing and evaluating mathematical expressions using formal grammar definitions.
This project demonstrates how to build a simple expression language parser in Rust:
.pest file) to tokenize and parse it.Enter expression: result = (a + b) * (c - d / 2)
Enter variables: a=3 b=5 c=10 d=4
result = 68
Enter expression: sum = Σk=1to2(k^a)
Enter variables: a=2
// 1^2 + 2^2 = 5
result = 5
You can store a formula and its variables in a file: formula.txt:
ROI = (R - C) / C * 100
R=1500
C=1000
cargo run -- parse formula.txt
Formula from file:
ROI = (R - C) / C * 100
ROI = 50
cargo run -- --help
cargo run -- credits
The parser uses a formal grammar written in pest syntax to define how formulas are recognized and parsed into an Abstract Syntax Tree (AST).
file = { SOI ~ expr ~ EOI }
expr = { assign | summation | sum | product }
assign = { ident ~ "=" ~ expr }
summation = { "Σ" ~ ident ~ "=" ~ number ~ "to" ~ number ~ "(" ~ expr ~ ")" }
sum = { product ~ (("+" | "-") ~ product)* }
product = { power ~ (("*" | "/") ~ power)* }
power = { atom ~ ("^" ~ atom)* }
atom = { number | ident | "(" ~ expr ~ ")" }
number = @{ "-"? ~ ASCII_DIGIT+ ~ ("." ~ ASCII_DIGIT+)? }
ident = @{ ASCII_ALPHA+ }