Package edu.hws.jcm.data

# Class Parser

java.lang.Object
edu.hws.jcm.data.Parser

All Implemented Interfaces:
  `Serializable`

public class Parser extends Object implements 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

Fields
Modifier and Type
Field
Description
`static final int`
`BOOLEANS`
An option that can be set for this parser.
`static final int`
`BRACES`
An option that can be set for this parser.
`static final int`
`BRACKETS`
An option that can be set for this parser.
`static final int`
`CASE_SENSITIVE`
An option that can be set for this parser.
`static final int`
`DEFAULT_OPTIONS`
The default options set that is used for a newly created Parser, if none is specified in the Constructor.
`static final int`
`FACTORIAL`
An option that can be set for this parser.
`static final int`
`NO_DIGITS_IN_IDENTIFIERS`
An option that can be set for this parser.
`static final int`
`NO_UNDERSCORE_IN_IDENTIFIERS`
An option that can be set for this parser.
`static final int`
`OPTIONAL_PARENS`
An option that can be set for this parser.
`static final int`
`OPTIONAL_SPACES`
An option that can be set for this parser.
`static final 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 final 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

Constructors
Constructor
Description
`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

Modifier and Type
Method
Description
`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(String name)`
Get the MathObject that has been registered with the parser under the given name.
`ExpressionProgram`
`parse(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(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(String name)`
Deregister the MathObject with the given name, if there is one registered with the Parser.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### 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 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:
  - 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

### 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 Details

### 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.

### Parser

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

### 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 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

public ExpressionProgram parse(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(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.

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

### get

public MathObject get(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.

### 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.

### remove

public void remove(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.

### 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.

### 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.

### 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.

### 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.

### 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.

### 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.

### 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.