Package edu.hws.jcm.data
Class Parser
java.lang.Object
edu.hws.jcm.data.Parser
- All Implemented Interfaces:
Serializable
A Parser can take a string and compile it into an ExpressionProgram.
MathObjects, such as variables and functions, can be registered with
the Parser. This means that the Parser will recognize them in the
strings that it parses. There are a few options that can be set to
control certain aspects of the parsing. If a string does not have
the correct syntax for an expression, then the Parser will throw a
ParseError when it tries to parse that string. A Parser can have a
parent. It inherits any MathObjects registered with its parent, but
a MathObject registered with a Parser will hide any MathObject of
the same name that is registered with its parent.
Every parser recognizes the constants pi and e and the operators
+, -, *, /, ^, and **. The ** operator is a synonym for ^, the
exponentiation operator. Both unary and binary + and - are recognized.
The exponentiation operator is right associative. The others are
left associative.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
The default options set that is used for a newly created Parser, if none is specified in the Constructor.static final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
An option that can be set for this parser.static final int
An that can be set for this parser.protected int
The set of options that have been enabled for this parser.static final int
An option that can be set for this parser.protected SymbolTable
The symbol table that contains the MathObjects that have been registered with this parser. -
Constructor Summary
ConstructorsConstructorDescriptionParser()
Construct a Parser with no parent and with the default options, BOOLEANS and STANDARD_FUNCTIONS.Parser
(int options) Create a Parser with the spedified option set and with no parent.Create a Parser with the specified parent.Create a Parser with the specified parent. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(MathObject sym) Register the MathObject with the Parser, associating it with its name.void
addOptions
(int newOptions) Add the options in the option set newOptions to this Parser's option set.Get the MathObject that has been registered with the parser under the given name.Parse the string str and create the corresponding expression.boolean
parseExpression
(ParserContext context) Called as part of the parsing process.boolean
parseFactor
(ParserContext context) Called as part of the parsing process.parseLogical
(String str) Parse the String, str, and create a corresponding logical-valued expression.boolean
parseLogicalExpression
(ParserContext context) Called as part of the parsing process.boolean
parseLogicalFactor
(ParserContext context) Called as part of the parsing process.boolean
parseLogicalTerm
(ParserContext context) Called as part of the parsing process.boolean
parsePrimary
(ParserContext context) Called as part of the parsing process.boolean
parseRelation
(ParserContext context) Called as part of the parsing process.boolean
parseTerm
(ParserContext context) Called as part of the parsing process.void
Deregister the MathObject with the given name, if there is one registered with the Parser.
-
Field Details
-
CASE_SENSITIVE
public static final int CASE_SENSITIVEAn option that can be set for this parser. If enabled, identifiers are case-sensitive. For example, Sin, sin, and SIN will be treated as separate identifiers. It really only makes sense to enable this at the time the Parser is first constructed.- See Also:
-
OPTIONAL_STARS
public static final int OPTIONAL_STARSAn that can be set for this parser. If enabled, mutltiplication can be indicated implicitely, as well as with a "*". For example, 2x will mean 2*x.- See Also:
-
OPTIONAL_SPACES
public static final int OPTIONAL_SPACESAn option that can be set for this parser. If enabled, spaces are not required to separate identifiers. This only has an effect if one of OPTIONAL_STARS or OPTIONAL_PARENS is also enabled. For example, xsin(x) will be read as x*sin(x), and sine will be read as sin(e).- See Also:
-
BRACKETS
public static final int BRACKETSAn option that can be set for this parser. If enabled, brackets, [ and ], can be used for grouping.- See Also:
-
BRACES
public static final int BRACESAn option that can be set for this parser. If enabled, braces, { and }, can be used for grouping.- See Also:
-
BOOLEANS
public static final int BOOLEANSAn option that can be set for this parser. If enabled, the "?" operator can be used in expressions, along with the logical operators invalid input: '&', |, ~, =, invalid input: '<', >, invalid input: '<'>, invalid input: '<'=, >=. The words "and", "or", and "not" can be used in place of invalid input: '&', |, and ~. These words are treated in a case-insensitive way, even if the CASE_SENSITIVE option is on. When this option is set, it is legal to call the parseLogical method to parse a boolean-valued expression. This option is enabled by default.- See Also:
-
FACTORIAL
public static final int FACTORIALAn option that can be set for this parser. If enabled, the factorial operator, !, is recognized.- See Also:
-
NO_UNDERSCORE_IN_IDENTIFIERS
public static final int NO_UNDERSCORE_IN_IDENTIFIERSAn option that can be set for this parser. The character "_", which can usually be used just like a letter, is not allowed in identifers.- See Also:
-
NO_DIGITS_IN_IDENTIFIERS
public static final int NO_DIGITS_IN_IDENTIFIERSAn option that can be set for this parser. Digits 0 through 9, which can usually be used in an identifier after the first character, are not allowed in identifiers.- See Also:
-
OPTIONAL_PARENS
public static final int OPTIONAL_PARENSAn option that can be set for this parser. If enabled, parentheses are optional around the parameter of a standard function. If the parentheses are omited, then the argument is the term that follows the function name. For example, "sin x + 1" means "sin(x) + 1" while "sin x * cos x" means "sin( x*cos(x) )".- See Also:
-
STANDARD_FUNCTIONS
public static final int STANDARD_FUNCTIONSAn option that can be set for this parser. When enabled, the standard functions are registered with the parser. This option is enabled by default. The standard functions are: sin, cos, tan, cot, sec, csc, arcsin, arccos, arctan, exp, ln, log2, log10, sqrt, cubert, abs, round, floor, ceiling, trunc.- See Also:
-
DEFAULT_OPTIONS
public static final int DEFAULT_OPTIONSThe default options set that is used for a newly created Parser, if none is specified in the Constructor. It includes the options BOOLEANS and STANDARD_FUNCTIONS.- See Also:
-
options
protected int optionsThe set of options that have been enabled for this parser. -
symbols
The symbol table that contains the MathObjects that have been registered with this parser.
-
-
Constructor Details
-
Parser
public Parser()Construct a Parser with no parent and with the default options, BOOLEANS and STANDARD_FUNCTIONS. -
Parser
Create a Parser with the specified parent. The options for this parser are inherited from the parent, if parent is non-null. If parent is null, the option set is empty. -
Parser
public Parser(int options) Create a Parser with the spedified option set and with no parent. -
Parser
Create a Parser with the specified parent. The options for this parser consist of the option set from the parent, together with any additional options in the specified options set.- Parameters:
parent
- parent of this Parser, possibly null.options
- additional options, in addition to ones inherited from parent.
-
-
Method Details
-
addOptions
public void addOptions(int newOptions) Add the options in the option set newOptions to this Parser's option set. The value of newOptions can be one of the option constants defined in this class, such as OPTIONAL_STARS, or it can consist of several option constants OR-ed together. -
parse
Parse the string str and create the corresponding expression. The expression must be numeric-valued, not logical. There can't be any extra characters in str after the expression. If a syntax error is found, a ParseError will be thrown.- Parameters:
str
- String to parse.- Returns:
- the expression defined by the string.
-
parseLogical
Parse the String, str, and create a corresponding logical-valued expression. The expression must be logical-valued, such as "x > 0", not numeric. There can't be any extra characters in str after the expression. If a syntax error is found, a ParseError will be thrown. It is not legal to call this method if the BOOLEANS option is not set.- Parameters:
str
- String to parse.- Returns:
- the logical-valued expression defined by str.
-
get
Get the MathObject that has been registered with the parser under the given name. If the CASE_SENSITIVE option is not set, names are converted to lower case for the purpose of registering and retrieving registered objects. -
add
Register the MathObject with the Parser, associating it with its name. An error will occur if the name is null. If the CASE_SENSITIVE option is not set, names are converted to lower case for the purpose of registering and retrieving registered objects. -
remove
Deregister the MathObject with the given name, if there is one registered with the Parser. If the name is not registered, nothing happens and no error occurs.- Parameters:
name
- MathObject to deregister.
-
parseLogicalExpression
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parseLogicalTerm
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parseLogicalFactor
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parseRelation
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parseExpression
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parseTerm
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parsePrimary
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension. -
parseFactor
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.
-