edu.neu.ccs.parser
Class JPTParser

java.lang.Object
  |
  +--edu.neu.ccs.parser.AbstractParser
        |
        +--edu.neu.ccs.parser.JPTParser
All Implemented Interfaces:
Parser

public class JPTParser
extends AbstractParser

Parses and evaulates data Strings using simple expression evaluation.

Grammar notation used in this specification is as defined within the Java Language Specification by James Gosling, Bill Joy, and Guy Steele, that is referenced below. This language specification is based largely on the Java Language Specification.

The characters of the parser input String are considered to be a sequence of input elements, which are white space and tokens. The tokens are the identifiers, numbers, separators, and operations of the syntactic grammar specified below.

The lexical grammar for the language defined by this parser is as follows:

Input:
InputElements
InputElements:
InputElement InputElementsopt
InputElement:
Whitespace
Token

White space is defined as the ASCII space, horizontal tab, form feed, line feed, and return characters. White space is required to separate tokens except for situations where a separator is not needed to distinguish separate tokens.

Whitespace:
The ASCII SP character, aka "space"
The ASCII HT character, aka "horizontal tab"
The ASCII FF character, aka "form feed"
The ASCII LF character, aka "line feed"
The ASCII CR character, aka "return"
Token:
Identifier
Number
Operation
Separator

An identifier is an unlimited-length sequence of identifier characters that must begin with a letter. Identifiers denote constants and procedures.

Identifier:
Letter IdentifierCharsopt
IdentifierChars:
IdentifierChar IdentifierCharsopt
IdentifierChar:
Letter
Digit
The ASCII '_' character, aka "underscore"
Letter:
An ASCII character whose code is in the range 65-90
An ASCII character whose code is in the range 97-122

A number is an integer or real number, as defined below.

All integers are represented internally as type BigInteger encapsulated in XBigInteger.

All real numbers are represented internally as type double encapsulated in XDouble.

Computations will be maintained in BigInteger format as long as possible. If the arguments of an arithmetic operation are both BigInteger then the result will be computed as BigInteger. If at least one of the arguments is double then both arguments will be forced to double and the computation will be done in double. Functions that work only with double will force the computation to double.

It is possible in the course of computation that a number classified as a real number below could be an integer value but be represented by a double. Note that this conversion can entail a loss of digits. Indeed, large values of type long cannot in Java be represented with full accurary in type double.

Note that this parser does not handle BigDecimal as a type. Any data entered as a BigDecimal will be interpreted as double. The reason for this design is that the mathematical functions supported in the parser are not available for BigDecimal in the Java libraries. As a consequence, the fromStringData method of the XBigDecimal does not use this parser and does not support any form of arithmetic expression evaluation.

Number:
Digits
RealNumber
Digits:
Digit Digitsopt
Digit one of:
0 1 2 3 4 5 6 7 8 9
RealNumber:
Digits RadixPoint Digitsopt Exponentopt
RadixPoint Digitsopt Exponentopt
Digits Exponent
RadixPoint:
The stored "radix point" token
Exponent:
ExponentIndicator Signopt Digits
Sign one of:
+ -

The following tokens are operations in this language.

Operation one of:
+ - * / %
== != < > <= >= && || !

The following tokens are separators in this language.

Separator one of:
The ASCII '(' character, aka "left parenthesis"
The ASCII ')' character, aka "right parenthesis"
The stored "argument list start" token
The stored "argument separator" token
The stored "argument list end" token

The syntactical grammar for the language defined by this parser is as follows:

Expression:
NumericExpression

Since this is largely a numeric evaluation language, all values are considered to be numeric values in the specification below. This parser does support boolean values and boolean expressions which are handled in the standard fashion.

NumericExpression:
( NumericExpression )
UnaryOperation NumericExpression
NumericExpression BinaryOperation NumericExpression
ProceduralExpression
Number
Constant

As stated above, all arithmetic operations are carried out either as BigInteger or double.

