Me and a friend have been trying desperately to get this done, but we barely know where to start. We know we need the ifstream to read in from the file, but that's about it. Anyone got any tips for us, please? (Note: we're both fairly new to programming, so please put it as simple as possible)
A stack machine is a model for executing computer programs. There are compilers that translate a high-level computer language into a set of instructions to be executed using a stack machine model. A stack machine consists of operands, instructions, a stack, and an evaluation model.
Consider the following stack machine instructions and their purpose.
INSTRUCTION PURPOSE EXAMPLE
Push- push the value onto the stack push 8
Pushc- push the contents of the variable onto the stack push x
Add- pop two operands from the stack, add them, and push the result onto the stack push 6
Mult - pop two operands from the stack, multiply them, and push the result onto the stack push 61
Sub pop two operands from the stack, subtract them (top element is subtracted from the second from the top), and push the result onto the stack. push 5
Div pop two operands from the stack, divide them (top element is divided by the second from the top), and push the result onto the stack. push 5
Assn pops two values from the stack and assigns the value of the top element to the variable that is second from the top push 4
Dspy pops and prints the value at the top of the stack
Imagine a hypothetical compiler that translates a computer program written in a language called fluffy (funny language used for fun yall) into an equivalent set of stack machine instructions. These instructions are then executed by the stack machine's evaluation model to carry out the meaning of the computer program. Here are example fluffy programs and their equivalent stack instructions.
fluffy stack machine instructions
output 8 + 6 push 8
fluffy stack machine instructions
x = 4
y = (7+x)*9
output y push 4
For this assignment you are to write a program that will act as an interpreter for stack machine instructions. The input to your program will be a file containing stack machine instructions. Your program will execute those instructions. Any output generated by the instructions should display to the screen.
The basic algorithm for a stack machine evaluation model is:
1. Create a stack
2. Create a symbol table for the variables x,y, and z. This is an array of size 3. Array position 0 is for x, position 1 is for y, and position 2 is for z.
2. while there are more instructions to evaluate do
get next instruction
if the instruction is 'push' then push the operand onto the stack
if the instruction is 'pushc' then get value of operand from the symbol table and push it onto the stack
if the instruction is 'add', 'mult', 'div', or 'sub', pop two operands from the stack and perform the operation. push the result back onto the stack
if the instruction is 'assn' pop two operands from the stack, put the value of the top element in the symbol table for the second top element
if the instruction is 'dspy' pop an operand from the stack stack and print it to the screen
To run your program, the user will be prompted for a file name containing a list of instructions.
Your program will open the file, and read and execute the instructions, sending any output to the screen. You can assume the file contains an error-free and correct set of instructions.
Use either a Linked Stack or an array-based Stack to implement this program.