Write a compiler for an algebraic calculator instruction set, as discussed in class. This program should repeatedly accept lines of input from the user, compile them into M68000 machine code, and then execute them. Unlike the previous calculator examples, the language should be a civilized one based on the following grammar:
program ::= { statement } end_line
A calculator program is a sequence of 0 or more calculator statements,
followed by an end of line.
statement ::= P expression
| V ( expression ) = expression
A statement either prints the value of an expression or it assigns
a value to a subscripted variable.
expression ::= term { op term }
op ::= + | - | * | / | %
An expression consists of at least one term, followed by an optional
sequence of operators term pairs. The operators are the usual arithmetic
operators. If, following a term, you find an operator, then get another
term!
term ::= number
| ( expression )
| V( expression )
A term is either a number, a parenthesized expression, or a variable.
number ::= digit { digit }
A number is a sequence of 1 or more digits.
Note that spaces should be allowed everywhere but between digits of a number, as discussed in class.
Here are some examples:
input: P5 output: 5 input: V(1)=1+2 PV(1) V (1) = V( 1 )*( V(1) + 1 ) PV(1) output: 3 output: 12Hints: First, each of the above broad gramatical rules can be compiled by a separate procedure. Thus, to compile expressions, you might use: a procedure with this structure:
procedure cexpr -- compiles one expression
op -- a local variable
cterm -- first compile one term
while next character is an operator
op = that operator
cterm -- compile the next term
generate code for op
end while
end procedure
Compiling the code for any number, term or expression should push the value
of that number, term or expression onto the stack. The code generated for
each operator combines the top two items on the stack.
There is only one semantic change you need to make to the instruction set of MP4 to make it easy to map the operations in this set to a subset of the operations of that set. What is it? (That's a homework problem due on the 9th!)