CogPar: A Versatile Parser for Mathematical Expression in Java

CogPar is lightweight but versatile parser for mathematical expressions.It can be used to analyse expressions and store them in an internal data structure for later evaluation. Repeated evaluation of the same expression using CogPar is fast.

CogPar comes with a highly configurable tokenizer which can be adapted for your own needs.

Arbitrary named variables are supported and values can be assigned in a single line of code.

The parser, it’s grammar an the tokenizer are well documented. You can read more about the internal workings of CogPar in these posts.

CogPar is distributed under the MIT license, so feel free to use it in your own projects.

You can download CogPar from Github.
Current release version 1.0.1

The code reference for CogPar can be found here.

CogPar in Java Applications

CogPar is currently used in the following applications:

Android Applications

  • DiffIt!: An Android app for calculating and explaining derivatives of mathematical expressions.
  • Tewter Essential Algebra: A GCSE training app for Android for the revision of algebra. Questions are generated randomly. The parser is used to parse the clear text input of the user and compare it to the correct result of the question.

If you have any applications that use CogPar and that you would like to be showcased here, please leave a comment.

Changes in version 1.0.1

  • Fixed additional tokens bug.
    The parser will now throw an exception if there are tokens left in the input when parsing is complete.
  • Modified grammar to be more in line with other applications.
    The grammar will now accept only simple values or bracketed expressions as function arguments. The previous version would interpret sin(x)^2 as sin(x^2). In this version it will be interpreted as (sin(x))^2. This also means that the ambiguous expression sin x^2 will be interpreted as (sin(x))^2.
 

2 Responses to CogPar: A Versatile Parser for Mathematical Expressions in Java

  1. Andrew Webb says:

    Nice software.

    If anyone is interested there is an expression parser in C++ over at this link.

    http://www.technical-recipes.com/2011/a-mathematical-expression-parser-in-java-and-cpp/

    Comments and suggestions welcome.

  2. Christos Kalonakis says:

    If anyone wants to use it for complex numbers, here is a forked version of the aforementioned project https://github.com/hrkalona/cogitolearning-examples/tree/CogPar_Complex

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>