Adding operators to the parser


To add an operator to the parser several steps are needed:

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.