Calculator implements a simple arithmetic expression evaluator. It can evaluate typical expressions with the "normal" operators and precedence. Formally, the BNF for the supported grammar is:
<stmt> ::= <var> = <expr> | <expr> <expr> ::= <rexpr> | <expr> <bool op> <rexpr> <bool op> ::= && | <or> <or> ::= || <rexpr> ::= <aexpr> | <rexpr> <rel op> <aexpr> <rel op> ::= < | <= | > | >= | == | != <aexpr> ::= <term> | <aexpr> <add op> <term> <add op> ::= + | - <term> ::= <factor> | <term> <mult op> <factor> <mult op> ::= * | / | % <factor> ::= <var> | <num> | ! <factor> | ( <expr> ) <var> ::= <letter> | <var> <var2> <var2> ::= <letterordigit> | . | _ <num> ::= <unum> | + <unum> | - <unum> <unum> ::= <int> | <int> . | <int> . <int> | . <int> <int> ::= <digit> | <int> <digit>
A <letter> is defined as a Java
char
for which
Char.isLetter(char)
is
true
. A <letterordigit> is defined as a Java
char
for which
Char.isLetterOrDigit(char)
is
true
. A digit is defined as a Java
char
for which
Char.isDigit(char)
is
true
.
Values for <var>
are looked up in the supplied Dictionary
. If <var>
can not be found, it is assumed to have the value zero. If the value found is "true" or "yes" (case insensitive), it is assumed to be one. Similarly, if the value found is "false" or "no", it is assumed to be zero. Assignment to <var>
stores the computed value in the same Dictionary
.
The period in <unum>
, if there is one, must be immediately adjacent to surrounding <int>
s.
@author Steve Drach <drach@sun.com>
@version 2.3