A Calculator Language


The following example develops the denotational semantics for the language of the simple three-function calculator. A “program” on this calculator consists of a sequence of keystrokes generally alternating between operands and operators. The syntax below gives those combinations that we call legal on the calculator. For instance,   “6 + 33 × 2 =”   produces the value 78 on the display of the calculator.

To Divide use / Symbol


 <program> ::= <expression sequence>
 <expression sequence> ::= <expression>| <expression> <expression sequence>
 <expression> ::= <term> | <expression> <operator> <term> 
        | <expression> <answer> | <expression> <answer> +/-
 <term>      ::= <numeral> | MR | Clear | <term> +/-
 <operator>  ::= + |  | x
 <answer>    ::= M+ | =
 <numeral>   ::= <digit> | <numeral> <digit>
 <digit>     ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Observe that unlike more complex calculators, keystrokes are entered and processed from left to right, so that the addition is performed before the multiplication. In fact calculators usually accept any sequence of key presses, but we have limited our syntax to those collections that a user is most likely to employ. We outlaw combinations such as that provide no new meaningful calculations although many real calculators allow them. Comments about two keys: