🔍ParsersDEV CommunityContent type: Blog

Building an Expression Calculator in Rust — Pratt Parsing Turns Precedence into Two Numbers, and Rationals Kill Float Error (opens in new tab)

Discussed on DEV

A terminal calculator where 1 + 2 * 3 is 7, 2^3^2 is 512 (= 2^9), and 0.1 + 0.2 is exactly 0.3 — not 0.30000000000000004. I wrote pcalc in Rust. Two implementation hinges: (1) a Pratt parser that expresses operator precedence as two "binding power" numbers per operator instead of a tower of grammar rules, and (2) exact rational arithmetic — every value is a reduced fraction — so floating-point rounding error simply doesn't exist. 📦 GitHub: (It's a CLI — no live demo. Run with cargo run or Doc...

Read the original article
Sign in to keep reading the full article.

Keyboard Shortcuts

Navigation

Next / previous post
j/k
Open post
oorEnter
Preview post
v

Post Actions

Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Save / unsave
s

Recommendations

Add interest / feed
Enter
Not interested
x

Go to

Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Discover
gb
Search
/

General

Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help