JEP - Java Math Expression Parser

- JavaDoc

# JEP

- Basic Usage
- Variables
- Data types
- Operators
- Functions
- Advanced Features
- Grammar
- FAQ
- Version History

# Extensions

- Overview
- XJep
- Differentiation
- Vectors and Matrices
- Groups
- Version History

# Operators

## Built in operators

All common arithmetic operators are supported. Boolean operators are also fully supported. Boolean expressions are evaluated to be either 1 or 0 (true or false respectively).

An indicates that the operator can be used with the specific type of variable. Refer to the grammar for detailed information about operator precedence.

|  | Double | Complex | String | Vector
Power | ^ |  |  |  |
Boolean Not | ! |  |  |  |
Unary Plus, Unary Minus | +x, -x |  |  |  |
Modulus | % |  |  |  |
Division | / |  |  |  |
Multiplication | * |  |  |  |
Addition, Subtraction | +, - |  |  | (only +) |
Less or Equal, More or Equal | <=, >= |  |  |  |
Less Than, Greater Than | <, > |  |  |  |
Not Equal, Equal | !=, == |  |  |  |
Boolean And | && |  |  |  |
Boolean Or | || |  |  |  |

## Adding operators

Adding operators is currently only possible by modifying the JEP source code. To add an operator to the parser several steps are needed:

- Create a new token with the descriptive name AT, say, and a string representing the operator.
- Modify the grammer rules to include a rule for the new operator
- Create a new object implementing org.nfunk.jep.function.PostfixMathCommandI to provide methods for evaluating the operator.
- Modify or sub class org.nfunk.jep.OperatorSet to bind the operator in the parser to PostfixMathCommand

Before atempting to add an operator you should be familiar with adding a custom function to JEP.

### JavaCC The parser generator

Rather than writing the parser by hand in java the parser is created using the JavaCC parser/scanner generator. This reads a file Parser.jjt which is written in a special language which defines the grammer and creates Parser.java and some other java files which implement the parser.

You should read some of the documentation on JavaCC and JJTree before attempting to modify the parser.

There is a three step process used to generate the parser.

1. JJTree is run. This reads the file Parser.jjt and creates a file Parser.jj. The purpose of this step is to add code needed to handle Abstract Systax Trees which are tree structures used represent a mathematical expression. As well as creating parser.jj it also creates some other java files: Node.java which represents the basic node in the parser; ASTConstant.java a node representing a constant value "0" or "1", ASTVarNode.java a node representing a variable, ASTFunNode.java a node representing a function or operator.
2. javacc is run. This reads Parser.jj and creates Parser.java the actual code for implementing the Parser. Parser.java is a very complicated file with nearly 2000 lines of code.
3. Normal java comilation step which compiles Parser.java.

This process should automatically be caried out when the project is built using the ANT build.xml file. It is not sufficient to simple recompile all the java files.

Only the Parser.jjt file should be modified, Parser.jj, Parser.java should not be modified as they will be overwritten during the build process. Furthermore ASTConstant.java, ASTFunNode.java, ASTStart.java, ASTVarNode.java, JavaCharStream.java, JJTParserState.java, Node.java, SimpleNode.java, ParseException.java, ParserConstants.java, ParserTokenManager.java, ParseTreeConstants.java, Token.java, TokenMgrError.java should not normally be modified as these are also automatically generated.