expression_parser

Crates.ioexpression_parser
lib.rsexpression_parser
version0.1.0
created_at2025-11-12 18:11:42.924044+00
updated_at2025-11-12 18:11:42.924044+00
descriptionA 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
id1929716
size33,866
(KaterynaLaptieva3570)

documentation

README

Expression Parser

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.

Overview

This project demonstrates how to build a simple expression language parser in Rust:

  • It reads a user-provided expression as a string.
  • Uses a formal grammar (.pest file) to tokenize and parse it.
  • Constructs an Abstract Syntax Tree (AST) from the parsed tokens.
  • Recursively evaluates the AST using variable values from user input.
  • Supports both interactive and CLI execution modes.

Features

  • Automatic substitution of variable values
  • Command-line interface for dynamic inputs
  • Support for summations, parentheses, and power operations

Program supports:

  • Numeric constants: 42, -3.14, 0.05
  • Variables: x, y, Revenue, a1
  • Parentheses: (a + b) * 2
  • Assignments: result = (a + b) * c
  • Whitespace flexibility: expressions can contain or omit spaces (a=1+2, a = 1 + 2, etc.)

Supported Operations:

  • Addition +
  • Subtraction -
  • Multiplication *
  • Division /
  • Parentheses ( )
  • Exponentiation ^
  • Summation Σ

Example Usage

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

Display Help:

cargo run -- --help

Display Credits:

cargo run -- credits

Grammar Definition

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+ }

Commit count: 0

cargo fmt