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
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.

Edited 7 Years Ago by KOFSoldier: n/a

Welcome to the real world. There is not free lunch.... sorry.
So
(a) either attend your classes and actually pay attention.
(b) decide that the class is not a good use of time and instead study some books in the library.
(c) Fail your degree/course.

But because this is the real world, I care very very little other than if you pick option (c), you just might be competing with me for a job, and then I will have a little smile on my face.

To go from zero knowledge to that assignment is about 10 hours of reading. (I would figure it is 6 lectures (50min) and corresponding work
(5 hours)). That is one long evening.

[Quick note: option (b) was taken by a a REALLY bright individual at my college and he did really well -- although his invidual exam marks had a huge distribution]

Edited 7 Years Ago by StuXYZ: n/a

Welcome to the real world. There is not free lunch.... sorry.
So
(a) either attend your classes and actually pay attention.
(b) decide that the class is not a good use of time and instead study some books in the library.
(c) Fail your degree/course.

But because this is the real world, I care very very little other than if you pick option (c), you just might be competing with me for a job, and then I will have a little smile on my face.

To go from zero knowledge to that assignment is about 10 hours of reading. (I would figure it is 6 lectures (50min) and corresponding work
(5 hours)). That is one long evening.

[Quick note: option (b) was taken by a a REALLY bright individual at my college and he did really well -- although his invidual exam marks had a huge distribution]

a) I go to class every day, haven't missed a single one of them. My teacher didn't show up for the first three weeks because of contractual problems and now we're playing "Catch Up: Accelerated".

b) Teacher is actually a good teacher, his wording is a little unspecific though. Such as what to name the file I'll be reading in from.

I won't even address C.

Anyone with any actual help? As oppose to criticisms. When I read the "Please read this before posting" topic, it said that people here would help with questions, they just won't do it for you. Which I am fine with.

OK, valid help.

Can you presently write a program that implements a stack of integers, and the operations upon the stack?

Can you write code that will read from a file? Yes, you need to use the fstream library in order to us an ifstream to read the file. Note that some lines of your file will have an instruction and an operand (variable or value) and some may have just an instruction. To deal with this, you could read char by char until either space or newline, and figure out what you've got, or read in the whole line into a char array (string) and parse that (look for a space that indicates presence of an operand)

You'll need a branching mechanism to handle each instruction type, once you've isolated an instruction.

Take a shot at it. Start small, start simple. Get a bit working, then move on to the next part. Probably start with the basic stack, then the file reading.

Show us some work, and we'll move on from there.

Well ok then don't word the problem as if it is verbatim from the question sheet. Doesn't matter were you are tell us what you have got.

The problem can be broken down in to several parts (a) start
(b) read in the input (or a bit of it) (c) process that input. (d) give output.

I put (a) up there because that is were you will want to initialize state and things like that.

Now were are you ??? What have you managed? What specifically have you failed to do ? The basic rules here are post something, pseudo code if you wish, C++ that doesn't work, just about anything.

-- Programming has a lot of unspecifics, if there is no file name then it is a runtime variable. Ie, the user gets to choose, but if it makes it simpler you choose a fixed value e.g "script.txt" Then generalize you program later.

-- When you read the post forum rules it said don't do peoples homework for them but help them understand what they are stuck on.

Hey sorry to reply so late to you all but I actually did get a 100 on it. vmanes advice actually helped alot. Instead of tackling the whole thing head on, I broke down each part individually and kind of combined them in the end. It actually wasnt all that hard and definitely wasn't 6 hours worth of lecturing (but then again, I already took fundamentals).

This article has been dead for over six months. Start a new discussion instead.