CS 2500 Programming Assignment # 04 Top Down Design of Program Due November 17, 1995 Program and Test Script Due November 27, 1995 Please read the class documents entitled programAssignmentRules, sampleProgSubmission, and topDownDesignSample (use the Gopher server) before beginning to do this programming assignment. ********************************************************************** Title: Using Recursion to Check for Well-Formed Prefix Arithmetic Expressions One of the jobs of a compiler is to check strings of characters to see if they are syntactically correct arithmetic expressions. For example, 3+4 is syntactically correct, but 3&4 is not. It's a little too much to ask this class to write Pascal code that can do all the expression checking that a compiler can do. However, it will be great to practice by working a simplified syntax problem. Consider the folowing set of rules for forming a valid (prefix) arithmetic expression: 1. An upper-case letter is an expression. 2. If s and t are strings which are expressions, then +st, -st, *st, and /st are expressions. 3. Nothing else is an expression. The set of rules above forces all "numbers" appearing in expressions to be a single upper-case letter, allows no white space in expressions, allows only four arithmetic operations, and no "leading" minus or plus signs. For instance, the following are expressions, according to the rule set above: B, +BR, -D/*ABC, +/+A*BCDE, /AB On the other hand, the following are NOT expressions according to the rule set. -A, + B A, *2P, +B, XY Your assignment is to enhance your solution to programming assignment #3 by adding an expression catcher, a function that reads in character strings and determines whether they represent well-formed expressions. The goal is to modify program #3 so that it can handle input lists that may contain some badly-formed expressions. The new version of the program must test each line of input. If the input string is not an expression, the program must print an appropriate error message instead of the translation of the input string into postfix notation. Aside from that change, all output of the program should be as before. SAMPLE INPUT FILE: +*AB/CD *A+B/CD A*B+C/D -*A+B/CDE *+AB-CD SAMPLE OUTPUT FILE that corresponds to the sample input file above: Prefix: +*AB/CD Postfix: AB*CD/+ Prefix: *A+B/CD Postfix: ABCD/+* Prefix: A*B+C/D ERROR: The string above is not a properly formed prefix expression. Prefix: -*A+B/CDE Postfix: ABCD/+*E- Prefix: *+AB-CD Postfix: AB+CD-* PROCESSING: Your expression catcher MUST do its testing by employing a RECURSIVE Boolean function called Expression, which determines if a character string represents a well-formed expression, according to the rule set. HINTS: It will make the task simpler if you read each string entirely before trying to test it. The first and last character positions of the substring being tested should be parameters of the Expression function. If you wish, you can incorporate the code for the abstract data type (ADT) "string" into your program. You will find this code in the class Gopher space under: CourseDisks/DL_Stuff3/string.pas Having the string ADT in your program will make it simple for you to do various standard operations on strings. Also, some complex operations you need to do on strings may be accomplished by using the operations of the ADT in combinations. If you do need any entirely new string operations, you can add them to the ADT. TESTING: Devise some challenging testfiles, and run your program on them.