| java.lang.Object edu.hws.jcm.data.Parser
Parser | public class Parser implements java.io.Serializable(Code) | | 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.
|
Field Summary | |
final public static int | BOOLEANS An option that can be set for this parser.
If enabled, the "?" operator can be used in expressions, along with the
logical operators &, |, ~, =, <, >, <>, <=, >=.
The words "and", "or", and "not" can be used
in place of &, |, and ~. | final public static int | BRACES An option that can be set for this parser. | final public static int | BRACKETS An option that can be set for this parser. | final public static int | CASE_SENSITIVE An 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. | final public static int | DEFAULT_OPTIONS The default options set that is used for
a newly created Parser, if none is specified
in the Constructor. | final public static int | FACTORIAL An option that can be set for this parser.
If enabled, the factorial operator, !, is recognized. | final public static int | NO_DIGITS_IN_IDENTIFIERS An 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. | final public static int | NO_UNDERSCORE_IN_IDENTIFIERS An option that can be set for this parser.
The character "_", which can usually
be used just like a letter, is
not allowed in identifers. | final public static int | OPTIONAL_PARENS An option that can be set for this parser.
If enabled, parentheses are optional around
the parameter of a standard function. | final public static int | OPTIONAL_SPACES An option that can be set for this parser.
If enabled, spaces are not required to separate
identifiers. | final public static int | OPTIONAL_STARS An that can be set for this parser.
If enabled, mutltiplication can be indicated
implicitely, as well as with a "*". | final public static int | STANDARD_FUNCTIONS An option that can be set for this parser.
When enabled, the standard functions are
registered with the parser. | protected int | options The set of options that have been enabled for this parser. | protected SymbolTable | symbols The symbol table that contains the MathObjects
that have been registered with this parser. |
Constructor Summary | |
public | Parser() Construct a Parser with no parent and with the default options,
BOOLEANS and STANDARD_FUNCTIONS. | public | Parser(Parser parent) Create a Parser with the specified parent. | public | Parser(int options) Create a Parser with the spedified option set and with no parent. | public | Parser(Parser parent, int options) Create a Parser with the specified parent. |
BOOLEANS | final public static int BOOLEANS(Code) | | An option that can be set for this parser.
If enabled, the "?" operator can be used in expressions, along with the
logical operators &, |, ~, =, <, >, <>, <=, >=.
The words "and", "or", and "not" can be used
in place of &, |, 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.
|
BRACES | final public static int BRACES(Code) | | An option that can be set for this parser.
If enabled, braces, { and }, can be used for grouping.
|
BRACKETS | final public static int BRACKETS(Code) | | An option that can be set for this parser.
If enabled, brackets, [ and ], can be used for grouping.
|
CASE_SENSITIVE | final public static int CASE_SENSITIVE(Code) | | An 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.
|
DEFAULT_OPTIONS | final public static int DEFAULT_OPTIONS(Code) | | The 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.
|
FACTORIAL | final public static int FACTORIAL(Code) | | An option that can be set for this parser.
If enabled, the factorial operator, !, is recognized.
|
NO_DIGITS_IN_IDENTIFIERS | final public static int NO_DIGITS_IN_IDENTIFIERS(Code) | | An 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.
|
NO_UNDERSCORE_IN_IDENTIFIERS | final public static int NO_UNDERSCORE_IN_IDENTIFIERS(Code) | | An option that can be set for this parser.
The character "_", which can usually
be used just like a letter, is
not allowed in identifers.
|
OPTIONAL_PARENS | final public static int OPTIONAL_PARENS(Code) | | An 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) )".
|
OPTIONAL_SPACES | final public static int OPTIONAL_SPACES(Code) | | An 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).
|
OPTIONAL_STARS | final public static int OPTIONAL_STARS(Code) | | An 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.
|
STANDARD_FUNCTIONS | final public static int STANDARD_FUNCTIONS(Code) | | An 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.
|
options | protected int options(Code) | | The set of options that have been enabled for this parser.
|
symbols | protected SymbolTable symbols(Code) | | The symbol table that contains the MathObjects
that have been registered with this parser.
|
Parser | public Parser()(Code) | | Construct a Parser with no parent and with the default options,
BOOLEANS and STANDARD_FUNCTIONS.
|
Parser | public Parser(Parser parent)(Code) | | 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)(Code) | | Create a Parser with the spedified option set and with no parent.
|
Parser | public Parser(Parser parent, int options)(Code) | | 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. Parameters: options - additional options, in addition to ones inherited from parent. |
add | public void add(MathObject sym)(Code) | | 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.
|
addOptions | public void addOptions(int newOptions)(Code) | | 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.
|
get | public MathObject get(String name)(Code) | | 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.
|
parse | public ExpressionProgram parse(String str)(Code) | | 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. the expression defined by the string. |
parseExpression | public boolean parseExpression(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parseFactor | public boolean parseFactor(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parseLogical | public ExpressionProgram parseLogical(String str)(Code) | | 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. the logical-valued expression defined by str. |
parseLogicalExpression | public boolean parseLogicalExpression(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parseLogicalFactor | public boolean parseLogicalFactor(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parseLogicalTerm | public boolean parseLogicalTerm(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parsePrimary | public boolean parsePrimary(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parseRelation | public boolean parseRelation(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
parseTerm | public boolean parseTerm(ParserContext context)(Code) | | Called as part of the parsing process. From outside this class, this would
probably be called only by a ParserExtension.
|
remove | public void remove(String name)(Code) | | 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. |
|
|