|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object
|
+--edu.neu.ccs.parser.AbstractParser
|
+--edu.neu.ccs.parser.JPTParser
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
| 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 java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
protected JPTParser.NumericOperation opPlus
protected JPTParser.NumericOperation opMinus
protected JPTParser.NumericOperation opTimes
protected JPTParser.NumericOperation opSlash
protected JPTParser.NumericOperation opPercent
protected JPTParser.BooleanOperation opEQ
protected JPTParser.BooleanOperation opNE
protected JPTParser.BooleanOperation opLT
protected JPTParser.BooleanOperation opGT
protected JPTParser.BooleanOperation opLE
protected JPTParser.BooleanOperation opGE
protected JPTParser.BooleanOperation opAND
protected JPTParser.BooleanOperation opOR
protected JPTParser.BooleanOperation opNOT
protected AbstractParser.Procedure procAbs
abs.
protected AbstractParser.Procedure procCeiling
ceiling.
protected AbstractParser.Procedure procFloor
floor.
protected AbstractParser.Procedure procRound
round.
protected AbstractParser.Procedure procMax
max.
protected AbstractParser.Procedure procMin
min.
protected AbstractParser.Procedure procSqrt
sqrt.
protected AbstractParser.Procedure procPower
power.
protected AbstractParser.Procedure procToDegrees
todegrees.
protected AbstractParser.Procedure procToRadians
toradians.
protected AbstractParser.Procedure procSin
sin.
protected AbstractParser.Procedure procSinDeg
sindeg.
protected AbstractParser.Procedure procCos
cos.
protected AbstractParser.Procedure procCosDeg
cosdeg.
protected AbstractParser.Procedure procTan
tan.
protected AbstractParser.Procedure procTanDeg
tandeg.
protected AbstractParser.Procedure procASin
asin.
protected AbstractParser.Procedure procASinDeg
asindeg.
protected AbstractParser.Procedure procACos
acos.
protected AbstractParser.Procedure procACosDeg
acosdeg.
protected AbstractParser.Procedure procATan
atan.
protected AbstractParser.Procedure procATanDeg
atandeg.
protected AbstractParser.Procedure procATan2
atan2.
protected AbstractParser.Procedure procATan2Deg
atan2deg.
protected AbstractParser.Procedure procExp
exp.
protected AbstractParser.Procedure procLn
ln. This is identical to log.
protected AbstractParser.Procedure procLog
log. This is identical to ln.
protected AbstractParser.Procedure procLogToBase
logtobase.
protected AbstractParser.Procedure procRandom
random.
| Constructor Detail |
public JPTParser()
| Method Detail |
public Object parse(String d)
throws ParseException
ParserObject it represents,
given this parsing scheme.
parse in interface Parserparse in class AbstractParserd - the String data to be parsed
ParseException - if the data is malformedprotected void defineNumericOperations()
protected void defineBooleanOperations()
protected void addOperations()
addOperations in class AbstractParserprotected void defineProcedures()
protected void addProcedures()
addProcedures in class AbstractParserprotected void addConstants()
addConstants in class AbstractParser
protected AbstractParser.ObjectOperationPair parseExpression(AbstractParser.ObjectOperationPair standing)
throws ParseException
String.
Replaces method with the same name due to changes in
the parameter and return type name.
parseExpression in class AbstractParserstanding - the value of the left operand
and the operation immediately preceding
the expression to be parsed
ParseException - if the expression is malformed
or if the incoming operation is null
protected Object nextTerm()
throws ParseException
String with all
leading unary operations already applied
or throws a ParseException if no term is present.
ParseException
protected Object nextSimpleTerm()
throws ParseException
String
or throws a ParseException if no term is present.
ParseException
protected AbstractParser.Operation[] nextUnaryOperations()
throws ParseException
String
and throws a ParseException if an operation is encountered that is
binary but not unary.
ParseException
protected Object parseNestedExpression()
throws ParseException
ParseException
protected Object parseProcedureCall(String identifier)
throws ParseException
ParseException
protected Object parseVariable(String identifier)
throws ParseException
ParseException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||