# stefanhaustein/expressionparser

Simple single-file configurable Java parser for mathematical expressions
Java
Switch branches/tags
Nothing to show
Latest commit aa28629 Mar 6, 2018
 Failed to load latest commit information. core Mar 5, 2018 demo Mar 5, 2018 gradle/wrapper Feb 27, 2018 .gitignore May 12, 2017 LICENSE Mar 13, 2016 README.md May 24, 2017 build.gradle Feb 27, 2018 gradlew Feb 27, 2018 gradlew.bat May 12, 2017 settings.gradle May 12, 2017

A simple single-file configurable Java parser for mathematical expressions.

# Gradle Build Integration

Jitpack for the win!

Step 1: Add jitpack to your root build.gradle at the end of repositories:

allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}

Step 2: Add the HtmlView2 dependency

dependencies {
compile 'com.github.stefanhaustein.expressionparser:core:v1.0.0'
}

# Examples and Demos

## Immediate evaluation

Calculator.java in the demo package contains a simple self-contained use case directly interpreting the input.

The parser configuration supports simple mathematical expressions, and the processor just evaluates them immediately, without constructing an intermediate tree representation.

Expression? 5+2*-2^3^2
Result:     -1019.0

SetDemo.java is similar to the calculator demo, but illustrates the flexibility of the expression parser with a slightly more "atypical" expression language.

Example output from [SetDemo.java]:

Operators: ∩ ∪ ∖
Expression? | {A, B, B, C}|
Result:     3
Expression? {1, 2, 3} ∪ {3, 4, 5}
Result:     {1.0, 2.0, 3.0, 4.0, 5.0}
Expression? {1, 2} ∩ {2, 3}
Result:     {2.0}
Expression? | {A, B, C} \ {A, X, Y} |
Result:     2

## Tree building

TreeBuilder.java shows how to builds a tree from the input (using a node factory. The corresponding demo app is able to do simplifications and to compute the symbolic derivative. An extended tokenizer translates superscript digits.

Input?  derive(1/x, x)

Parsed: derive(1/x, x)

⎛1   ⎞
⎝x   ⎠

(-derive(x, x))     ⎪
Equals: ───────────────     ⎪ Reciprocal rule
x²            ⎪

(-1)
Equals: ────
x²

-1
Equals: ──
x²

Flat:   -1/x²

## Integration with a "main" parser

The BASIC demo parser is able to parse 70's BASIC programs. The rest of the BASIC demo directory contains some code to run them.

**** EXPRESSION PARSER BASIC DEMO V1 ****

251392K SYSTEM  252056464 BASIC BYTES FREE

print "Hello World"
Hello World

10 print "Hello World"
list

10 PRINT "Hello World"

run
Hello World

run
,------*------,
,-------------   '---  ------'
'-------- --'      / /
,---' '-------/ /--,
'----------------'
THE USS ENTERPRISE --- NCC-1701
YOUR ORDERS ARE AS FOLLOWS:
DESTROY THE 14 KLINGON WARSHIPS WHICH HAVE INVADED
THE GALAXY BEFORE THEY CAN ATTACK FEDERATION HEADQUARTERS
ON STARDATE 2328  THIS GIVES YOU 28 DAYS.  THERE ARE
4 STARBASES IN THE GALAXY FOR RESUPPLYING YOUR SHIP
YOUR MISSION BEGINS WITH YOUR STARSHIP LOCATED
IN THE GALACTIC QUADRANT, 'ALTAIR I'.
COMBAT AREA      CONDITION RED
SHIELDS DANGEROUSLY LOW
---------------------------------
STARDATE          2300
*                             CONDITION          *RED*