Skip to content

About

a library for parsing a text representation of a mathematical expression that is written in terms of x

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

bb4-expression

📊 Build status for all bb4 projects

This library project for parsing mathematical expressions.

Language (informal grammar)

MathExpressionParser accepts a single indeterminate x, decimal literals, parentheses, the binary operators + - * / ^, optional whitespace, and implicit multiplication (e.g. 2x, 2(x+1), (x+1)x). Unary - is part of an atom (tighter than ^): -x, -2x, and -(x+1) each become one operand, so x^-x is x^(-x) and -x^2 is (-x)^2. A positive coefficient like 2x is juxtaposition (tighter than explicit *//, looser than ^), so 2x/3x means (2x)/(3x) and 2x^2 means 2*(x^2). Other letters are rejected.

expr          = sum ;
sum           = product { ( '+' | '-' ) product } ;
product       = juxtaposition { ( '*' | '/' ) juxtaposition } ;
juxtaposition = power { power } ;   (* implicit multiply binds tighter than * / : 2x, 2(x+1), (x+1)x *)
power         = atom { '^' atom } ;   (* note: '^' is left-associ here, not right-assoc *)
atom          = number | 'x' | '(' expr ')' | '-' atom ;
number        = digit { digit } [ '.' digit { digit } ] ;

Parse failures are thrown as com.barrybecker4.expression.ParseError (an IllegalArgumentException with offset into the trimmed source). Use com.barrybecker4.expression.ExprEvaluator to evaluate a parsed Expr for a numeric x.

To use, include this dependency in your gradle file

implementation 'com.barrybecker4:bb4-expression:<version>'

If you have not already done so, first install Git and Intellij. You also need to have java SDK installed and set the JAVA_HOME environment variable to point to the location where it is installed.

Type 'gradlew build' at the root (prepend with ./ if running on Cygwin or Linux). This will build everything, but since it is a library project there won't be much to see.

When there is a new release, versioned artifacts will be published by Barry Becker to Sonatype.

License

All source (unless otherwise specified in individual file) is provided under the MIT License

About

a library for parsing a text representation of a mathematical expression that is written in terms of x

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages