(Last Revision: 10/15/98)

//////////////////////////////////////////////////
CS 3100 PROGRAMMING ASSIGNMENT #03
-- TRANSFORMING INFIX TO POSTFIX, using
Information Hiding, Table, Stack, and Queue
//////////////////////////////////////////////////


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DUE: Initial version of the program
     Wednesday, October 28, 1998

(The initial version must include the implementations of the
stack and queue, the main program, the second level functions,
stubs for the third level, and documentation for all functions
except the stack and queue functions.)

DUE: Finished Program
     Friday, November 6, 1998
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


THE ASSIGNMENT:

Write a program that uses a table, 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 on in this document.  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.


SKIPPING OVER WHITE SPACE

Write your code so that it can deal with any amount of blanks,
tabs, and/or newlines before, within, and after the symbols in
the expressions.  


IMPLEMENTATION OF THE STACK AND QUEUE

Your program must use "packages" of code: one that implements a
stack and one that implements a queue.  If you are writing in
C++, you must have a stack class and a queue class.  If you are
writing in some other language, you must do something similar.
We can discuss the details of this in the classroom.  Basically
the requirement is to implement the stack and queue as data
structures and to maintain information hiding.

Your stack must have the following operations: Push, Pop,
StackIsEmpty, and StackIsFull.  If your programming language is
C++, you must have an appropriate constructor.  Otherwise you
must have a stack operation CreateStack.

Similarly, the queue data structure must have: Enqueue,
Dequeue, QueueIsEmpty, QueueIsFull, and perhaps CreateQueue,
depending on the programming language you use.

You are required to create LINKED implementations of the stack
and the queue that use SIMULATED POINTERS, as described in
chapter three of our text.

Note: You can get lots of ideas on what to do by studying the
code in the book that implements stacks, queues, and linked
lists based on simulated pointers.  I'll be happy to work out
examples in class.


INFORMATION HIDING

Here is a rule of information hiding:  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:  (Push, Pop, StackIsEmpty, ..., and so
forth).  You are bound by this rule no matter what programming
language you decide to use.

If we do things that way, what happens if we change the
implementation of the stack?

If we have made the same operations available with the new
implementation, the program will continue to work correctly.
(We also might have to re-compile the program.)

In C++, you can and should enforce this rule by making Push,
Pop, and so forth PUBLIC members of the stack class, and making
all other members of the stack class PRIVATE.  Then if you write
some code that attempts to access private data members, the
compiler will issue an error message and compilation will
fail.  If you program in another language, probably you will
just have to REMEMBER not to access the stack except by using
the operations above, because the compiler will not stop you.

Of course a similar rule applies to the queue, and any other
data structure that you might choose to create for your
program:  The code outside of the implementation is only
allowed to execute the predefined operations of the data
structure.


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 variable in each expression will be a single
upper case letter.  The other meaningful symbols in expressions
will be '$', '+', '-', '*', '/', '(', and ')'.  There will be
no other characters, except blanks, tabs, and/or newlines.  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.  

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 (me) to check
that the rule numbers in the table are correct.

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.

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 table is included in the sample output at
the beginning of this document.  The rule found in the table
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 (i.e. remove the top element and return
a copy of it) 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.

I will be discussing some aspects of the program in class.

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.