Overflow is extremely unlikely in BigInteger. Overflow when using double is handled by the use of the IEEE values +Infinity, -Infinity, and Nan.

UnaryOperation one of:
! + -
BinaryOperation:
Operation but not !

The following six procedures will maintain the internal data type as BigInteger or double: abs, cieling, floor, round, max, min.

All other procedures are calculated using double values.

ProceduralExpression:
ProcedureIdentifier ArgumentList
ProcedureIdentifier one of:
name usage explanation
abs abs(x) absolute value of x
ceiling ceiling(x) smallest integral i >= x
floor floor(x) largest integral i <= x
round round(x) nearest integral i to x
max max(x, y) maximum of x and y
min min(x, y) minimum of x and y
sqrt sqrt(x) square root of x for x >= 0
power power(x, y) x to the power y for x > 0 or x == 0 with y > 0
todegrees todegrees(x) convert radians to degrees
toradians toradians(x) convert degrees to radians
sin sin(x) sine of x for x in radians
sindeg sindeg(x) sine of x for x in degrees
cos cos(x) cosine of x for x in radians
cosdeg cosdeg(x) cosine of x for x in degrees
tan tan(x) tangent of x for x in radians
tandeg tandeg(x) tangent of x for x in degrees
asin asin(x) arcsine in radians of x for -1 <= x <= 1
asindeg asindeg(x) arcsine in degrees of x for -1 <= x <= 1
acos acos(x) arccosine in radians of x for -1 <= x <= 1
acosdeg acosdeg(x) arccosine in degrees of x for -1 <= x <= 1
atan atan(x) arctangent in radians of x
atandeg atandeg(x) arctangent in degrees of x
atan2 atan2(y, x) arctangent in radians of point (x, y)
atan2deg atan2deg(y, x) arctangent in degrees of point (x, y)
exp exp(x) e to the power x for e = 2.718...
log log(x) natural log of x for x > 0
ln ln(x) synomym for natural log of x for x > 0
logtobase logtobase(x, b) log of x to base b for x > 0 and b > 0
random random(x, y) random number between x and y
ArgumentList:
Start End
Start NumericExpression Argumentsopt End
Start:
The stored "argument list start" token
End:
The stored "argument list end" token
Arguments:
ArgumentSeparator NumericExpression Argumentsopt
ArgumentSeparator
The stored "argument separator" token

The constants e and pi are real-numbers with their value taken from the constants provided in the Java Math class. The constants true and false are boolean values and are the only way to represent a primitive boolean value without operation on numeric values.

Constant one of:
e pi true false Infinity NaN

Since:
1.0
See Also:
Java Language Specification

Nested Class Summary
static class JPTParser.BooleanOperation
          Class used to store a boolean operation.
static class JPTParser.NumericOperation
          Class used to store a numeric operation.
 
Nested classes inherited from class edu.neu.ccs.parser.AbstractParser
AbstractParser.ObjectOperationPair, AbstractParser.Procedure
 
Field Summary
protected  JPTParser.BooleanOperation opAND
          BooleanOperation to implement and (as double ampersand).
protected  JPTParser.BooleanOperation opEQ
          BooleanOperation to implement equals.
protected  JPTParser.BooleanOperation opGE
          BooleanOperation to implement greater than or equals.
protected  JPTParser.BooleanOperation opGT
          BooleanOperation to implement greater than.
protected  JPTParser.BooleanOperation opLE
          BooleanOperation to implement less than or equals.
protected  JPTParser.BooleanOperation opLT
          BooleanOperation to implement less than.
protected  JPTParser.NumericOperation opMinus
          NumericOperation to implement minus.
protected  JPTParser.BooleanOperation opNE
          BooleanOperation to implement not equals.
protected  JPTParser.BooleanOperation opNOT
          BooleanOperation to implement not (as exclamation).
protected  JPTParser.BooleanOperation opOR
          BooleanOperation to implement or (as double bar).
protected  JPTParser.NumericOperation opPercent
          NumericOperation to implement remainder.
protected  JPTParser.NumericOperation opPlus
          NumericOperation to implement plus.
protected  JPTParser.NumericOperation opSlash
          NumericOperation to implement divide.
protected  JPTParser.NumericOperation opTimes
          NumericOperation to implement times.
protected  AbstractParser.Procedure procAbs
          Procedure to implement abs.
protected  AbstractParser.Procedure procACos
          Procedure to implement acos.
protected  AbstractParser.Procedure procACosDeg
          Procedure to implement acosdeg.
protected  AbstractParser.Procedure procASin
          Procedure to implement asin.
protected  AbstractParser.Procedure procASinDeg
          Procedure to implement asindeg.
protected  AbstractParser.Procedure procATan
          Procedure to implement atan.
protected  AbstractParser.Procedure procATan2
          Procedure to implement atan2.
protected  AbstractParser.Procedure procATan2Deg
          Procedure to implement atan2deg.
protected  AbstractParser.Procedure procATanDeg
          Procedure to implement atandeg.
protected  AbstractParser.Procedure procCeiling
          Procedure to implement ceiling.
protected  AbstractParser.Procedure procCos
          Procedure to implement cos.
protected  AbstractParser.Procedure procCosDeg
          Procedure to implement cosdeg.
protected  AbstractParser.Procedure procExp
          Procedure to implement exp.
protected  AbstractParser.Procedure procFloor
          Procedure to implement floor.
protected  AbstractParser.Procedure procLn
          Procedure to implement ln.
protected  AbstractParser.Procedure procLog
          Procedure to implement log.
protected  AbstractParser.Procedure procLogToBase
          Procedure to implement logtobase.
protected  AbstractParser.Procedure procMax
          Procedure to implement max.
protected  AbstractParser.Procedure procMin
          Procedure to implement min.
protected  AbstractParser.Procedure procPower
          Procedure to implement power.
protected  AbstractParser.Procedure procRandom
          Procedure to implement random.
protected  AbstractParser.Procedure procRound
          Procedure to implement round.
protected  AbstractParser.Procedure procSin
          Procedure to implement sin.
protected  AbstractParser.Procedure procSinDeg
          Procedure to implement sindeg.
protected  AbstractParser.Procedure procSqrt
          Procedure to implement sqrt.
protected  AbstractParser.Procedure procTan
          Procedure to implement tan.
protected  AbstractParser.Procedure procTanDeg
          Procedure to implement tandeg.
protected  AbstractParser.Procedure procToDegrees
          Procedure to implement todegrees.
protected  AbstractParser.Procedure procToRadians
          Procedure to implement toradians.
 
Fields inherited from class edu.neu.ccs.parser.AbstractParser
ARGUMENT_LIST_END, ARGUMENT_LIST_START, ARGUMENT_SEPARATOR, constants, data, environment, FLOATING, identity, INTEGRAL, NESTED_EXPRESSION_END, NESTED_EXPRESSION_START, next, OPERATION_PREFIX, operations, precedence, prefixes, procedures, RADIX_POINT, UNDERSCORE
 
Constructor Summary
JPTParser()
           
 
Method Summary
protected  void addConstants()
          Adds the standard constants for this parser to the environment.
protected  void addOperations()
          Adds the standard operations for this parser to the operation table.
protected  void addProcedures()
          Adds the standard procedures for this parser to the procedure table.
protected  void defineBooleanOperations()
          Define the boolean operations.
protected  void defineNumericOperations()
          Define the numeric operations.
protected  void defineProcedures()
          Define the procedures.
protected  Object nextSimpleTerm()
          Returns the next simple term in the data String or throws a ParseException if no term is present.
protected  Object nextTerm()
          Returns the next term in the data String with all leading unary operations already applied or throws a ParseException if no term is present.
protected  AbstractParser.Operation[] nextUnaryOperations()
          Returns the array of unary operations that occur after the current position in the data String and throws a ParseException if an operation is encountered that is binary but not unary.
 Object parse(String d)
          Parses the given data and returns the Object it represents, given this parsing scheme.
