Skip to main content

Arithmetic calculator which uses a Pratt parser

Project description

Introduction

An arithmetic expression calculator in Python, demoing the Pratt parsing algorithm.

This takes inspiration from this 2010 blog post by Eli Bendersky, as well as a few other sources:

  1. Simple but Powerful Pratt Parsing
  2. Pratt Parsers: Expression Parsing Made Easy
  3. Compiling Expressions (Chapter 17 of Crafting Interpreters)

Requirements

Requires Python 3.13 or greater.

Installation

pipx install pratt-calc

In some cases it may be necessary to specify the Python version manually:

PIPX_DEFAULT_PYTHON=python3.13 pipx install pratt-calc

Or, if you have uv installed:

uvx pipx install pratt-calc

Contributing

Install uv, then run:

git clone https://github.com/BrandonIrizarry/pratt-calc
cd pratt-calc
uv sync --locked

Usage

pratt-calc $EXPRESSION

Example:

pratt-calc '3-4*5'

This should print -17 at the console.

Note that surrounding the input with single-quotes is recommended for all but the simplest expressions, to avoid clashing with the shell you're using.

Trigonometric Functions

pratt-calc supports the following trigonometric functions:

  1. sin
  2. cos
  3. tan
  4. csc
  5. sec
  6. cot

The constant 𝝿 is also available as pi. Examples:

pratt-calc 'cos(pi)' => -1.0

pratt-calc 'sin(1)^2 + cos(1)^2' => 1.0

A Note on the Implementation of Trig Functions

Trig functions are implemented as unary operators, as opposed to function calls. Hence the parentheses used by sin and so forth are merely there to enforce precedence, even though they conveniently evoke the intuition of a function call.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pratt_calc-0.2.2.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pratt_calc-0.2.2-py3-none-any.whl (18.0 kB view details)

Uploaded Python 3

File details

Details for the file pratt_calc-0.2.2.tar.gz.

File metadata

  • Download URL: pratt_calc-0.2.2.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pratt_calc-0.2.2.tar.gz
Algorithm Hash digest
SHA256 69bce3d80fd6ad3b51618647cd582b77360b6cc32b0120b946807a0b15eab8d4
MD5 45f28cd31128567b730e50c89252e7be
BLAKE2b-256 a3c6c5013ad995b3c224d989a152acc76fb0ce11fe9dc9cdfbcc3cc2612dd10b

See more details on using hashes here.

File details

Details for the file pratt_calc-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: pratt_calc-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 18.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pratt_calc-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 14d4766eb8060d02da677c8295515f7a9ae01928b6a9a0b9282c17e92b2bf825
MD5 a0ba179aabb9091a5d3d0a4518ed7ac4
BLAKE2b-256 b579778014bd958a0bba39ebae7a6ba75e8bfab54eebf27f1f63189d076733fe

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page