Spring 2022
CS 3304: Comparative Languages Project 2
Postfix Evaluator in Functional Language
Due: Mar 29th, 2022 at 11:59PM on Canvas Max Points: 100
Copyright By PowCoder代写 加微信 powcoder
Total Pages: 3
This project is a continuation of our first project. For this project, we are going to be implementing a postfix calculator using the Haskell language. The input will be processed line by line and the result will be returned, assuming there are no errors. If there are errors, they will also be reported. Any error messages that are in the input will simply be passed along as is.
Basic Algorithm
For each line in the input
For each token in the line
If the token is an operator
pop two numbers from the stack
perform the operations on the operators push the result back on the stack
else the token is a number
push the number on the stack
The final number on the stack is the answer for the equation
A few things can go wrong when evaluating the expression
1. Whenwepopthestackandtherearelessthan2elementsonthestack
2. Whendividing,therecouldbeadivisionby0.
3. Whenyouaredoneprocessingthelineandtherearemorethan1elementonthe
Each of the above is an error and will have error messages as indicated below. How to perform the operations
When popping from the stack, the second operand comes off first and the second first operand comes off second. Then the operation is performed as such
Spring 2022
operand1 operation operand2
This is not a big deal for the operations of multiplication and addition but it is extremely important for division and subtraction. So please pay attention to the order of popping and application.
There are 3 errors that this project will detect and report.
1. If you attempt to pop the stack and you cannot pop 2 operands, then you will output
¡°ERROR: Too few operands¡±.
2. If at the end of processing the line, there is more than 1 number on the stack, you will
output ¡°ERROR: Too few operations¡±.
3. If you pop the stack for division and operand2 is 0, you will output ¡°ERROR: Attempted
division by 0¡±.
Starter Code
The starter code is available for this project. It is meant to get you started with the input and output and with the command line parameters. Since Haskell relies on indentation to know the nesting, I encourage you to download the code file rather than copy and paste. If you do copy and paste make sure you are using spaces not tabs and that you have the same indentation as the example.
Please implement your solution in evaluate function. You are welcome to create as many helper functions as you¡¯d like. evaluate takes in a list of tokens and must return the output in a string format.
To run the program
1. Compile calc.hs with the following command which will produce an executable calc
$> ghc calc.hs
2. Execute calc with your input file, input.txt, and path to the output file, output.txt. $> ./calc input.txt output.txt
The input for this project is simply the input from the last project.
Too few closing parenthesis 849960/74*+358029 *-
Too few closing parenthesis 713170/73*- – 9413/895*58*+6420*22*+ +
Spring 2022
Sample output is also provided for the given sample input. If the input is an error message simply output that message. If it is not an error, then output the result.
Restrictions
1. You may not import any modules.
1. Modules are not needed for this assignment. Use what is in the prelude.
2. Your program must get the input and output file names as command-line arguments.
1. The first argument will be the input file.
2. The second argument will be the output file.
3. You must write a function for each of the operations.
4. You may not use any non-functional approach, i.e. forcing an imperative style on this
5. You must use guards instead of if-else statements.
6. Include the honor code in your work.
7. This is an individual assignment.
< - - - - - - - - - End of the document - - - - - - - - - >
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com