CS 2500 Programming Assignment #2 Information Hiding, Stacks, and Queues LEVEL ONE PROGRAM DUE: Saturday, October 25, 1997 FINISHED PROGRAM DUE: Saturday, November 01, 1997 THE ASSIGNMENT: Add code to the program, infix.shell.p. Make the program use a stack and queue to translate infix expressions to postfix expressions. Here is an example of an input file: $ (T + Q/H - U*K) - (T-B) /L*A $ $ (A + B ) / ( C - D) $ $ B * B - F * A * C /(T + A ) $ $ X * (X) + R - F / G $ $ X * (X + R - F / G $ Here is the output file corresponding to the input above: Rule Number Table Row corresponds to stack top. Column corresponds to current input character. $ + - * / ( ) $ 4 1 1 1 1 1 5 + 2 2 2 1 1 1 2 - 2 2 2 1 1 1 2 * 2 2 2 2 2 1 2 / 2 2 2 2 2 1 2 ( 5 1 1 1 1 1 3 $ ( T + Q / H - U * K ) - ( T - B ) / L * A $ T Q H / + U K * - T B - L / A * - $ ( A + B ) / ( C - D ) $ A B + C D - / $ B * B - F * A * C / ( T + A ) $ B B * F A * C * T A + / - $ X * ( X ) + R - F / G $ X X * R + F G / - $ X * ( X + R - F / G $ Bad Expression. As you can see, the program first prints a table. You will be told more about the table later. Next the program reads an infix expression and writes it on the output. The program then, if possible, writes the translation of the expression. If the infix expression contains an error, it is not possible to make a translation. In that case the program writes the message "Bad Expression". This continues until all the infix expressions have been processed. THE PROGRAM SHELL Save a couple of copies of infix.shell.p. One copy should also be named infix.shell.p, and the other should be called something like prog3.p. (If you are programming in a language other than Pascal, please have a word with me.) You must then add some code to prog3.p, to transform it into the solution to this programming assignment. Please read through prog3.p. You will see comments that tell you where to add code. SKIPPING OVER WHITE SPACE Prog3.p, contains a procedure called SkipWhiteSp. You can add calls to SkipWhiteSp to solve any problems you have with skipping over white space. IMPLEMENTATION OF THE STACK AND QUEUE Prog3.p also contains code that implements a stack and queue. The code you ADD to prog3.p must be "implementation independent". It must be written in such a way that it will continue to work if the code implementing the stack or queue is changed. This is very important. It will be a big factor in determining your grade on the program. If you follow the principles of information hiding and encapsulation of data structures properly, then your program will work, with absolutely no changes, when you switch from one implementation of a stack or queue to another. How do you "follow the principles of information hiding and encapsulation of data structures"? Well, if you have read Chapter Three of your text book, you can probably answer that question. Please review that material. Here is ONE EXAMPLE of how information hiding will affect your program: Except for the actual implementation code of the stack, all parts of your program that use the stack must do so ONLY by making calls to the formal stack operations: CreateStack, DestroyStack, EmptyStack, FullStack, Push, and Pop. Since all allowable implementations of the stack will make these operations available, the program is guaranteed not to stop working correctly if we change the implementation of the stack. ANOTHER EXAMPLE: It would be a violation of information hiding to have a reference to "stack.Elements" in your main program, or in any part of your program other than the implementation code of the stack. This is because if your stack is implemented as a record with a field called "Elements", that is an "implementation detail" that could CHANGE if the implementation of the stack were changed. If you have a reference to something called "stack.Elements" in a portion of your program which is not implementation code of the stack, and if the old implementation code is then replaced with new implementation code, then the program CAN stop working correctly, because there may no longer be such a thing as "stack.Elements", or there may be such a thing, but it may now be very different from what it was before the replacement. INPUT AND OUTPUT OF THE PROGRAM The program must read only from standard input and write only to standard output. When I test your program I will use a prepared input file and indirection in the following manner: a.out < infix.in > infix.out Assume that the input will contain a series of infix expressions. Each expression will be on a line all by itself. Each variable in each expression will be a single upper case letter. The other meaningful symbols in expressions will be '$', '+', '-', '*', '/', '(', and ')'. All other characters must be treated as white space. The dollar sign ('$') will be the first and last symbol in every expression. Those two dollar signs will be the ONLY dollar signs in any expression. Any amount of white space is permitted to appear at any location in the input. Blank lines are permitted anywhere. There are no other rules about how the input must appear. You have to write the code of your program so that the program works correctly on ANY input that conforms to the rules above. You can do this by writing code that always makes a call to SkipWhiteSp before attempting to read a black space character. See the sample input above, and create some good test input files for your own use in verifying that your program works. ACTIONS OF THE PROGRAM The program begins by writing the table of rule numbers it is using. This is just a way of allowing the user to check that the correct rule numbers have been recorded. The method the program will use to translate infix formulas is due to E.W. Dijkstra, and is reported on page 222 of Andrew Tanenbaum's book, "Structured Computer Organization". There are just a few action rules for doing translations. This tends to make translation rather easy. However, one must decide which action rule to apply, and when to apply it. Most of the logic required to make such decisions is already built into the program shell (prog3.p) that I am giving to you, but you will have to add some more code to make it work. Directions for this are in the program shell. The program employs a stack and queue. Before starting to translate each expression the program must make sure that both the stack and the queue exist and are empty. A translation always begins by reading the initial '$' and pushing it onto the stack. During translation, any variable read from the input is immediately enqueued. Let's agree to use the name "current symbol" for the black space character most recently read from the input. When the current symbol is NOT the initial dollar-sign, and is NOT a variable, the program consults a table in order to get the number of a rule to apply. The rule tells what the program must do next in order to go forward with the translation. The rule number lies in the ROW of the table corresponding to the symbol on the top of the stack, and the COLUMN of the table corresponding to the current symbol. Although there is a large number of combinations of stack top symbol and current symbol, there are only 5 rule numbers. Here are the rules: RULE 1. Push the current symbol onto the stack and read another black space character from the input. RULE 2. Pop the stack and enqueue the symbol that comes out. Do NOT read a new black space character from the input. RULE 3. Pop the stack and read another black space character. Do NOT save any characters, or put anything in the queue at this time. RULE 4. Stop. The symbols now on the queue represent the postfix translation of the infix expression. RULE 5. Stop. An error in the input has been discovered. Read through your copy of prog3.p until you understand it well. Then add segments of code to finish it. I will be discussing some aspects of the program in class. Be sure to spend time reading your copy of prog3.p, otherwise you probably will not get much out of the class discussion. The remaining details of how you implement this program are left to you. However, it should be done according to the rules of good programming you have learned, and the items printed should be formatted attractively and clearly.