
      A Language of Arithmetic and Boolean Expressions


An arithmetic or boolean expression is defined by the following grammar.

        Exp ::= BExp
              | AExp
              | BOOLEAN              
              | INTEGER              

       BExp ::= '(' '||'  Exp Exp+ ')'  // two or more parameters
              | '(' '&&'  Exp Exp+ ')'  // two or more parameters
              | '(' '!'   Exp ')'       // one parameter
              | '(' RelOp Exp Exp  ')'  // two parameters
              | '('  EqOp Exp Exp  ')'  // two parameters

      RelOp ::= '<' | '>' | '<=' | '>='
       EqOp ::= '==' | '!='

       AExp ::= '(' '+' Exp Exp* ')'    // one or more parameters
              | '(' '-' Exp Exp? ')'    // one or two parameters
              | '(' '*' Exp Exp+ ')'    // two or more parameters
              | '(' '/' Exp Exp  ')'    // two parameters
              | '(' '%' Exp Exp  ')'    // two parameters
              | '(' '^' Exp Exp  ')'    // two parameters
              
    BOOLEAN ::= 'true' | 'false'
    INTEGER ::= [-][0-9]+

The '/', ' %', '^', relational, and equality operators are strictly binary operators.
The '+' operator can take any number of inputs (i.e., one or more).
The '-' operator can take one or two inputs.
The '*', '&&', and '||' operators can take two or more inputs.
The '!' operator takes just one input.

Notice that this language is a subset of Language_0 (our language of trees) and a superset of Language_1 (our language of arithmetic expressions). That is, every string from this language is also a string from Language_0. And every string from Language_1 is also a string from this language.

The boolean '&&' and '||' operators should be short circuited.

Notice that the result of evaluating an Exp is either an integer value or a boolean value.

Since an expression in this language can have a value with one of two types, we need a way for the interpreter to return multiple types of values. Remember that a Java method can only return a single type of value, but our interpreter's eval() method must be able to return both an integer value and a boolean value. What we will do is define a Value class that encapsulates the return value from our interpreter. A Value object will be defined to hold both an integer value and a boolean value. In addition, a Value object will contain a "tag" field that specifies which of the two values in the object is the valid one, either the int or the boolean.

Objects like Value are called "tagged unions" or "discriminated unions".
See
   http://en.wikipedia.org/wiki/Tagged_union


Notice that there is nothing in the grammar that stops a boolean expression from being nested in an arithmetic expression (or visa-versa). Because of this, an arithmetic expression (or a boolean expression) cannot guarantee that its operands are integers (or, respectively, booleans). An expression like
   (+ true 1)
will parse and be passed to the interpreter. So the evaluator for arithmetic expressions should check the type of each of its operands (and similarly for the boolean evaluator).

In addition, since we are not really parsing the grammar given above (we are in fact parsing the tree language Language_0), there is nothing that enforces our grammar rules about the number of parameters to each operator. So the interpreter should do a runtime check of the number of parameters to each operator. But this is an expensive thing to be doing in an interpreter (it really slows it down) so in future languages we will look for ways to mitigate this problem.
