Simple yet fancy infix-to-postfix parser for mathematical expressions. :bowtie:
Java Shell
Clone or download
Latest commit 502e0c7 Jul 27, 2018

README.md

Parsington

Parsington is an infix-to-postfix (or infix-to-syntax-tree) expression parser for mathematical expressions written in Java. It is simple yet fancy, handling (customizable) operators, functions, variables and constants in a similar way to what the Java language itself supports.

Parsington is part of the SciJava project for scientific computing in Java.

Rationale

Expression parsers are as old as the hills; what makes this one different?

  • No dependencies.
  • Available on Maven Central.
  • Permissive BSD-2 license. See LICENSE.txt.
  • Separation of concerns. Parsington is a parser, not an evaluator. Once you have the postfix queue and/or syntax tree, what you do with it is your business (though there is a small evaluation API in the eval subpackage if that appeals to you). In general, there is no assumption that your variables will consist of any particular data type, numerical or otherwise.
  • Clean, well-commented codebase with unit tests. 'Nuff said!

History

The ImageJ Ops project needed an expression parser so that it could be more awesome. But not one limited to primitive doubles, or one that conflated parsing with evaluation, or one licensed in a restrictive way. Just a simple infix parser: a nice shunting yard implementation, or maybe some lovely recursive descent. Something on GitHub, with no dependencies, available on Maven Central. But surprisingly, there was only tumbleweed. So Parsington was born, and all our problems are now over!

Usage

In your POM <dependencies>:

<dependency>
  <groupId>org.scijava</groupId>
  <artifactId>parsington</artifactId>
  <version>1.0.3</version>
</dependency>

Postfix queues

To parse an infix expression to a postfix queue:

LinkedList<Object> queue = new ExpressionParser().parsePostfix("a+b*c^f(1,2)'");
// queue = [a, b, c, f, 1, 2, (2), <Fn>, ^, ', *, +]

Syntax trees

To parse an infix expression to a syntax tree:

SyntaxTree tree = new ExpressionParser().parseTree("a+b*c^f(1,2)'");
       +-------+
       |   +   |
       +---+---+
           |
    +------+------+
    |             |
+---+---+     +---+---+
|   a   |     |   *   |
+-------+     +---+---+
                  |
           +------+------+
           |             |
       +---+---+     +---+---+
       |   b   |     |   '   |
       +-------+     +---+---+
                         |
                         |
                     +---+---+
                     |   ^   |
                     +---+---+
                         |
                  +------+------+
                  |             |
              +---+---+     +---+---+
              |   c   |     | <Fn>  |
              +-------+     +---+---+
                                |
                         +------+------+
                         |             |
                     +---+---+     +---+---+
                     |   f   |     |  (2)  |
                     +-------+     +---+---+
                                       |
                                +------+------+
                                |             |
                            +---+---+     +---+---+
                            |   1   |     |   2   |
                            +-------+     +-------+

Evaluation

To evaluate an expression involving basic types:

Object result = new DefaultEvaluator().evaluate("6.5*7.8^2.3");

Interactive console

There is also an interactive console shell you can play with.

Run it easily using jrun:

jrun org.scijava:parsington

Or run from source, after cloning this repository:

mvn
java -jar target/parsington-*-SNAPSHOT.jar

Simple example invocations with the console's default evaluator:

> 6.5*7.8^2.3
732.3706691398969 : java.lang.Double
> 2*3,4
      ^
Misplaced separator or mismatched groups at index 4

The postfix built-in function lets you introspect a parsed postfix queue:

> postfix('6.5*7.8^2.3')
6.5 : java.lang.Double
7.8 : java.lang.Double
2.3 : java.lang.Double
^ : org.scijava.parse.Operator
* : org.scijava.parse.Operator
> postfix('[1, 2f, 3d, 4., 5L, 123456789987654321, 9987654321234567899]')
1 : java.lang.Integer
2.0 : java.lang.Float
3.0 : java.lang.Double
4.0 : java.lang.Double
5 : java.lang.Long
123456789987654321 : java.lang.Long
9987654321234567899 : java.math.BigInteger
[7] : org.scijava.parse.Group
> postfix('f(x, y) = x*y')
f : org.scijava.parse.Variable
x : org.scijava.parse.Variable
y : org.scijava.parse.Variable
(2) : org.scijava.parse.Group
<Fn> : org.scijava.parse.Function
x : org.scijava.parse.Variable
y : org.scijava.parse.Variable
* : org.scijava.parse.Operator
= : org.scijava.parse.Operator
> postfix('math.pow(q) = q^q')
math : org.scijava.parse.Variable
pow : org.scijava.parse.Variable
. : org.scijava.parse.Operator
q : org.scijava.parse.Variable
(1) : org.scijava.parse.Group
<Fn> : org.scijava.parse.Function
q : org.scijava.parse.Variable
q : org.scijava.parse.Variable
^ : org.scijava.parse.Operator
= : org.scijava.parse.Operator

The tree function is another way to introspect, in syntax tree form:

> tree('math.pow(q) = q^q')
 '='
 - '<Fn>'
  -- '.'
   --- 'math'
   --- 'pow'
  -- '(1)'
   --- 'q'
 - '^'
  -- 'q'
  -- 'q'
 : org.scijava.parse.SyntaxTree