CS239 Advanced Programming

James Madison University

Software Requirements Specification

Programming Assignment 5

Expression Evaluator

Final Program Due by April 9 (Wednesday).  Hardcopy due on April 10 at the beginning of class.


Introduction

Purpose: This application will provide a tool to evaluate an expression in the Latte language.  The application must read the expression, decide if it can be  evaluated according to the syntax of the language and if so produce a result.  You must use a recursive solution to this problem.

Deadlines

Background

Programming languages like natural languages have a grammar associated with them that can describe the language rules.  For example, in Java, one can define a declaration statement as a primitive type name followed by a variable or a class name followed by a variable.  A variable can be a single letter or a single letter followed by one or more letters, digits, or special symbols.  Special symbols include - and _ only, etc.  Some grammars are by their nature recursive.  Think about English.  A noun phrase consists of a noun or an adjective followed by a noun phrase.  big, red fox for example is an adjective, big, followed by a noun phrase which is an adjective red followed by a noun phrase which is fox.  The base case is the single noun and the refinement involves stripping off each adjective until we reach the single noun.

In this assignment, you will take a grammar and parse it to discover if the grammar is legal in the language and then evaluate it to achieve its result.

Existing program components

Your application will be written from scratch.  You may use tools found in the standard Java libraries.  You must have a Driver and an ExpressionEvaluator class.  You may have others.

Input

Your application will read a series of expressions entered at the keyboard.  The application will prompt the user for this input.  Execution ends when a return statement (no data is entered) is issued from the keyboard.

Output

  1. The application will open with the single line "Welcome to the Latte Evaluation Calculator".  
  2. Output one blank line.
  3. Prompt with the statement: "Enter an expression.  Press <enter> to quit: "  The cursor should advance to the next line for the input of the expression.
  4. Output a blank line before displaying the result.
  5. If the expression can be evaluated, output the expression followed by " = " followed by the result.  All results should be expressed as integers  If the expression cannot be evaluated, output the expression followed by " = Bad Result".  
  6. Follow the output with a blank line and repeat from step 3 until the user presses enter (no input).
  7. Output the final line, "Thank you for using the Latte Evaluation Calculator"

Valid Expressions 

This picture shows the grammar of Latte expressions.

Examples of valid expressions are
3 A 2 M 7
( 3 S 2 ) D 1
7 S 2 A 5 M ( 3 R 2 )
7S2A5M(3R2)

Whitespace should be ignored.  A valid expression can only contain the operators A, S, M, D, and R, the digits 0 - 9, and the parentheses ().  Case is important.  a is not the same as A.

A Add (like +)
S Subtract (like -)
M Multiply (like *)
D Divide (like /)
R Remainder (like %)

Error Handling

If an invalid expression is entered, you will output the message BAD RESULT in place of the actual result.  See output specs above.

Hints and Suggestions

Timesheet

You may choose to keep a timesheet for yourself, but it is not a requirement of this assignment.

Honor Code

This work must conform to the JMU Honor Code and the specific requirements of this class. You may have an opportunity to work with your team in class on this assignment but only in the confines of the classroom.  Any other help must come from the professor or the TAs for this class.  See the collaboration policy for further information.

Grading