Advanced Features

Manipulating expressions

It is possible to manipulate expressions by manipulating the corresponding expression tree created by JEP. The parse() and getLastRootNode() method can be used to obtain the expression tree of an expression. This will be useful if you want to do more than just evaluate the expressions you parse. For example, you may want to determine the derivative of an expression. In order to be able to this, you need direct access to the expression tree.

The expression tree consists of nodes. Each of the nodes in the parse tree is an object of one of the following types:

All of them extend the SimpleNode class (which implements the Node interface). Binary operators (+,-,*,/...) and functions are ASTOpNodes. The type of operator (the function class) is stored in the opID member while the function class is stored in the pfmc member. Use the getOperator() and getPFMC() methods to access these members.

To traverse the expression tree you can use a visitor class (ParserDumpVisitor is an example class used to print out all the nodes). Look at the StandardEvaluator class to see how expressions are evaluated using the Visitor design pattern.

top