vmm.functions
Class Parser

java.lang.Object
  extended by vmm.functions.Parser

public class Parser
extends java.lang.Object

A Parser can parse a string that holds the definition of a real-valued or complex-valued function or expression.

An expression can use the operators +, -, *, /, and ^, where ^ represents expontiation. Multiplication can also be represented by juxtaposition. The ternary conditional operator "?:" can be used, as in "x < 0 ? -x : x". In the condition, the relational operators =, !=, <, >, <=, and >= and the logical operators and, or, and not can be used. Not equal can be expressed as <> as well as by !=, and the logical operators can be written as & |, and ~. Parentheses (), braces {}, and brackets [] can be used for grouping.

Names in expressions are not case-sensitive. The constants pi, e, i can be used in expressions (where i is the complex number, square root of minus one. Standard functions that are defined for both real and complex arguments are: abs, sqrt, cubert, exp, log, log2, log10, sin, cos, tan, sec, csc, cot, sinh, cosh, tanh, sech, scsh, coth, arcsin, arccos, arctan, arcsinh, arccosh, arctanh. Log can also be written as ln, and the inverse trig functions can also be written as asin, acos, atan, asinh, acosh, and atahn. Functions defined only for real arguments are: trunc, round, ceiling, floor, and signum. The signum function can also be written as sign or sgn. The real- and imaginary-part functions, re and im, are defined for a complex argument and produce a real result; they will also accept real arguemnts. The function complex(x,y) takes two real arguments and produces the complex value x+i*y; complex(x,y) is just another way to write this complex value, without using the constant i directly.

The definition of an expression or function can include references to parameters and functions that have been added to the parser (see add(Variable), add(ComplexVariable), add(Function), and add(ComplexFunction).


Constructor Summary
Parser()
          Create a parser with no parent.
Parser(Parser parent)
          Create a new parser.
 
Method Summary
 void add(ComplexFunction f)
          Add a complex-valued function to the parser so that it can be used in expressions parsed by this parser.
 void add(ComplexVariable v)
          Add a complex variable that can then be used as a parameter in expressions parsed by this parser.
 void add(Function f)
          Add a real-valued function to the parser so that it can be used in expressions parsed by this parser.
 void add(Variable v)
          Add a variable that can then be used as a parameter in expressions parsed by this parser.
 java.lang.Object get(java.lang.String name)
          Gets the object from the parser's symbol table that has the specified name, or returns null if there is no such object.
 Expression parse(java.lang.String str)
          Parse an expression.
 ComplexExpression parseComplexExpression(java.lang.String str)
          Parse a complex-valued expression.
 ComplexFunction parseComplexFunction(java.lang.String name, java.lang.String definition, java.lang.String... argumentName)
          Define a complex-valued function of any number of complex arguments by parsing its definition.
 ComplexFunction1 parseComplexFunction1(java.lang.String name, java.lang.String definition, java.lang.String argumentName)
          Define a complex-valued function of one real argumentsby parsing its definition.
 ComplexFunction2 parseComplexFunction2(java.lang.String name, java.lang.String definition, java.lang.String argumentName1, java.lang.String argumentName2)
          Define a conplex-valued function of two complex arguments by parsing its definition.
 ComplexFunction3 parseComplexFunction3(java.lang.String name, java.lang.String definition, java.lang.String argumentName1, java.lang.String argumentName2, java.lang.String argumentName3)
          Define a conplex-valued function of three complex arguments by parsing its definition.
 Expression parseExpression(java.lang.String str)
          Parse a real-valued expression.
 Function parseFunction(java.lang.String name, java.lang.String definition, java.lang.String... argumentName)
          Define a real-valued function of any number of real arguments by parsing its definition.
 Function1 parseFunction1(java.lang.String name, java.lang.String definition, java.lang.String argumentName)
          Define a real-valued function of one real argument by parsing its definition.
 Function2 parseFunction2(java.lang.String name, java.lang.String definition, java.lang.String argumentName1, java.lang.String argumentName2)
          Define a real-valued function of two real arguments by parsing its definition.
 Function3 parseFunction3(java.lang.String name, java.lang.String definition, java.lang.String argumentName1, java.lang.String argumentName2, java.lang.String argumentName3)
          Define a real-valued function of three real arguments by parsing its definition.
 void remove(java.lang.String objectName)
          Removes the object from the parser's symbol table that has the specified name, if there is such an object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Parser

public Parser()
Create a parser with no parent. (See Parser(Parser).)


Parser

public Parser(Parser parent)
Create a new parser. (This is mainly for internal use in this class.)

Parameters:
parent - if the parent is non-null, then this parser inherits any symbols that have been added to the parent parser. Symbols that are added to this parser do not affect the parent, but will hide symbols in the parent that have the same name making them unavailable for use by this parser.
Method Detail

parse

public Expression parse(java.lang.String str)
Parse an expression. This is just a synonym for parseExpression(String).

Throws:
ParseError - if a syntax error is found in the definition of the expression.

parseExpression

public Expression parseExpression(java.lang.String str)
Parse a real-valued expression.

Throws:
ParseError - if a syntax error is found in the definition of the expression.

parseFunction1

public Function1 parseFunction1(java.lang.String name,
                                java.lang.String definition,
                                java.lang.String argumentName)
Define a real-valued function of one real argument by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName - The name of the argument to the function. This must be non-null. It should be a legal identifier. The argument name can be used in the definition of the function.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseFunction2

public Function2 parseFunction2(java.lang.String name,
                                java.lang.String definition,
                                java.lang.String argumentName1,
                                java.lang.String argumentName2)
Define a real-valued function of two real arguments by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName1 - The name of the first argument to the function. This must be non-null. It should be a legal identifier.
argumentName2 - The name of the second argument to the function. This must be non-null. It should be a legal identifier.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseFunction3

public Function3 parseFunction3(java.lang.String name,
                                java.lang.String definition,
                                java.lang.String argumentName1,
                                java.lang.String argumentName2,
                                java.lang.String argumentName3)
Define a real-valued function of three real arguments by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName1 - The name of the first argument to the function. This must be non-null. It should be a legal identifier.
argumentName2 - The name of the second argument to the function. This must be non-null. It should be a legal identifier.
argumentName3 - The name of the third argument to the function. This must be non-null. It should be a legal identifier.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseFunction

public Function parseFunction(java.lang.String name,
                              java.lang.String definition,
                              java.lang.String... argumentName)
Define a real-valued function of any number of real arguments by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName - The names of the arguments to the function. There can be any number of argumentNames. They must be non-null and should be legal identifiers.
Returns:
the function. If the number of arguments is one, two, or three, then the return value is actually of type Function1, Function2, or Function3, respectively.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseComplexExpression

public ComplexExpression parseComplexExpression(java.lang.String str)
Parse a complex-valued expression.

Throws:
ParseError - if a syntax error is found in the definition of the expression.

parseComplexFunction1

public ComplexFunction1 parseComplexFunction1(java.lang.String name,
                                              java.lang.String definition,
                                              java.lang.String argumentName)
Define a complex-valued function of one real argumentsby parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName - The name of the argument to the function. This must be non-null. It should be a legal identifier.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseComplexFunction2

public ComplexFunction2 parseComplexFunction2(java.lang.String name,
                                              java.lang.String definition,
                                              java.lang.String argumentName1,
                                              java.lang.String argumentName2)
Define a conplex-valued function of two complex arguments by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName1 - The name of the first argument to the function. This must be non-null. It should be a legal identifier.
argumentName2 - The name of the second argument to the function. This must be non-null. It should be a legal identifier.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseComplexFunction3

public ComplexFunction3 parseComplexFunction3(java.lang.String name,
                                              java.lang.String definition,
                                              java.lang.String argumentName1,
                                              java.lang.String argumentName2,
                                              java.lang.String argumentName3)
Define a conplex-valued function of three complex arguments by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName1 - The name of the first argument to the function. This must be non-null. It should be a legal identifier.
argumentName2 - The name of the second argument to the function. This must be non-null. It should be a legal identifier.
argumentName3 - The name of the third argument to the function. This must be non-null. It should be a legal identifier.
Throws:
ParseError - if a syntax error is found in the definition of the function.

parseComplexFunction

public ComplexFunction parseComplexFunction(java.lang.String name,
                                            java.lang.String definition,
                                            java.lang.String... argumentName)
Define a complex-valued function of any number of complex arguments by parsing its definition.

Parameters:
name - the name of the function. This can be null and really only has to be non-null if the function will be added to a Parser for use in other expressions.
definition - The string that will be parsed to define the function.
argumentName - The names of the arguments to the function. There can be any number of argumentNames. They must be non-null and should be legal identifiers.
Returns:
the function. If the number of arguments is one, two, or three, then the return value is actually of type ComplexFunction1, ComplexFunction2, or ComplexFunction3, respectively.
Throws:
ParseError - if a syntax error is found in the definition of the function.

add

public void add(Variable v)
Add a variable that can then be used as a parameter in expressions parsed by this parser.

Parameters:
v - the variable to be added to the parser. This must be non-null and must have a non-null name. Furthermore, the name should be a legal identifier that can be used in expressions; that is, it should start with a letter and contain only letters, digits, the underscore character, with one or more apostrophes allowed at the end of the name.

add

public void add(ComplexVariable v)
Add a complex variable that can then be used as a parameter in expressions parsed by this parser.

Parameters:
v - the variable to be added to the parser. This must be non-null and must have a non-null name. Furthermore, the name should be a legal identifier that can be used in expressions; that is, it should start with a letter and contain only letters, digits, the underscore character, with one or more apostrophes allowed at the end of the name.

add

public void add(Function f)
Add a real-valued function to the parser so that it can be used in expressions parsed by this parser.

Parameters:
f - the function to be added to the parser. (Note that the only way to get such a function is from a Parser.) This must be non-null and must have a non-null name. The name should be a legal identifier; that is, it should start with a letter and contain only letters, digits, the underscore character, with one or more apostrophes allowed at the end of the name.

add

public void add(ComplexFunction f)
Add a complex-valued function to the parser so that it can be used in expressions parsed by this parser.

Parameters:
f - the function to be added to the parser. (Note that the only way to get such a function is from a Parser.) This must be non-null and must have a non-null name. The name should be a legal identifier; that is, it should start with a letter and contain only letters, digits, the underscore character, with one or more apostrophes allowed at the end of the name.

get

public java.lang.Object get(java.lang.String name)
Gets the object from the parser's symbol table that has the specified name, or returns null if there is no such object. The parent of the parser, if there is one, is also searched. The name is not case sensitive. (This is mostly for internal use, and not likely to be used by ordinary programmers.)


remove

public void remove(java.lang.String objectName)
Removes the object from the parser's symbol table that has the specified name, if there is such an object. (The object is only removed if it is found in this parser, not in the parent.) The name is not case sensitive. (This is mostly for internal use, and not likely to be used by ordinary programmers.)