How do you parse a mathematical formula in Delphi? Interpreted languages make expression evaluation make it easy. But what about compiled applications? Parsing mathematical expressions is a challenge in computer programming. Innocent looking expressions such as “sin(x)/cos(y)” can be notoriously hard and frustrating to develop and test. Luckily, this is now a solved problem. We have formula parsers for Delphi, C++ Builder, .NET, C++, Java, COM, Python, PHP. Your needs are covered for all of these languages with the time proven and tested features of math parser components.

## TbcParser Delphi Component

TbcParser is a mathematical expression parser VCL (Visual Component Library) component that can be used with Delphi and C++ Builder 4,5,6,7, 2006, 2007, 2009, 2010, XE, XE2, XE3, XE4, XE5, XE6, XE7, XE8, Delphi 10, 10.1, 10.2 Tokyo, 10.3 Rio, 10.4 Sydney, Delphi 11 and 12 and most likely any newer version. It also works with and Lazarus / Free Pascal. It can be used for **Windows, Mac OSX, iOS and Android Development with Delphi**.

TbcParser VCL Component features include:

- Easy to use component Programming API.
- Comes with predefined functions.
- You can create custom functions/variables and get a callback to your own functions that you define in your source code.
- Optimization: Constant expression elimination for repeated tasks.
- Logical Operators: <, >, =, &, |, !, <>, <=, >=
- Analytic Operators: +, -, /, *, ^
- Paranthesis: (, {, [
- Functions in the form of: f(x,y,z,…)
- Royalty free distribution.
- Source code is included.

List of predefined functions is available in the math parser documentation.

TbcParser is useful in financial, scientific, engineering programs. One can use the math parser in a speadsheet application like Excel.

It’s internal arithmetic uses ‘Extended’ Delphi storage type for floating point numbers.

TbcParser formula parser component comes with the Pascal source code and there is also a help file for reference and a demo application for aid in getting started. So it is easy to make it work with different versions of Delphi or C++ Builder in the future.

## Simple Example of Using Math Parser in Delphi

TbcParser installs as a component in Delphi. You can drag drop it on your Form or DataModule and then reference it just like any other component on your Form. Here is a simple event handler that uses the math parser:

procedure TForm1.Button1Click(Sender: TObject); begin bcParser1.Expression := Edit1.Text; //Get the expression user has typed. //Expression could be something like: 'SIN(X)+Y/T' //X and Y are predefined variables, though you can undefined them if need be. bcParser1.X := 5; bcParser1.Y := 4; //You can create other variables as needed: bcParser1.CreateVar('T', 400); Label1.Caption := FloatToStr(bcParser1.Value); //Display the result. end;

## OnVariableFound Event in TbcParser

Sometimes it is not possible to predefine all variables that can be used in an expression. In this case, TbcParser provides OnVariableFound event to provide values for variables that were not predefined. Here is an example event handler that returns a value for a variable named ‘A’ but reports all other variables to be invalid.

procedure TForm1.bcParser1VariableFound(Sender: TObject; const varName: String; var RetVal: NUMBER); begin if varName = 'A' then begin RetVal := 3; end else begin raise Exception.Create('unknown variable name: '+varName); end; end;

## User Defined Function in Math Parser

TbcParser comes with predefined functions that you can use, or you can remove them if you don’t want them. You can also add your own functions by writing a function in Delphi, and then giving the address of that function to TbcParser instance to associate with a name to use in a math expression. Here is an example of defining a function to use in TbcParser:

{ A user defined function that calculates the factorial of a number. Functions that take 1 parameter have this signature: function f(const x: NUMBER) : NUMBER; Functions that take 2 parameters have this signature: function f(const x, y: NUMBER) : NUMBER; Functions that take any number of parameters have this signature: function f(const x : array of NUMBER): NUMBER; Note that the verification of the parameter count for functions that take any number of parameters is done at evaluation time by the function itself, rather than during parse time. For example if SIN(1,2,3) will fail at parse time because SIN function is defined to take 1 parameter. But a MULTIPLY(1,2,3) user defined function could throw exception at evaluation time if the number of parameters is not correct. } function factorial(const val: NUMBER) : NUMBER; begin if val <> Floor(val) then begin raise Exception.Create('Parameter to ''FACTORIAL'' function must be INTEGER.'); // Alternatively we could just floor the val and use it. end; if (val = 1) or (val = 0) then begin Result := 1; end else if val < 0 then begin raise Exception.Create('Cannot calculate factorial of negative number.'); end else begin Result := val * factorial(val -1); end; end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin bcParser1.CreateOneParamFunc('FACTORIAL', @factorial); // Now the math expression you give to TbcParser can contain FACTORIAL(x) function. end;

## Download Evaluation Version of Math Parser

TbcParser evaluation version contains already compiled dcu files for some of the Delphi versions. Older Delphi versions were compiled some years ago and may not reflect latest parser features but does encompass the basic use case. You can download evaluation version here.

## Install TbcParser as a component

After you download TbcParser evaluation version, you may install the specific bcParser.dcu or bpl that was built for your specific Delphi version. The component will only function while the IDE is running.

When you buy TbcParser you will not need dcu files, bcParser.pas will be sufficient to build and install as a component. To install as a component, in old versions of Delphi, you simply pick the bcParser.pas in install component dialog. In newer versions of Delphi, you need to build a package. To do this, create a package project, add bcParser.pas to it. Set your build targets and settings such as Win64, or Win32. Then right click package project to BUILD. Then right click to install and TbcParser will appear in the component palette under ‘Bestcode’ section.

## Delphi iOS and Android Development with TbcParser

As of version 4.0, there are actually 2 bcParser.pas files, one is for old versions of Delphi, the other is for Delphi XE series that supports upgraded APIs like generic collections, zero based strings, and iOS development. If you are going to do FMX, or iOS development, then you need to use the newer bcParser.pas file for Delphi XE. Over almost two decades the Delphi APIs have changed and were deprecated such that we felt it necessary to fork the code for to continue to support old Delphi and VCL and at the same time move forward to FMX and newer Delphi versions including iOS Development and starting with Delphi XE5 Android development. If you are migrating your application to newer Delphi versions and you already had an old version of TbcParser, the external API differences between old and new TbcParser should be minimal when you get the new version.

## Purchasing TbcParser Formula Parser Component

You can pay with credit card and download TbcParser Delphi component *immediately* from our **online store** for only $29.95. Full version includes Pascal source code. Upgrades are free for registered users.

Licensing is per developer. You can deploy the the component **royalty free with your applications as many times as you want.** Site license allows any number of developers use the component at your development site. Site License is $290.95. Site licenses can be purchased here.