CS 261 Lab F - Expression Parsing

Due Wed Feb 25 at 9:00am

Overview

In this assignment, you will write a simple class that can parse and evaluate mathematical expressions represented as Strings, such as: 1 + 2 - (3 * 4). Such evaluation is an effective and common use of a Stack data structure--indeed, many computer languages and compilers use Stacks for this purpose!

This lab will be completed in pairs. Review the pair programming guidelines if you don't remember them!

Objectives

Necessary Files

You will want to download the copy of the zipped source files. and import them into Eclipse. These files include a number of helpful classes for you to use:

This assignment draws heavily from the Koffman and Wolfgang textbook, particularly Chapter 3.4. Indeed, you will be implementing a version of the program described in that section. I highly suggest you have the book handy and use it as a reference :)

Infix, Prefix, and Postfix Notation

Normally we write mathematical expressions using what is called infix notation. We put the operator (e.g., the plus or multiplication sign) in between the operands (the numbers we want to add or multiply). So if we want to add 3 and 4, we write (3 + 4).

This is not the only way of writing mathematical expressions! Alternatively, we could use prefix notation (also called "Polish notation") and write an expression by putting the operator before the operands: if we wanted to add 3 and 4, we would write (+ 3 4). You can think of this kind of like having the operator be the "method" that we're calling, followed by the "parameters"---a variation on writing add(3,4). Indeed, some computer languages (called functional languages) work in exactly this way!

Finally, we could write mathematical expressions using postfix notation (also called "Reverse Polish Notation", or RPN), putting the operator after the operands: adding 3 and 4 is written as (3 4 +). This method of writing expressions makes them very easy to evaluate using a computer and a Stack--and is in fact how many computer systems process mathematics. Note that postfix notation doesn't require any further steps to keep operations in order--no parentheses are needed!

There are more details about these different notations following the above links, in the textbook, and we will talk about them briefly before lab. You can also see this link for a further set of examples.

Assignment Details

Your assignment is to create a ExpressionParser class that is able to evaluate mathematical expressions written in either postfix or infix notation. The class will be able to handle a number of binary operations: addition (+), subtraction (-), multiplication (*), division (/), and exponents (^). The class will be able to handle numbers in double precision, though does not need to handle negative numbers (parsing negative numbers is really hard!) Your program will also be able to handle parentheses, using those to make sure operations happen in order.

Furthermore, your ExpressionParser will be able to handle mathematical variables (eg., x and y)--the user can assign values to these variables, which will be used when the expression is evaluated. So if I assign 4 to x, I should be able to enter "x*(3+2)" and get a result of 20 back.

Read through these instructions carefully, they contain details about how to complete this program!

The ExpressionParser

Postfix Evaluation

Your first step should be to write the method to evaluate postfix expressions. This is a relatively straightforward algorithm using a Stack.

Infix Evaluation

Now that you've gotten the hand of expression evaluation and using a stack, you should implement infix evaluation. This is a significantly more complicated, but still doable.

Testing

As always, be sure and test your code thoroughly to make sure you understand the algorithm at play. Use lots of print statements and inspections to follow what is going on. You want to be able to explain how to use Stacks to evaluate infix expressions!

Be sure and add at least a few tests to the provided ExpressionTester that show you've tested your work.

Submitting

Make sure both your names are on all the classes you've modified (ExpressionParser and ExpressionTester), and upload the entire project directory to the LabF submission folder on hedwig. Only one partner should upload the code. Make sure you upload your work to the correct folder! The lab is due at the start of class the morning after lab.

After you have submitted your solution, log onto Moodle and submit the Lab F Partner Evaluation. Both partners need to submit evaluations.

Extensions

There are plenty of extensions to add to this, though no extra credit this time.

Grading

This assignment will be graded based on approximately the following criteria: