The instructions of a stack based virtual machine use a stack from which their arguments are fetched, and they replace the arguments with the result of the operation. Consider a stack based virtual machine which supports the following instructions:
Write a program which accepts an arithmetic expression, and generates as output, the instructions needed by this virtual machine to evaluate the expression.
| 1) |
Input:
3-2
Output:
PUSH 3
PUSH 2
SUB
|
| 2) |
Input:
1/5
Output:
PUSH 1
PUSH 5
DIV
|
| 3) |
Input:
((5-2)*3+7)*4
Output:
PUSH 5
PUSH 2
SUB
PUSH 3
MUL
PUSH 7
ADD
PUSH 4
MUL
|
| 4) |
Input:
(1-(2-3)*9)/2
Output:
PUSH 1
PUSH 2
PUSH 3
SUB
PUSH 9
MUL
SUB
PUSH 2
DIV
|
| 5) |
Input:
(9-(8-(7-(6-5))))
Output:
PUSH 9
PUSH 8
PUSH 7
PUSH 6
PUSH 5
SUB
SUB
SUB
SUB
|