Save your program in a file called proj.ml
in folders called proj1
and proj2
respectively for phases 1 and 2 of your project.
I will test your code in the same environment as the lab
machines in MI 302, using the commands
/usr/bin/ocaml proj.ml
For phase 2 of your project, you will implement an interpreter for
a simple RPN calculator language, described below.
You should submit your program in a folder called proj2
, and be sure
that it runs as described above.
Your program will read characters from standard in, scan them into tokens, and
interpret the tokens in the RPN language described below. There are only three
tokens: numbers (which can include both floating-point numbers and integers, with
or without -
signs), variables (any string of letters, uppercase or
lowercase), and operations (described below). You should use the built-in regular
expression facilities of Ocaml to scan the input for these tokens.
As usual, tokens can be separated by any amount of whitespace.
Your program will be an interpreter for a simple Reverse Polish Notation (RPN) calculator. The basic syntax of this language is that we have some operands, which for us will be either numbers or variable names, and operators, which will be detailed below. A program for this calculator is just a series of operands and operators.
RPN calculators work by maintaining a single stack of operands. Whenever the next token is an operand, we just push it onto the stack. If the next token is an operator, we pop off some elements of the stack, apply the operator, and push the result of the operation back onto the stack. You can Google RPN to get lots more information.
The operators in your RPN language will be as follows:
p
Prints the top element of the stack and removes
it.
+
Adds the top two elements of the stack, replaces them with
their sum.
-
Subtracts the top element of the stack from the
second-to-top element, and replaces them with their difference.
*
Multiplies the top two elements of the stack, replaces them
with their product./
Divides second-to-top element of the stack by the
top element, and replaces them with their quotient.
d
Duplilcates the top element of the stack by removing
it and then adding it back on twice.
s
Sets the variable, which should be second-to-the-top in
the stack, with the value of the top element in the stack. These two elements are popped
off of the stack and replaced by a single instance of the variable itself.
<
Tests whether the second-to-the top element is less than
the top element. If so, these are replaced with the number 1
, and
of not, they are replaced with 0
.
>
Tests whether the second-to-the top element is greater than
the top element. If so, these are replaced with the number 1
, and
of not, they are replaced with 0
.
=
Tests whether the second-to-the top element is equal to
the top element. If so, these are replaced with the number 1
, and
of not, they are replaced with 0
.
?
This is the ternary "if" operator. If the element on top of
the stack is nonzero, the top three elements are popped off and replaced with
the second-to-the-top element. Otherwise, if the top is zero, the top three are
popped and replaced with the third-to-the-top element.
To implement this, you will need regular expressions for all the token types, and you will also need to maintain a stack as usual for RPN calculators. Your stack will contain both numbers and variable names. To implement the variables, you will of course need to maintain a symbol table as well.
You are free to implement this in any way you wish. However, I recommend you proceed in the following steps.
getValue()
, which will either return
the number or look up the variable in the symbol table, and setValue(...)
,
which will return an error if we have a number, and otherwise modify the symbol table
accordingly.
You do not have to create your program in the exact way that I have suggested. However, for full credit your program should
After your RPN interpreter is working, I should be able to type in
5 2 + p
and your interpreter should print 7
.
Here's a more complicated example:
2 3 d * d 4 + p - p
should print
13 -7