Parsing and Evaluating Mathematical Expressions in Object Pascal
July 13th, 2016
Update: March 13th, 2017

The pdf file Parsing and Evaluating Mathematical Expression in Object Pascal contains a description of the source code for the one pass mathematical expression parser written for Free Pascal. As with the Delphi version, the parser in a sequence of four increasingly more complex set of units. While there is no important change to the original parser written for Delphi, the Free Pascal code contains some innovations and is hopefully better. By the same token, I hope the documentation will be better. The original parser and its documentation remain available.

The source code is available in four increasingly complex versions of the parser:

  1. Parsing a Simplified Grammar
  2. More Mathematical Operations and Comments
  3. Adding Constants, Functions and Variables
  4. Extended National Language Support

Finally, the units making up the one pass parser are consolidated into a package ( suitable for integration into the Lazarus IDE. Note that this is a runtime package only, no components are installed in the IDE's palette.


Running the tcalc demonstration program, I noticed that it displayed a Gtk warning:

michel@hp:~/Documents/Lazarus/simpleparser/tcalc$ ./tcalc (tcalc:4751): Gtk-WARNING **: Impossible de trouver le moteur de thème dans module_path : « pixmap » Utilisation : tcalc [(--LANG|-l) ln] ( -? identificateur | --Help | "expression" )"

That seemed strange as tcalc is a console application, which does not use a graphic library such as Gtk. Furthermore, that warning did not appear in the previous version of tcalc.

The problem was easy enough to identify because the most significant change in the latest version is the addition of national language support. Implicitly SetDefaultLang found in LCLTranslator (used in DefaultTranslator) switches between translation files at program startup. It turns out that the Interfaces unit is needed and it pulls in the entire LCL package. Without Interfaces the code does not compile. And with it, it will not compile using the LCLBase package instead of the LCL package. Turns out that SetDefaultLang wants to translate all of the forms in the project. There are none in tcalc but the unit does not know that.

So I decided to rip out the form updating code and to save that modified version in a unit called UnitTranslator. It does not require Interfaces and the LCL can be replaced with the LCLBase package. I have explained in more details the changes made. If you are interested, go here.

While at it, I cleaned up the archives, removing some unnecessary files. Aside from the above changes to tcalc there is no substantive change in the downloadable archives.