protected  AbstractParser.ObjectOperationPair parseExpression(AbstractParser.ObjectOperationPair standing)
          Parses the next expression in the data String.
protected  Object parseNestedExpression()
          Parse an expression in parentheses and return its value.
protected  Object parseProcedureCall(String identifier)
          Parse a procedure with arguments and return its value.
protected  Object parseVariable(String identifier)
          Parse a variable expression and return its value.
 
Methods inherited from class edu.neu.ccs.parser.AbstractParser
addConstant, addOperation, addOperationAfterPrecedenceOf, addOperationAtPrecedenceOf, addOperationBeforePrecedenceOf, addProcedure, afterDigits, afterSign, assign, isExponentAt, isOperationOrPrefix, isSignAt, nextOperation, nextTokenIs, nextTokenIs, parseArgumentList, parseIdentifier, parseNumber, precedenceOf, setArgumentSeparatorToken, setLeftParenthesisToken, setRadixPointToken, setRightParenthesisToken, skipWhitespace, startsIdentifier, startsNumber, withinIdentifier
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

opPlus

protected JPTParser.NumericOperation opPlus
NumericOperation to implement plus.


opMinus

protected JPTParser.NumericOperation opMinus
NumericOperation to implement minus.


opTimes

protected JPTParser.NumericOperation opTimes
NumericOperation to implement times.


opSlash

protected JPTParser.NumericOperation opSlash
NumericOperation to implement divide.


opPercent

protected JPTParser.NumericOperation opPercent
NumericOperation to implement remainder.


opEQ

protected JPTParser.BooleanOperation opEQ
BooleanOperation to implement equals.


opNE

protected JPTParser.BooleanOperation opNE
BooleanOperation to implement not equals.


opLT

protected JPTParser.BooleanOperation opLT
BooleanOperation to implement less than.


opGT

protected JPTParser.BooleanOperation opGT
BooleanOperation to implement greater than.


opLE

protected JPTParser.BooleanOperation opLE
BooleanOperation to implement less than or equals.


opGE

protected JPTParser.BooleanOperation opGE
BooleanOperation to implement greater than or equals.


opAND

protected JPTParser.BooleanOperation opAND
BooleanOperation to implement and (as double ampersand).


opOR

protected JPTParser.BooleanOperation opOR
BooleanOperation to implement or (as double bar).


opNOT

protected JPTParser.BooleanOperation opNOT
BooleanOperation to implement not (as exclamation).


procAbs

protected AbstractParser.Procedure procAbs
Procedure to implement abs.


procCeiling

protected AbstractParser.Procedure procCeiling
Procedure to implement ceiling.


procFloor

protected AbstractParser.Procedure procFloor
Procedure to implement floor.


procRound

protected AbstractParser.Procedure procRound
Procedure to implement round.


procMax

protected AbstractParser.Procedure procMax
Procedure to implement max.


procMin

protected AbstractParser.Procedure procMin
Procedure to implement min.


procSqrt

protected AbstractParser.Procedure procSqrt
Procedure to implement sqrt.


procPower

protected AbstractParser.Procedure procPower
Procedure to implement power.


procToDegrees

protected AbstractParser.Procedure procToDegrees
Procedure to implement todegrees.


procToRadians

protected AbstractParser.Procedure procToRadians
Procedure to implement toradians.


procSin

protected AbstractParser.Procedure procSin
Procedure to implement sin.


procSinDeg

protected AbstractParser.Procedure procSinDeg
Procedure to implement sindeg.


procCos

protected AbstractParser.Procedure procCos
Procedure to implement cos.


procCosDeg

protected AbstractParser.Procedure procCosDeg
Procedure to implement cosdeg.


procTan

protected AbstractParser.Procedure procTan
Procedure to implement tan.


procTanDeg

protected AbstractParser.Procedure procTanDeg
Procedure to implement tandeg.


procASin

protected AbstractParser.Procedure procASin
Procedure to implement asin.


procASinDeg

