Back to: Main Page > Mathematical programs

Mathematical expression parser in C++

Download the source (zipped tar file)


Last updated: September 11, 2008.


The title is quite self-explanatory: with this module, the user can type a mathematical formula, which is then built into an object allowing to do computations with this formula.

This parser is intended to be simple to use both for the programmer and the user. It offers some formal calculus possibilities, such as differentiation.

The parser will try to read as much as possible in the user's mind. For example, an input like sinxcos(x)^2 will be interpreted as (sin(x))*(cos(x))^2

For the programmer, it is object-oriented. A constructor simply takes a string expression and variable names and returns an object which can be used to get values of the formula, or combined with other objects of the same type. The example below should make it clear.

As a counterpart, the code is not very clean...

This program I used, for example, to write a program for Drawing vector fields, differential equations, level curves...

There are two groups of files, one for dealing with real expressions and the other one for complex expressions(bearing the same names suffixed with _c).

I have had problems compiling the complex library with g++. I can't find two computers where it works the same way. It may be necessary to slighlty change the headers of the complex files.

Note that I began writing this at a time when the C++ standard library did not exist. I haven't had the energy to update everything, hence the use of old-fashioned C-style strings and arrays.


See a sample file on how to use the code.

The command
g++ example.cpp mathexpr.cpp -lm
should leave you with a working sample executable.


Copyright Yann OLLIVIER 1997-2000

This software may be freely distributed as is, including this whole notice.

It may be modified, but any modification should include this whole notice as is and a description of the changes made.

This software may not be sold.

This software or any modified version of it may be freely used in free programs. The program should include a copy of this whole notice. If you want to use it in a program you sell, contact me

This software comes with absolutely no warranty.


Thanks to Werner Jacobs, Valts Silaputnins and Olivier Wittenberg for bug correction.

Back to: Main Page > Mathematical programs

To leave a comment: contact (domain)