net.sourceforge.webcompmath.data
Class Parser

java.lang.Object
  extended by net.sourceforge.webcompmath.data.Parser
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
WCMParserBean

public class Parser
extends java.lang.Object
implements java.io.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:
Serialized Form

Field Summary
static int BOOLEANS
          An option that can be set for this parser.
static int BRACES
          An option that can be set for this parser.
static int BRACKETS
          An option that can be set for this parser.
static int CASE_SENSITIVE
          An option that can be set for this parser.
static int DEFAULT_OPTIONS
          The default options set that is used for a newly created Parser, if none is specified in the Constructor.
static int FACTORIAL
          An option that can be set for this parser.
static int NO_DIGITS_IN_IDENTIFIERS
          An option that can be set for this parser.
static int NO_UNDERSCORE_IN_IDENTIFIERS
          An option that can be set for this parser.
static int OPTIONAL_FUNCTIONS
          An option that can be set for this parser.
static int OPTIONAL_PARENS
          An option that can be set for this parser.
static int OPTIONAL_SPACES
          An option that can be set for this parser.
static int OPTIONAL_STARS
          An that can be set for this parser.
protected  int options
          The set of options that have been enabled for this parser.
static int RATIONAL_EXPONENTS
          An option that can be set for this parser.
static int STANDARD_FUNCTIONS
          An option that can be set for this parser.
protected  SymbolTable symbols
          The symbol table that contains the MathObjects that have been registered with this parser.
 
Constructor Summary
Parser()
          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.
Parser(Parser parent)
          Create a Parser with the specified parent.
Parser(Parser parent, int options)
          Create a Parser with the specified parent.
 
Method Summary
 void 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.
 MathObject get(java.lang.String name)
          Get the MathObject that has been registered with the parser under the given name.
protected  int getOptions()
          Gets the current options.
 ExpressionProgram parse(java.lang.String str)
          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.
 ExpressionProgram parseLogical(java.lang.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 remove(java.lang.String name)
          Deregister the MathObject with the given name, if there is one registered with the Parser.
 void setVariable(Variable inVar)
          added by PKHG 050415 to make visual building of GraphApplet1 possible
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CASE_SENSITIVE

public static final 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. It really only makes sense to enable this at the time the Parser is first constructed.

See Also:
Constant Field Values

OPTIONAL_STARS

public static final int OPTIONAL_STARS
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.

See Also:
Constant Field Values

OPTIONAL_SPACES

public static final int OPTIONAL_SPACES
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).

See Also:
Constant Field Values

BRACKETS

public static final int BRACKETS
An option that can be set for this parser. If enabled, brackets, [ and ], can be used for grouping.

See Also:
Constant Field Values

BRACES

public static final int BRACES
An option that can be set for this parser. If enabled, braces, { and }, can be used for grouping.

See Also:
Constant Field Values

BOOLEANS

public static final 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 ~. 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:
Constant Field Values

FACTORIAL

public static final int FACTORIAL
An option that can be set for this parser. If enabled, the factorial operator, !, is recognized.

See Also:
Constant Field Values

NO_UNDERSCORE_IN_IDENTIFIERS

public static final 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.

See Also:
Constant Field Values

NO_DIGITS_IN_IDENTIFIERS

public static final 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.

See Also:
Constant Field Values

OPTIONAL_PARENS

public static final int OPTIONAL_PARENS
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) )".

See Also:
Constant Field Values

STANDARD_FUNCTIONS

public static final int STANDARD_FUNCTIONS
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.

See Also:
Constant Field Values

OPTIONAL_FUNCTIONS

public static final int OPTIONAL_FUNCTIONS
An option that can be set for this parser. When enabled, a set of optional functions are registered with the parser. This option is disabled by default. The optional functions are: sinh, cosh, tanh.

See Also:
Constant Field Values

RATIONAL_EXPONENTS

public static final int RATIONAL_EXPONENTS
An option that can be set for this parser. When enabled, the parser will use rpow instead of pow for exponentiation. rpow attempts to determine if the exponent is a rational number. If it is and the denominator of the reduced fraction is odd, then negative bases are allowed (which is not the case with pow). rpow only looks for rational exponents with a denominator up to 1000. This option is disabled by default.

See Also:
Constant Field Values

DEFAULT_OPTIONS

public static final int DEFAULT_OPTIONS
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.

See Also:
Constant Field Values

options

protected int options
The set of options that have been enabled for this parser.


symbols

protected SymbolTable symbols
The symbol table that contains the MathObjects that have been registered with this parser.

Constructor Detail

Parser

public Parser()
Construct a Parser with no parent and with the default options, BOOLEANS and STANDARD_FUNCTIONS.


Parser

public Parser(Parser parent)
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.

Parameters:
parent - the parent to use in creating a parser

Parser

public Parser(int options)
Create a Parser with the spedified option set and with no parent.

Parameters:
options - options to use in creating parser

Parser

public Parser(Parser parent,
              int options)
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 Detail

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.

Parameters:
newOptions - options to add

getOptions

protected int getOptions()
Gets the current options. Intended for use by WCMParserBean

Returns:
the options

parse

public ExpressionProgram parse(java.lang.String str)
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

public ExpressionProgram parseLogical(java.lang.String str)
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. PKHG 050416 exceptions strings edited to get a better clue where the error occurs

Parameters:
str - String to parse.
Returns:
the logical-valued expression defined by str.

get

public MathObject get(java.lang.String name)
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.

Parameters:
name - name of the math object
Returns:
the mathobject

add

public void add(MathObject sym)
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.

Parameters:
sym - mathobject to register

remove

public void remove(java.lang.String name)
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

public boolean parseLogicalExpression(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - the parser context to use
Returns:
true or false

parseLogicalTerm

public boolean parseLogicalTerm(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - the parser context to use
Returns:
true or false

parseLogicalFactor

public boolean parseLogicalFactor(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - the parser context to use
Returns:
true or false

parseRelation

public boolean parseRelation(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - parser context to use
Returns:
true or false

parseExpression

public boolean parseExpression(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - parser context to use
Returns:
true or false

parseTerm

public boolean parseTerm(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - parser context to use
Returns:
true or false

parsePrimary

public boolean parsePrimary(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - parser context to use
Returns:
true or false

parseFactor

public boolean parseFactor(ParserContext context)
Called as part of the parsing process. From outside this class, this would probably be called only by a ParserExtension.

Parameters:
context - parser context to use
Returns:
true or false

setVariable

public void setVariable(Variable inVar)
added by PKHG 050415 to make visual building of GraphApplet1 possible

Parameters:
inVar -