Support for evaluating expressions in XML form.
A validated DOM Object is built for the XML expression. The recursive methods in this class walk over the DOM tree and evaluate the expression.
The tree walk reflects the structure of the expression. The expression structure is shown below in BNF like syntax.
statement: assignment | addExp
assignment: ident "=" addExp
addExp: term | addExp addOp addExp
term: unaryExpr | term mulOp term
unaryExpr: factor | minus factor
factor: ident | number | "(" addExp ")"
The XML expression is validated by an XML schema (see expression.xsd).
Expressions may be evaluated by themselves or assigned to a variable. The class supports a basic symbol table to support symbols and their associated values.
Public Member Functions | |
Integer | eval (DOMParser parser, byte[] xmlBytes) |
This function is passed an assignment statement or an expression, formatted in XML. | |
Private Member Functions | |
ArrayList | toElementNodeList (NodeList list) |
A DOM NodeList may include a variety of different node types. | |
int | symbolLookup (String name) |
Given a symbol name, look the symbol up in the symbol table. | |
void | symbolEnter (String name, int value) |
Enter a symbol and its value into the symbol table. | |
String | nodeTypeToString (short type) |
Get the type name associated with the numeric Node type value. | |
int | factor (Node root) |
Evaluate identifiers, numbers or parenthesized expressions (via a recursive call to the addExp method). | |
int | unaryExp (Node root) |
Process a factor or a unary minus expression (the silly unary plus is not supported). | |
int | term (Node root) |
Process a factor expression. | |
int | addExp (Node root) |
void | store (Node lhs, int value) |
"Store" a value in a symbol | |
Integer | statement (Node root) |
Process and assignment statement or an expression. | |
Static Private Member Functions | |
Document | bytesToDocument (DOMParser parser, byte[] xmlBytes) throws SAXException, IOException |
Parse the XML into a DOM Document. | |
Private Attributes | |
HashMap | mSymTab = new HashMap() |
The symbol table consists of a set of String, Integer pairs, where the String object is the key. |
|
addExp: term | addExp addOp addExp
|
|
Parse the XML into a DOM Document.
|
|
This function is passed an assignment statement or an expression, formatted in XML. If the argument is an assignment statement, the right hand side (RHS) value is assigned to the variable on the left hand side (LHS). In the case of an assignment statement the function will return null. If the argument is an expression the expression will be evaluated. The function will return the result as an Integer object.
|
|
Evaluate identifiers, numbers or parenthesized expressions (via a recursive call to the addExp method).
factor: ident | number | paren addExp
|
|
Get the type name associated with the numeric Node type value. This method is called from error messages.
|
|
Process and assignment statement or an expression.
statement: assignment | addExp
|
|
"Store" a value in a symbol
|
|
Enter a symbol and its value into the symbol table.
|
|
Given a symbol name, look the symbol up in the symbol table.
|
|
Process a factor expression.
term: factor | term mulOp term
|
|
A DOM NodeList may include a variety of different node types. This is awkward when it comes to expression processing, since most of the time we only care about ELEMENT_NODEs. The toElementNodeList method builds an ArrayList that consists only of ELEMENT_NODES.
|
|
Process a factor or a unary minus expression (the silly unary plus is not supported).
unaryExp: factor | uminus factor
|
|
The symbol table consists of a set of String, Integer pairs, where the String object is the key.
|