Question:
I need Help writing a C++ Program?
Deon
2009-10-08 13:42:46 UTC
Will compensate for help.



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
push 10
add
mult pop two operands from the stack, multiply them, and push the result onto the stack push 61
push 38
mult
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
push 8
sub
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
push 10
div
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
push x
assn
dspy pops and prints the value at the top of the stack push 10
dspy

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
push 6
add
dspy

fluffy stack machine instructions
x = 4
y = (7+x)*9
output y push 4
push x
assn
push 7
pushc x
add
push 9
mult
push y
assn
dspy


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
end do


fluffy
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.
Three answers:
rwid
2009-10-08 14:19:12 UTC
Break down the required features into separate classes then have your main loop initialize and cycle through the interpreter instructions until end-of-file. Writing a full parser would be overkill so a simple main loop should be sufficient. Separate classes to encapsulate all file activity, manipulate the symbol table, stack and another for "interpreting" instructions/evaluation model might be best.



You should decide up front which stack architecture you're most comfortable implementing. The array-based is the easiest but will likely have a size limitation (assuming a fixed depth). The linked-list approach to a stack adds minor complexity but can grow on the heap with little limitation (but must check for out-of-memory exception if dynamically enlarging the stack depth). Create methods to handle all aspects when dealing with the stack (e.g. push, pop, display, initialization, standardize error handling for stack under/over-flow). Also, the elements of your stack might have their own classes (e.g. numeric/integer, symbol, float?) inheriting from a base class (e.g. operand).



This assignment's instructions appear comprehensive. Make certain you have methods that cover each requirement aspect. Not too bad of an assignment; should take about three hours to code and test or maybe a little longer if you unit test as you go.



Please let us know where you're having problems and/or if you need further clarification.
2016-04-06 20:25:14 UTC
Why don't you just add a temporary variable? Ok, just thinknig about it, this works: (This will all be pseudo code because I can't recall C) Variables will be called var1 and var2. var1 = var1 + var2 var 2 = var1 - var2 var1 = var2 - var1 Each step being done after the previous one. Hope it helps.
Paul
2009-10-08 13:50:48 UTC
It's really simple - how far have you got with it ? Post your code so far and we can help you to get it working.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...