protected AbstractParser.Procedure procASinDeg
Procedure to implement asindeg.


procACos

protected AbstractParser.Procedure procACos
Procedure to implement acos.


procACosDeg

protected AbstractParser.Procedure procACosDeg
Procedure to implement acosdeg.


procATan

protected AbstractParser.Procedure procATan
Procedure to implement atan.


procATanDeg

protected AbstractParser.Procedure procATanDeg
Procedure to implement atandeg.


procATan2

protected AbstractParser.Procedure procATan2
Procedure to implement atan2.


procATan2Deg

protected AbstractParser.Procedure procATan2Deg
Procedure to implement atan2deg.


procExp

protected AbstractParser.Procedure procExp
Procedure to implement exp.


procLn

protected AbstractParser.Procedure procLn
Procedure to implement ln. This is identical to log.


procLog

protected AbstractParser.Procedure procLog
Procedure to implement log. This is identical to ln.


procLogToBase

protected AbstractParser.Procedure procLogToBase
Procedure to implement logtobase.


procRandom

protected AbstractParser.Procedure procRandom
Procedure to implement random.

Constructor Detail

JPTParser

public JPTParser()
Method Detail

parse

public Object parse(String d)
             throws ParseException
Description copied from interface: Parser
Parses the given data and returns the Object it represents, given this parsing scheme.

Specified by:
parse in interface Parser
Specified by:
parse in class AbstractParser
Parameters:
d - the String data to be parsed
Throws:
ParseException - if the data is malformed

defineNumericOperations

protected void defineNumericOperations()
Define the numeric operations. Replaces the method named defineNumericOperators.

Since:
2.2

defineBooleanOperations

protected void defineBooleanOperations()
Define the boolean operations. Replaces the method named defineBooleanOperators

Since:
2.2

addOperations

protected void addOperations()
Adds the standard operations for this parser to the operation table. Replaces the method named addOperators.

Overrides:
addOperations in class AbstractParser
Since:
2.2

defineProcedures

protected void defineProcedures()
Define the procedures.

Since:
2.1

addProcedures

protected void addProcedures()
Adds the standard procedures for this parser to the procedure table.

Overrides:
addProcedures in class AbstractParser

addConstants

protected void addConstants()
Adds the standard constants for this parser to the environment.

Overrides:
addConstants in class AbstractParser

parseExpression

protected AbstractParser.ObjectOperationPair parseExpression(AbstractParser.ObjectOperationPair standing)
                                                      throws ParseException
Parses the next expression in the data String. Replaces method with the same name due to changes in the parameter and return type name.

Specified by:
parseExpression in class AbstractParser
Parameters:
standing - the value of the left operand and the operation immediately preceding the expression to be parsed
Returns:
the value of the parsed expression and the operation immediately following it
Throws:
ParseException - if the expression is malformed or if the incoming operation is null
Since:
2.2

nextTerm

protected Object nextTerm()
                   throws ParseException
Returns the next term in the data String with all leading unary operations already applied or throws a ParseException if no term is present.

ParseException
Since:
2.2

nextSimpleTerm

protected Object nextSimpleTerm()
                         throws ParseException
Returns the next simple term in the data String or throws a ParseException if no term is present.

ParseException
Since:
2.2

nextUnaryOperations

protected AbstractParser.Operation[] nextUnaryOperations()
                                                  throws ParseException
Returns the array of unary operations that occur after the current position in the data String and throws a ParseException if an operation is encountered that is binary but not unary.

ParseException
Since:
2.2

parseNestedExpression

protected Object parseNestedExpression()
                                throws ParseException
Parse an expression in parentheses and return its value.

ParseException
Since:
2.2

parseProcedureCall

protected Object parseProcedureCall(String identifier)
                             throws ParseException
Parse a procedure with arguments and return its value.

ParseException
Since:
2.2

parseVariable

protected Object parseVariable(String identifier)
                        throws ParseException
Parse a variable expression and return its value.

ParseException
Since:
2.2