2023/10/26 18:08
2023/10/27
Unlimited A empts Allowed
CS 3304 Project #2
In this assignment, you are going to implement an interpreter for (a subset of) the language Forth using Haskell. We begin by describing Forth and then some guidelines on implemen ng your interpreter.
What is FORTH?
Forth is a cute language meant for control and embedded systems applica ons. It comes with a lot of func onality, but for this assignment, you will be implemen ng only a small subset of the en re language. You can read more about Forth at, e.g., the pForth website (h p:/www.so synth.com/pforth/) .
The central en ty in Forth is a stack. You push things onto the stack and do opera ons from there. A Forth program is a bunch of “words” with spaces between them. For instance, the following is a simple Forth program:
All it does is to push the three above numbers onto the stack. 91 is on top and 23 is at the bo om (last in, first out). Here’s another Forth program:
The only difference between this and the previous is the “dot” (.) at the end of it. The “dot” pops (removes) the top element off the stack and prints it. A er this, only two numbers are remaining (23 and 7) on the stack. 7 is on the top.
The next command we will encounter is “.S” (dot-S). Consider the program:
23 7 91 .S
This causes as output “23 7 91”. dot-S prints the en re stack (topmost element is printed rightmost) but doesn’t remove anything.
So, we can put things onto the stack and remove them. The following program leaves the stack empty, but causes as output “91 7 23”.
23 7 91 . . .
We can also do arithme c opera ons on the stack. “+” takes the top two elements off the stack, adds them, and pushes the result back. Similarly for “-”, “*”, and “/”. So,
A empt 1 Add Comment
CS 3304 Project #2 What is FORTH?
In Progress
NEXT UP: Submit Assignment
100 Possible Points
Command Summary
An Interpreter in Haskell Implementa on Guidelines Submi ng Your Program Requirements Implementa on Requirement
Sample Files
causes “9” to be printed and leaves the stack empty.
causes “-1” to be printed and leaves the stack empty. Som
e other useful opera ons in Forth are given now. DUP element of the list.
https://canvas.vt.edu/courses/176190/assignments/1849711
uplicates the topmost
Submit Assignment
2023/10/26 18:08 Project 2
causes “77 77” to be printed (and the stack now contains these two integers). Similarly, SWAP swaps the topmost two elements on the stack.
8 7 SWAP .S
prints “7 8”. DROP drops the topmost element from the stack (unlike “dot”, it doesn’t print anything). OVER causes a copy of the second element on the stack to leapfrog over the first. So,
8 9 OVER .S
causes “8 9 8” to be printed (and remain on the stack). Finally, ROT takes the third element from the stack and moves it to the top.
7 8 9 ROT .S
prints “8 9 7” (and this also remains on the stack). As more examples,
11 22 33 SWAP DUP
causes “11 33 22 22” to be on the stack (doesn’t print anything). Similarly,
11 22 33 ROT DROP
causes “22 33” to be on the stack (doesn’t print anything). For your assignment, you only have to support the above commands (words). Furthermore, you can assume that only integers can be pushed onto the stack. A syntac cally correct FORTH program is simply a string that contains any combina on of integers and FORTH words, separated by white space. When prin ng using “dot” or “dot-S” you can assume that all prin ngs are on the same line, with spaces in between the elements printed.
Command Summary
. pops the top of the stack and displays it.
.S displays the stack with the top on the right
+ * / – performs the math operator on the top two stack operands and pushes the result back on the stack
DUP duplicates the top element on the stack
SWAP switches the first and second two elements on the stack DROP drops the topmost element from the stack
OVER copies the second element in the stack and pushes it on top ROT takes the third element on the stack and puts it on top
An Interpreter in Haskell
The opera on of the interpreter is to read a set of lines from a file named named as the first commandline argument (which will be in the current directory), where each line is a string deno ng a syntac cally correct FORTH program. The interpreter will process this program and return a tuple (a,b) which is printed onto a file named as the second commandline argument. “a” is a string that contains the result of all the prin ngs that happen during the execu on of the program. “b” is a string that iden fies what is remaining on the stack (again topmost element to the right). For each line in the input, there is a corresponding line in the output.
For instance, for the input
11 22 33 ROT DROP .
the output is
(“33″,”22”)
(yes you have to print those brackets and quotes). For the input
11 1 3 4 + 5 7 DROP . .S
the output is
(“5 11 1 7″,”11 1 7”)
If something illegal is a empted, your interpreter should print
(illegal,illegal)
This can happen, for instance, when dot/DUP/DROP is a empted on an empty stack, the arithme c opera ons/SWAP/OVER are a empted on a stack that contains less than two elements, or when ROT is a empted on a stack that contains less than three elements.
If nothing is printed by the program, or if the stack is empty, you should subs tute an empty string (“”) in the appropriate place in the returned tuple.
For instance,
https://canvas.vt.edu/courses/176190/assignments/1849711
Submit Assignment
CS Help, Email: tutorcs@163.com
2023/10/26 18:08 Project 2
(“2 3″,””)
Implementation Guidelines
Your implementa on must adhere to the tenets of func onal programming. Specifically, your program must u lize recursion and higher-order func ons to the fullest extent. It must also be free of side effects with the excep on of input/output.
You may not use any impera ve features of Haskell (remember the language does have some impuri es). You may use let s however as they do not run contrary to the spirit of func onal programming. Absolutely no itera on is permi ed (use recursion).
Adopt an itera ve development approach. E.g., prepare your interpreter to first handle simple pushing and popping off stack, and then proeed to add the more complex FORTH words.
You might want to check out the Haskell supplied Prelude (h ps:/hackage.haskell.org/package/base-4.14.1.0/docs/Prelude.html) to see if there are any useful func ons and datatypes provided. See also the code from the lecture summaries. (These will be coming soon, i.e. next week at the latest)
Submitting Your Program
Your Haskell implementa on is to be organized in a single file. The file will use a .hs extension. Haskell is installed on rlogin. I’m pre y sure you can get easy to install Haskell installers for all major OSs.
All documenta on and iden fying informa on should be placed in comments within the single source file. The comments at the beginning of the file should iden fy the assignment and give your full name. Every Haskell func on should be preceded by comments explaining its purpose, the meaning of each parameter, and the general strategy of its implementa on if applicable.
Your program will be submi ed electronically to Canvas. It will be scored 75 points for correctness and the TAs will assess the remaining 25 points based on the following requirements
Requirements
1. Your program must take 2 commandline arguments. a. The first is the name of the input file to use.
b. The second is the name of the output file to use.
2. You must use a recursive func onal approach.
Do not use itera on, with the excep on of line process. See the homework 5 main for ideas.
3. You must comment your code and each func on as described above. 4. This is an individual assignment.
5. You must follow the Implementa on Requirement as stated below.
Implementation Requirement
Do not reverse the list and process it “backwards”. Simply process the head and then process the tail. This is how func onal programming wants to operate and if you a empt to force a procedural approach your going to have a hard me.
Sample Files
samples.zip (h ps:/canvas.vt.edu/courses/176190/files/29188107?wrap=1) (h ps:/canvas.vt.edu/courses/176190/files/29188107/download? download_frd=1)
You will receive at most 75 points from the grader. The rest of the 25 points will come from mee ng the stated requirements. These points will be assigned a er the due by the TAs.
Choose a submission type
Submit Assignment
https://canvas.vt.edu/courses/176190/assignments/1849711 3/4
程序代写 CS代考 加QQ: 749389476
2023/10/26 18:08 Project 2
Canvas Files
Choose a file to upload
File permi ed: HS
https://canvas.vt.edu/courses/176190/assignments/1849711
Submit Assignment
Computer Science Tutoring