Scientific Calculator

In many situations we'd like to use a workstation (or personal computer) as a calculator. It can be tedious to write and compile a C++ program every time we want to do a few calculations. Your assignment is to implement a scientific calculator program, similar in flavor to the Unix program "dc" (Decimal Calculator).

Your calculator will read in commands in the simple language of the calculator (which you will design), and perform each command in sequence. The commands may be entered interactively or from a file (using a calculator command to specify the program file name).

Your calculator should support the following types of commands:

  1. Compute value of expression
  2. Assign value of expression to variable
  3. Print value of expression
  4. Execute a program file

Your grade will be based in part on the functionality provided by your calculator. Your calculator should support a wide variety of arithmetic operators. For your implementation to be considered complete, you must support expressions with the standard four arithmetic operators (+, -, *, /), nested expressions (within parentheses), and variable assignment and usage. In addition, you may want to add other operators, including some of the more exotic trigonometry or math functions found on scientific calculators. Users might also appreciate a help command. Extra credit will be given for these additional operators and functions. (The very adventurous might consider user defined functions!)

The first part of your assignment is to design the input language that will be used to specify commands to the calculator. Your language description should be a formal one; regular expressions to describe the input symbols (such as variable names and real numbers), and a context-free grammar for the syntax of the commands. You will be using top-down parsing for your interpreter, so your grammar should be LL(1). You should turn in a small language manual, containing the regular expression definitions of the symbols used in the language, and a grammar for valid input programs.

The second part of the assignment is to recognize valid input commands. This will require that you first "scan" the input and break it up into "tokens" according to the regular expression description for input symbols. These "tokens" then form the input to your parser procedure. You should use recursive-descent parsing. Your calculator should signal an error whenever the input does not conform to the language specification.

The third part of your assignment is to augment your parsing procedures with code to implement the functionality of each command. Your calculator will essentially "interpret" each command, performing commands as they are recognized. To do so, you will need to maintain the current values of all variables for use in expressions and print statements.

You will be working in two-person teams for this assignment. You may work with whomever you choose, but you must register your team with the TA. Both members of the team will receive the same grade for the project. You may divide the work up in any way you decide, and may work as closely (or as independently) as you like. It would be a good idea to work closely together at least for the first part of the assignment.

Due Date:

This assignment is due at the start of class on Tuesday, April 6th.