A Math Parser for Python

This repository contains a parser for simple mathematical expressions of the form 2*(3+4) written in 116 lines of Python code. No dependencies are used except for what can be found in the Python standard library. It exists solely for educational reasons.

How to Use it

python3 compute.py '2*(3+4)'

and you should receive 14 as the result.

This is perhaps not so surprising, but you can also run

python graphviz.py '2*(3+4)' > graphviz_input
dot -Tpng graphviz_input -o output.png

to get a visual reprentation of the abstract syntax tree

(this requires having Graphviz installed).

simple-tree

Introduction

Creating a hand-written parser for anything is completely useless nowadays as there are tools like

ANTLR that perform all the heavy lifting for you.

Moreover, this particular problem must have been solved millions of times

by undergrad computer science students all around the world.

However, it has not been solved by me until this date,

as in my undergrad studies at TU Vienna we were skipping the low-level work

and built a parser based on yacc/bison.

I really enjoyed doing this small side project

because it takes you back to the roots of computer science

(this stuff dates back to 1969, according to Wikipedia)

and I like a lot how you end up with a beautiful and simple solution.

Be aware that I am by no means an expert in compiler construction

and someone who is would probably shudder at some of the things happening here,

but to me it was a nice educational exercise.

The literature regarding this topic is very formal,

which makes it a bit hard to get into the topic for an uninitiated person.

In this description, I have tried to focus more on intuitive explanations.

However, to me it is quite clear that if you don’t stick to the theory,

then you will soon run into things that are hard to make sense of

if you cannot connect it to what’s going on in the literature.

#data visualization #express

A simple parser for mathematical expressions
1.25 GEEK