Problem

In the language Lisp, each of the four basic arithmetic operators appears before an arbitr...

In the language Lisp, each of the four basic arithmetic operators appears before an arbitrary number of operands, which are separated by spaces. The resulting expression is enclosed in parentheses. The operators behave as follows:

• (+ a b c ...) returns the sum of all the operands, and (+) returns 0.

• (- a b c ...) returns a - b - c - ..., and (- a) returns -a. The minus operator must have at least one operand.

• (* a b c ...) returns the product of all the operands, and (*) returns 1.

• (/ a b c ...) returns a / b / c / ..., and (/ a) returns 1 / a. The divide operator must have at least one operand.

You can form larger arithmetic expressions by combining these basic expressions using a fully parenthesized prefix notation. For example, the following is a valid Lisp expression:

(+ (- 6) (* 2 3 4) (/ (+ 3) (*) (- 2 3 1)))

This expression is evaluated successively as follows:

(+ (- 6) (* 2 3 4) (/ 3 1 -2))(+ -6 24 -1.5)16.5

Design and implement an algorithm that uses a stack to evaluate a legal Lisp expression composed of the four basic operators and integer values. Write a program that reads such expressions and demonstrates your algorithm.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 5
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT