edu.neu.ccs.parser
Class JPTParser

java.lang.Object
  extended byedu.neu.ccs.parser.BaseParser
      extended byedu.neu.ccs.parser.JPTParser
All Implemented Interfaces:
Parser

public class JPTParser
extends BaseParser

Parses and evaulates data Strings using simple expression evaluation.

The grammar used in this parsing is similar to that defined within the Java Language Specification by James Gosling, Bill Joy, and Guy Steele but as of 2.5.0 we have made significant additions to aid the user of these facilities in interactive mode.

As of 2.5.0, the parser algorithms have all been refactored down to the new base class called BaseParser. The purpose of this class is therefore to define and install constants, operations, and functions. Each such entity is now public static final so they may be utilized by some other class that extends BaseParser if so desired.

The constants, functions, and operations are all commented individually. Summary tables of the constants, functions, and operations are provided immediately below and repeated in the comments for the methods addConstants(), addFunctions(), and addOperations(). The summary tables for operations show precedence.

This class now provides an exponentiation operator denoted ^ that attempts exponentiation as an integral computation if possible. This is in contrast to the power function that always uses Math.pow in its implementation. By choosing ^ or power, the user may select implementation.

The function definitions continue to throw exceptions if given incorrect arguments but will no longer throw exceptions if mathematical errors occur. Rather the computation proceeds as in Java itself to produce either an infinity or NaN.

In 2.6.0, added hyperbolic functions.

Constant definitions:

Identifier Type Definition
true XBoolean true
false XBoolean false
pi XDouble Math.PI
e XDouble Math.E
MaxInt XInt Integer.MAX_VALUE
maxint XInt Integer.MAX_VALUE
MinInt XInt Integer.MIN_VALUE
minint XInt Integer.MIN_VALUE
MaxLong XLong Long.MAX_VALUE
maxlong XLong Long.MAX_VALUE
MinLong XLong Long.MIN_VALUE
minlong XLong Long.MIN_VALUE
MaxDouble XDouble Double.MAX_VALUE
maxdouble XDouble Double.MAX_VALUE
MinDouble XDouble Double.MIN_VALUE
mindouble XDouble Double.MIN_VALUE
Infinity XDouble Double.POSITIVE_INFINITY
infinity XDouble Double.POSITIVE_INFINITY
NaN XDouble Double.NaN
nan XDouble Double.NaN

Function definitions:

Function Name Typical Usage
abs abs(x)
ceiling ceiling(x)
floor floor(x)
round round(x)
max max(x,y)
min min(x,y)
sqrt sqrt(x)
power power(x,y)
root root(x,y)
todegrees todegrees(theta)
toradians toradians(angle)
sin sin(theta)
sindeg sindeg(angle)
cos cos(theta)
cosdeg cosdeg(angle)
tan tan(theta)
tandeg tandeg(angle)
asin asin(s)
asindeg asindeg(s)
acos acos(c)
acosdeg acosdeg(c)
atan atan(t)
atandeg(t) atandeg(t)
atan2 atan2(y,x)
atan2deg atan2deg(y,x)
exp exp(x)
log log(x)
ln ln(x)
log2 log2(x)
log10 log10(x)
logtobase logtobase(x,b)
sinh sinh(x)
cosh cosh(x)
tanh tanh(x)
asinh asinh(x)
acosh acosh(x)
atanh atanh(x)

In the above table:
    x and y stand for generic variables;
    theta is an angle in radians;
    angle is an angle in degrees;
     s, c, t stand for possible values of sin, cos, tan;
    and b is the base for a logarithm.

In 2.5.0, random was removed from the list of functions because as implemented in the class AbstractFunction a function has a fixed number of arguments. Instead, random is now implemented directly in BaseParser to take one of three forms with the following interpretations.

Function Name Typical Usage Interpretation
random random() Random number between 0 and 1
random random(x) Random number between 0 and x
random random(x,y) Random number between x and y

Operation definitions:

Precedence Operations Symbols
0 IDENTITY None
1 operationOR ||
2 operationAND &&
3 operationEQ
operationNE
==
!=
4 operationLT
operationGT
operationLE
operationGE
<
>
<=
>=
5 operationPlus
operationMinus
+
-
6 operationTimes
operationSlash
operationPercent
*
/
%
7 operationCaret ^
8 operationNOT !

Since:
1.0
Version:
2.6.0
See Also:
Java Language Specification

Field Summary
static AbstractFunction functionAbs
          AbstractFunction to implement abs.
static AbstractFunction functionACos
          AbstractFunction to implement acos based on Math.acos.
static AbstractFunction functionACosDeg
          AbstractFunction to implement acosdeg based on Math.acos and appropriate radians to degrees conversion.
static AbstractFunction functionACosh
          AbstractFunction to implement acosh.
static AbstractFunction functionASin
          AbstractFunction to implement asin based on Math.asin.
static AbstractFunction functionASinDeg
          AbstractFunction to implement asindeg based on Math.asin and appropriate radians to degrees conversion.
static AbstractFunction functionASinh
          AbstractFunction to implement asinh.
static AbstractFunction functionATan
          AbstractFunction to implement atan based on Math.atan.
static AbstractFunction functionATan2
          AbstractFunction to implement atan2 based on Math.atan2.
static AbstractFunction functionATan2Deg
          AbstractFunction to implement atan2deg based on Math.atan2 and appropriate radians to degrees conversion.
static AbstractFunction functionATanDeg
          AbstractFunction to implement atandeg based on Math.atan and appropriate radians to degrees conversion.
static AbstractFunction functionATanh
          AbstractFunction to implement atanh.
static AbstractFunction functionCeiling
          AbstractFunction to implement ceiling.
static AbstractFunction functionCos
          AbstractFunction to implement cos based on Math.cos.
static AbstractFunction functionCosDeg
          AbstractFunction to implement cosdeg based on Math.cos and appropriate degrees to radians conversion.
static AbstractFunction functionCosh
          AbstractFunction to implement cosh.
static AbstractFunction functionExp
          AbstractFunction to implement exp based on Math.exp.
static AbstractFunction functionFloor
          AbstractFunction to implement floor.
static AbstractFunction functionLn
          AbstractFunction to implement ln based on Math.log.
static AbstractFunction functionLog
          AbstractFunction to implement log based on Math.log.
static AbstractFunction functionLog10
          AbstractFunction to implement log10.
static AbstractFunction functionLog2
          AbstractFunction to implement log2.
static AbstractFunction functionLogToBase
          AbstractFunction to implement logtobase.
static AbstractFunction functionMax
          AbstractFunction to implement max.
static AbstractFunction functionMin
          AbstractFunction to implement min.
static AbstractFunction functionPower
          AbstractFunction to implement power based on Math.pow.
static AbstractFunction functionRoot
          AbstractFunction to implement root based on MathUtilities.root.
static AbstractFunction functionRound
          AbstractFunction to implement round.
static AbstractFunction functionSin
          AbstractFunction to implement sin based on Math.sin.
static AbstractFunction functionSinDeg
          AbstractFunction to implement sindeg based on Math.sin and appropriate degrees to radians conversion.
static AbstractFunction functionSinh
          AbstractFunction to implement sinh.
static AbstractFunction functionSqrt
          AbstractFunction to implement sqrt based on Math.sqrt.
static AbstractFunction functionTan
          AbstractFunction to implement tan based on Math.tan.
static AbstractFunction functionTanDeg
          AbstractFunction to implement tandeg based on Math.tan and appropriate degrees to radians conversion.
static AbstractFunction functionTanh
          AbstractFunction to implement tanh.
static AbstractFunction functionToDegrees
          AbstractFunction to implement todegrees based on Math.toDegrees.
static AbstractFunction functionToRadians
          AbstractFunction to implement toradians based on Math.toRadians.
static BooleanOperation operationAND
          BooleanOperation to implement "and".
static NumericOperation operationCaret
          NumericOperation to implement exponentiation.
static BooleanOperation operationEQ
          BooleanOperation to implement equals.
static BooleanOperation operationGE
          BooleanOperation to implement greater than or equals.
static BooleanOperation operationGT
          BooleanOperation to implement greater than.
static BooleanOperation operationLE
          BooleanOperation to implement less than or equals.
static BooleanOperation operationLT
          BooleanOperation to implement less than.
static NumericOperation operationMinus
          NumericOperation to implement minus.
static BooleanOperation operationNE
          BooleanOperation to implement not equals.
static BooleanOperation operationNOT
          BooleanOperation to implement "not".
static BooleanOperation operationOR
          BooleanOperation to implement "or".
static NumericOperation operationPercent
          NumericOperation to implement remainder.
static NumericOperation operationPlus
          NumericOperation to implement plus.
static NumericOperation operationSlash
          NumericOperation to implement division.
static NumericOperation operationTimes
          NumericOperation to implement times.
 
Fields inherited from class edu.neu.ccs.parser.BaseParser
ARGUMENT_LIST_END, ARGUMENT_LIST_START, ARGUMENT_SEPARATOR, ASSIGNMENT_BY_LET, ASSIGNMENT_BY_SET, data, EVAL, FLOATING, IDENTITY, IF_THEN_ELSE, INTEGRAL, NESTED_EXPRESSION_END, NESTED_EXPRESSION_START, next, OPERATION_PREFIX, RADIX_POINT, RANDOM, suspend, UNDERSCORE
 
Constructor Summary
JPTParser()
           
 
Method Summary
protected  void addConstants()
          Adds the standard constants for this parser to the environment.
protected  void addFunctions()
          Adds the standard functions for this parser to the function table.
protected  void addOperations()
          Adds the standard operations for this parser to the operation table.
 
Methods inherited from class edu.neu.ccs.parser.BaseParser
addConstant, addFunction, addOperation, addOperationAfterPrecedenceOf, addOperationAtPrecedenceOf, addOperationBeforePrecedenceOf, addReserved, afterDigits, afterSign, assignArgumentList, assignLetVariable, assignSetVariable, assignVariable, call, call, call, call, call, call, call, call, call, call, constants, evaluate, evaluateIdentifier, extractExpression, functionNames, functions, getFunction, getOperation, getValue, initializeStructures, isConstantID, isEnvironmentID, isExponentAt, isFunctionName, isLetVariableID, isOperationOrPrefix, isOperationSymbol, isOrdinaryFunctionName, isPossibleIdentifier, isPossibleOperation, isPossibleVariableID, isReservedID, isSetVariableID, isSignAt, isSimpleFunctionName, makeTable, makeTable, makeTable, makeTable, nextIdentifier, nextOperation, nextSimpleTerm, nextTerm, nextTokenIs, nextTokenIs, nextUnaryOperations, operations, operationSymbols, ordinaryFunctionNames, ordinaryFunctions, parse, parseArgumentList, parseAssignment, parseEval, parseExpression, parseExpression, parseFunctionCall, parseIdentifierExpression, parseIdentifierExpressionList, parseIfThenElse, parseNestedExpression, parseNumber, parseRandom, parseSpecialFunction, parseWithArgumentList, popContext, precedenceOf, pushContext, removeFunction, reserved, reserveID, set_variables, setArgumentSeparatorToken, setLeftParenthesisToken, setRadixPointToken, setRightParenthesisToken, simpleFunctionNames, simpleFunctions, simpleFunctionsToString, skipWhitespace, specialPush, startsIdentifier, startsNumber, stringToSimpleFunctions, throwAgainAndPop, withinIdentifier
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

functionAbs

public static final AbstractFunction functionAbs
AbstractFunction to implement abs.


functionCeiling

public static final AbstractFunction functionCeiling
AbstractFunction to implement ceiling.


functionFloor

public static final AbstractFunction functionFloor
AbstractFunction to implement floor.


functionRound

public static final AbstractFunction functionRound
AbstractFunction to implement round.


functionMax

public static final AbstractFunction functionMax
AbstractFunction to implement max.


functionMin

public static final AbstractFunction functionMin
AbstractFunction to implement min.


functionSqrt

public static final AbstractFunction functionSqrt
AbstractFunction to implement sqrt based on Math.sqrt.


functionPower

public static final AbstractFunction functionPower
AbstractFunction to implement power based on Math.pow.

See Also:
operationCaret

functionRoot

public static final AbstractFunction functionRoot
AbstractFunction to implement root based on MathUtilities.root.


functionToDegrees

public static final AbstractFunction functionToDegrees
AbstractFunction to implement todegrees based on Math.toDegrees.


functionToRadians

public static final AbstractFunction functionToRadians
AbstractFunction to implement toradians based on Math.toRadians.


functionSin

public static final AbstractFunction functionSin
AbstractFunction to implement sin based on Math.sin.


functionSinDeg

public static final AbstractFunction functionSinDeg
AbstractFunction to implement sindeg based on Math.sin and appropriate degrees to radians conversion.


functionCos

public static final AbstractFunction functionCos
AbstractFunction to implement cos based on Math.cos.


functionCosDeg

public static final AbstractFunction functionCosDeg
AbstractFunction to implement cosdeg based on Math.cos and appropriate degrees to radians conversion.


functionTan

public static final AbstractFunction functionTan
AbstractFunction to implement tan based on Math.tan.


functionTanDeg

public static final AbstractFunction functionTanDeg
AbstractFunction to implement tandeg based on Math.tan and appropriate degrees to radians conversion.


functionASin

public static final AbstractFunction functionASin
AbstractFunction to implement asin based on Math.asin.


functionASinDeg

public static final AbstractFunction functionASinDeg
AbstractFunction to implement asindeg based on Math.asin and appropriate radians to degrees conversion.


functionACos

public static final AbstractFunction functionACos
AbstractFunction to implement acos based on Math.acos.


functionACosDeg

public static final AbstractFunction functionACosDeg
AbstractFunction to implement acosdeg based on Math.acos and appropriate radians to degrees conversion.


functionATan

public static final AbstractFunction functionATan
AbstractFunction to implement atan based on Math.atan.


functionATanDeg

public static final AbstractFunction functionATanDeg
AbstractFunction to implement atandeg based on Math.atan and appropriate radians to degrees conversion.


functionATan2

public static final AbstractFunction functionATan2
AbstractFunction to implement atan2 based on Math.atan2.


functionATan2Deg

public static final AbstractFunction functionATan2Deg
AbstractFunction to implement atan2deg based on Math.atan2 and appropriate radians to degrees conversion.


functionExp

public static final AbstractFunction functionExp
AbstractFunction to implement exp based on Math.exp.


functionLog

public static final AbstractFunction functionLog

AbstractFunction to implement log based on Math.log.

This is identical to ln.


functionLn

public static final AbstractFunction functionLn

AbstractFunction to implement ln based on Math.log.

This is identical to log.


functionLog2

public static final AbstractFunction functionLog2

AbstractFunction to implement log2.

log2(x) is computed as:

Math.log(x) / Math.log(2)


functionLog10

public static final AbstractFunction functionLog10

AbstractFunction to implement log10.

log10(x) is computed as:

Math.log(x) / Math.log(10)


functionLogToBase

public static final AbstractFunction functionLogToBase

AbstractFunction to implement logtobase.

logtobase(x,b) is computed as:

Math.log(x) / Math.log(b)


functionSinh

public static final AbstractFunction functionSinh

AbstractFunction to implement sinh.

sinh(x) is computed as:

MathUtilities.sinh(x)


functionCosh

public static final AbstractFunction functionCosh

AbstractFunction to implement cosh.

cosh(x) is computed as:

MathUtilities.cosh(x)


functionTanh

public static final AbstractFunction functionTanh

AbstractFunction to implement tanh.

tanh(x) is computed as:

MathUtilities.tanh(x)


functionASinh

public static final AbstractFunction functionASinh

AbstractFunction to implement asinh.

asinh(x) is computed as:

MathUtilities.asinh(x)


functionACosh

public static final AbstractFunction functionACosh

AbstractFunction to implement acosh.

acosh(x) is computed as:

MathUtilities.acosh(x)


functionATanh

public static final AbstractFunction functionATanh

AbstractFunction to implement atanh.

atanh(x) is computed as:

MathUtilities.atanh(x)


operationPlus

public static final NumericOperation operationPlus

NumericOperation to implement plus.

Symbol: "+".

Unary and binary.


operationMinus

public static final NumericOperation operationMinus

NumericOperation to implement minus.

Symbol: "-".

Unary and binary.


operationTimes

public static final NumericOperation operationTimes

NumericOperation to implement times.

Symbol: "*".

Binary.


operationSlash

public static final NumericOperation operationSlash

NumericOperation to implement division.

Symbol: "/".

Binary.


operationPercent

public static final NumericOperation operationPercent

NumericOperation to implement remainder.

Symbol: "%".

Binary.


operationCaret

public static final NumericOperation operationCaret

NumericOperation to implement exponentiation.

Symbol: "^".

Binary.

If the arguments are integral, that is, of type XBigInteger, then an effort will be made to carry out the exponentiation in integral arithmetic.

If the arguments are not integral or if it is impossible to do an integral computation, then this method reverts to using Math.pow.

This operation is more subtle than the function functionPower introduced in this class. The power function always uses Math.pow and makes no attempt to handle integral arguments in a special way.

It is intentional that this operation and the power function operate differently. In some cases, the user may want an integral computation but in other cases if the internal BigInteger would be extremely large may actually perfer to do the computation using double precision floating point.

See Also:
functionPower

operationEQ

public static final BooleanOperation operationEQ

BooleanOperation to implement equals.

Symbol: "==".

Binary. All types.


operationNE

public static final BooleanOperation operationNE

BooleanOperation to implement not equals.

Symbol: "!=".

Binary. All types.


operationLT

public static final BooleanOperation operationLT

BooleanOperation to implement less than.

Symbol: "<".

Binary. All types.


operationGT

public static final BooleanOperation operationGT

BooleanOperation to implement greater than.

Symbol: ">".

Binary. All types.


operationLE

public static final BooleanOperation operationLE

BooleanOperation to implement less than or equals.

Symbol: "<=".

Binary. All types.


operationGE

public static final BooleanOperation operationGE

BooleanOperation to implement greater than or equals.

Symbol: ">=".

Binary. All types.


operationAND

public static final BooleanOperation operationAND

BooleanOperation to implement "and".

Symbol: "&&".

Binary. Boolean only.


operationOR

public static final BooleanOperation operationOR

BooleanOperation to implement "or".

Symbol: "||".

Binary. Boolean only.


operationNOT

public static final BooleanOperation operationNOT

BooleanOperation to implement "not".

Symbol: "!".

Binary. Boolean only.

Constructor Detail

JPTParser

public JPTParser()
Method Detail

addConstants

protected void addConstants()

Adds the standard constants for this parser to the environment.

Identifier Type Definition
true XBoolean true
false XBoolean false
pi XDouble Math.PI
e XDouble Math.E
MaxInt XInt Integer.MAX_VALUE
maxint XInt Integer.MAX_VALUE
MinInt XInt Integer.MIN_VALUE
minint XInt Integer.MIN_VALUE
MaxLong XLong Long.MAX_VALUE
maxlong XLong Long.MAX_VALUE
MinLong XLong Long.MIN_VALUE
minlong XLong Long.MIN_VALUE
MaxDouble XDouble Double.MAX_VALUE
maxdouble XDouble Double.MAX_VALUE
MinDouble XDouble Double.MIN_VALUE
mindouble XDouble Double.MIN_VALUE
Infinity XDouble Double.POSITIVE_INFINITY
infinity XDouble Double.POSITIVE_INFINITY
NaN XDouble Double.NaN
nan XDouble Double.NaN

Overrides:
addConstants in class BaseParser

addFunctions

protected void addFunctions()

Adds the standard functions for this parser to the function table.

Function Name Typical Usage
abs abs(x)
ceiling ceiling(x)
floor floor(x)
round round(x)
max max(x,y)
min min(x,y)
sqrt sqrt(x)
power power(x,y)
root root(x,y)
todegrees todegrees(theta)
toradians toradians(angle)
sin sin(theta)
sindeg sindeg(angle)
cos cos(theta)
cosdeg cosdeg(angle)
tan tan(theta)
tandeg tandeg(angle)
asin asin(s)
asindeg asindeg(s)
acos acos(c)
acosdeg acosdeg(c)
atan atan(t)
atandeg(t) atandeg(t)
atan2 atan2(y,x)
atan2deg atan2deg(y,x)
exp exp(x)
log log(x)
ln ln(x)
log2 log2(x)
log10 log10(x)
logtobase logtobase(x,b)
sinh sinh(x)
cosh cosh(x)
tanh tanh(x)
asinh asinh(x)
acosh acosh(x)
atanh atanh(x)

In the above table:
    x and y stand for generic variables;
    theta is an angle in radians;
    angle is an angle in degrees;
     s, c, t stand for possible values of sin, cos, tan;
    and b is the base for a logarithm.

In 2.5.0, random was removed from the list of functions because as implemented in the class AbstractFunction a function has a fixed number of arguments. Instead, random is now implemented directly in BaseParser to take one of three forms with the following interpretations.

Function Name Typical Usage Interpretation
random random() Random number between 0 and 1
random random(x) Random number between 0 and x
random random(x,y) Random number between x and y

Overrides:
addFunctions in class BaseParser

addOperations

protected void addOperations()

Adds the standard operations for this parser to the operation table.

Except for the introduction of the ^ operation, follows Arnold, Gosling, Holmes:

The Java Programming Language, Third Edition, 6.10

Precedence Operations Symbols
0 IDENTITY None
1 operationOR ||
2 operationAND &&
3 operationEQ
operationNE
==
!=
4 operationLT
operationGT
operationLE
operationGE
<
>
<=
>=
5 operationPlus
operationMinus
+
-
6 operationTimes
operationSlash
operationPercent
*
/
%
7 operationCaret ^
8 operationNOT !

Overrides:
addOperations in class BaseParser
See Also:
operationCaret