By editing the ASTFunctionNode file it is also relatively easy to allow references to other functions. This provides an easy mechanism to extend the range to the parser e.g. have a tcl(...) function that passes the string to a Tcl interpreter and retuns the result.
Functional if is supported via the following syntax: (boolean) ? (value1) : (value2)
Extensibility is also supported by allowing method calls on the Tokens, the syntax is (value1).method(comma separated arguments)
JavaCC by itself simply generates a file (and support files) that allow an input to be parsed, it does not return a parse tree. For the purposes of type checking we require a parse tree, and this is obtained using JJTree, a preprocessor for JavaCC.
JJtree operates by annotating the grammar file to support the generation of the parse tree. Thus the process is
The parser can also be passed a symbol table of ptolemy.data.expr.Variables which the expression to be parsed can reference.
Anything between quotes(") or apostrophes(') is taken to be one string. Strings are allowed to contain newlines or carriage returns. In addition, these characters, as well as other special characters, can be escaped using the standard Java syntax (\n, \t, \077, etc.).
The expressions recognized follow as close as possible the syntax of Java. In particular the operator precedences implemented here follow exactly those in Java. Any type conversions that are performed are lossless. If the user wants lossy conversions, explicit casts will be necessary.
Complex number are specified by an i or j after the imaginary part of the number. Long numbers are specified by an l or L after an integer number.
Users can register constants with the parser and also register classes where functions that may be called are defined. For a more thorough description of what the Parser is designed to do, please consult the Ptolemy II design document (or contact nsmyth@eecs)
Note that the parsers created by JavaCC generally have quite a bit of internal state. On the other hand, the parse trees generated by this parser are much smaller. It is also fairly cheap to traverse a parse tree in order to evaluate it. Thus it is usually preferable to cache parse trees and create a new parser when the cached parse tree becomes invalid. @author Neil Smyth, Steve Neuendorffer @version $Id: PtParser.java,v 1.143.4.1 2008/03/25 22:31:39 cxh Exp $ @since Ptolemy II 1.0 @Pt.ProposedRating Yellow (nsmyth) @Pt.AcceptedRating Yellow (yuhong) @see ptolemy.data.expr.ASTPtBitwiseNode @see ptolemy.data.expr.ASTPtFunctionApplicationNode @see ptolemy.data.expr.ASTPtFunctionDefinitionNode @see ptolemy.data.expr.ASTPtFunctionalIfNode @see ptolemy.data.expr.ASTPtLeafNode @see ptolemy.data.expr.ASTPtLogicalNode @see ptolemy.data.expr.ASTPtMethodCallNode @see ptolemy.data.expr.ASTPtProductNode @see ptolemy.data.expr.ASTPtRelationalNode @see ptolemy.data.expr.ASTPtRootNode @see ptolemy.data.expr.ASTPtSumNode @see ptolemy.data.expr.ASTPtUnaryNode @see ptolemy.data.Token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|