Skip to content

lumbytyci/math-expr-recursive-descent-parser

Repository files navigation

Build Status

Recursive Descent Parser

A simple and lightweight recursive descent parser for parsing mathematical expressions.

Currently only parsing is supported, no error handling is provided.

Supported operations are negation, exponentiation, addition, multiplication and grouping.
The recuirsive descent parser works with the following grammar:

Expression = Addition
Addition = Multiplication | Addition "+" Multiplication | Addition "-" Multiplication
Multiplication = Exponentiation | Multiplication "*" Exponentiation | Multiplication "/" Exponentiation
Exponentiation = Primary | Exponentiation "^" Primary
Primary = Immediate | "(" Expression ")"

Currently the parser runs in a simple REPL:

>>> 2+2+2-6
0.0
>>> 2*4/8
1.0
>>> 2^10
1024.0
>>> (2+2)*2
8.0
>>> 2^2^2
16.0
>>> 2^((3+2)*2)/(2*512)
1.0

Getting started

  1. Install virtual-env (skip if already installed)
  2. Clone the repository
  3. Setup the virtualenv and activate
  4. Install the required dependencies
pip install virtualenv
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt

Contributing

Feel free to help with additional functionality by opening a PR. Follow the styling guidelines. All tests must pass. Features needed:

  • Error handling (Matching brackets, illegal expressions etc.)
  • Division order of operation

About

Parse mathematical expressions with a recursive descent parser

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages