Question: A stack machine is a simple implementation model for executing computer programs. In fact, in some development environments, there are compilers that translate a high-level
A stack machine is a simple implementation model for executing computer programs. In fact, in some development environments, there are compilers that translate a high-level computer language into a set of instructions to be executed using the stack machine model (e.g., the Java virtual machine instruction set). A stack machine consists of a stack, instructions, operands, a symbol table, and an evaluation model.
Assume that we have a stack machine that supports the following instructions for a hypothetical assembly language (known as HAL) and the associated evaluation criteria for each instruction:
declare x: Inserts an integer variable named x in the symbol table and initializes it to zero (e.g., declare length). A variable must be declared before it can be used in an instruction. A valid variable name may consist of letters and numbers, but it must start with a letter.
read: Pushes a literal integer value read from the keyboard onto the stack (e.g., read).
put a: Pushes the literal integer value a onto the stack (e.g., put 8).
get x: Locates the variable x in the symbol table and pushes its value onto the stack (e.g., get length).
add: Pops the top two values off the stack, adds them, and pushes the result back onto the stack (e.g., add).
multiply: Pops the top two values off the stack, multiplies them, and pushes the result back onto the stack (e.g., multiply).
subtract: Pops the top two values off the stack, subtracts the second operand from the first, and pushes the result back onto the stack (e.g., subract).
divide: Pops the top two values off the stack, divides the first operand by the second, and pushes the result back onto the stack (e.g., divide).
set x: Pops the top value off the stack and assigns it to the variable x in the symbol table.
write: Pops the top value off the stack and writes it to the screen at the current cursor position (e.g., write).
writestring s: Writes the string s to the screen at the current cursor position (e.g., writestring The volume of the box is:).
writenl: Writes a new line character to the screen and positions the cursor at the beginning of the next line (e.g., writenl).
compare: Pops the top two values off the stack and compares them (e.g., compare). The result of the comparison is stored in a special integer variable named status. Specifically, if the two values are equal, it assigns 1 to status. Otherwise, it assigns 0.
jumpequal a: If the value of status is 1, it assigns the value -1 to status and branches to the instruction indicated by the literal integer value a (e.g., jumpequal 3). Otherwise, it just assigns the value -1 to status.
jump a: Unconditionally branches to the instruction indicated by the literal integer value a (e.g., jump 3).
end: Stops the program.
As you can see from the instructions described above, the operands are limited to variable names and literal integer values (except for writestring). All arithmetic operations are integer operations. Together, all of the instructions and the associated evaluation criteria describe the evaluation model.
In this problem, you will develop a program that uses a stack machine to interpret a program written in HAL.
The input to your interpreter program will be a file containing a program written in HAL. Your interpreter program should prompt the user for the name of the file containing the HAL program. You can assume the file contains a complete and correct HAL program. An example HAL program is shown below. Here, each line of the program is associated with a unique, sequential integer (i.e., the line number), where the first line number is 0.
0 declare a
1 declare b
2 declare c
3 put 3
4 set b
5 get a
6 read
7 add
8 set a
9 put 1
10 get b
11 subtract
12 set b
13 get b
14 get c
15 compare
16 jumpequal 18
17 jump 5
18 writestring the sum is:
19 get a
20 write
21 writenl
22 end



0 declare 1 declare 2 declare 3 put 4 set -C get 6 read 7 add 8 set put 10 get 11 subtract 12 set 13 get 14 get 15 compare 16 iumpequal 17jump 18 writestringthe sum is: 19 get 20 write 21 writenl 22 end -C -18 0 declare 1 declare 2 declare 3 put 4 set -C get 6 read 7 add 8 set put 10 get 11 subtract 12 set 13 get 14 get 15 compare 16 iumpequal 17jump 18 writestringthe sum is: 19 get 20 write 21 writenl 22 end -C -18
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
