Wednesday, October 14, 2009

Math Parser for C++

Source: www.millionslink.com

By: Programmer, Wed Oct 14th, 2009
C++ Programmer? Do you need to parse mathematical expressions using C++? bcParserCPP Mathematical Expression Parser for C++ parses and evaluates math expressions given as strings at runtime.

Parsing formulas given as strings at runtime is a challenge for any programmer when he wants to allow the user input a mathematical formula. It is easy to compute mathematical expressions that are known at compile time. We type it in our C++ code, compile and when we execute our program, we get the result. For example x+sin(x) can be easily computed when the value of x is supplied. However, when the mathematical expression is given as a string at runtime, for example "x+sin(x)", it is no different then a meaningless "abc". The programmer has to make an explicit effort to make the program understand that there is a plus (+) sign that adds the value of x and the value of sin(x). Of crourse, the program also needs to understand the concept of a function call such as "sin(x)".

The concept of breaking such an expression into it's conceptual sub-sections is called parsing. The programmer writes code which typically creates a data structure which represents the mathematical expression in a way that can be used to evaluate it efficiently. This is a simplified, very high level, birds eye view of the solution process.

C++ language, with it's expressive syntax, capable libraries and data structures makes it possible to implement a re-usable solution for the expression parsing problem. bcParserCPP -Math Parser for C++ - simplifies the task for us by implementing the parse and evaluate algorithm. The expression is first parsed into an expression tree. Once this is done, the compiled tree structure is re-used with different variable values to achieve very fast evaluation performance.

bcParserCPP Math parser is implemented in C++. It is very flexible due to it's C++ template based design and Object Oriented architecture. It allowes the programmer set the expression as a string, create user defined variables, set their values, create user defined functions and get expression value with one function call. The expression can contain arithmetic operators, logical operators, variables, functions, and numeric literals.

A simple example looks like this:
MathParser.SetExpression("x+sin(x)");
MathParser.SetX(3);
MathParser.SetY(2);
double value = MathParser.getValue();


The programmer can implement his own functions in C or C++ language and hook them up to the Math Parser object so that they can be used in expressions all with few lines of code.

The Math Parser reports errors via ParserException. This exception object carries out syntax errors that indicate invalid portion of the mathematical expression. The Math Parser class is template based and the numeric type that it uses can be plugged in. Typical use is with float, double, long double types. But it is possible to hookup user defined numeric types that implement necessary operators to support arbitrary precision arithmetic. The Math Parser can be extended to support other numeric types such as complex numbers as well. The variable and function names, and error messages are communicated using a string class. By default, this is the basic_string of standard C++ library. string for ascii and wstring for unicode are both supported.

Tackling all of the challenges of implementing a parser is almost impossible within the scope of a broader project where there are pressing requirements in terms of time and functionality to be delivered. Most of us need to focus on the actual business problem at hand and view the mathematical expression parsing aspect of the problem as a side issue. Thus, it becomes very very hard to deliver correct, united tested, proven code on time and in budget. Trying to code a expression parser could take an experienced developer a month to write and to test.

When such as road block appears, the best way is to find a good component that can easily be integrated into your development project, leaving you free time to focus on other aspects of your project or to simply go home and rest. There is peace of mind in using a component that many others have used for many years and flushed out the bugs for you.

bcParserPP C++ math parser is a well tested, easy to use, simple C++ Class that can save time and money for experienced or novice programmers alike. It comes as a C++ Header file soure code so you are never stuck with a black box. Template based, C++ Object Oriented implementation makes it possible to customize many aspects of the parser. There is no library to link, just include the C++ header file and you are all set. It's a breeze to use and pleasure to see in action. It supports Visual C++, Borland C++ and GNU C++ on Windows, Linux, Mac OS X.

No comments: