Professional Documents
Culture Documents
Laboratory Exercise 3: Arithmetic Expression Evaluator Due Date: January 31, 2013, 11:55PM Full Points 100
where | stands for OR and things in single quotes are literals and CR stands for the return key on your keyboard and :: stands for is dened by. Note that there is EXACTLY ONE space between components of the expression and there is NO space after the last number and the PRECEDENCE of the operators is left to right. Also note that there is no space after the optional sign in the rst operand. So, the grammar above tells us that the following are valid expressions 1.34 -2.3 -2.3 1.34 0.34 + + + / * 1.0 4.56 4.56 * 3.5 2.3 * 3.5356445 + 3.3 - 18.39394 4.31 - 3.443
and expressions listed below are INVALID and the reason is given in parenthesis 1 + 3.2 1.0+2.034 0.1345 + 34.3 (all numbers have to be floats. Integers are not allowed) (there has to be a space after 1.0 and +) (too many spaces after 0.1345)
(only the first number can have a sign) (an expression is more than one number) (.394 is not correct, it has to be 0.394) (It should be 30.0)
You should write a SAL program that evaluates the expression and outputs the answer followed by newline. After evaluating one expression, your program should prompt the user to enter another expression. The program should terminate if the input is Q or X. If the input does not meet the rules specied by the expression grammar above, your program should report an error (with a friendly message) and should give the user a chance to enter a new expression from the beginning.
Useful Hints
1. The key idea in this program is to read character inputs from the keyboard using the get ch instruction and convert them into a real number. You should consult the example in Figure 4.7 on page 103 of your textbook to get an idea of how to do that. Note that the example converts ASCII characters into integers. You have to convert them into a real number. 2. The grammar for the expression is also good way to structure your program and you should identify the repetitive parts of the program and encapsulate them into procedures. 3. You will need the SAL instruction cvt x, y, which converts the value in y into the type of x. This is useful to convert an integer into a real number.