(Latest Revision: 05/05/2000)
05/05/00: Clarified the statement of the algorithm.
05/05/00: Added the grammar for generating a prefix expression.

Utilizing a Stack: Prefix to Postfix Expression Conversion


THE ASSIGNMENT:

Your assignment is to write a program that converts a series of prefix expressions to postfix.


INPUT:

The program will not explicitly read input from any file. The program will read from standard input. By default standard input is the keyboard. However under unix one can redirect standard input so that it comes from a file instead.

The input contains a series of zero or more error-free, arithmetic expressions in prefix notation. The expressions are made up of two types of symbols:

(1) operands -- an operand is a single upper-case letter, and

(2) operators -- the operators allowed are +, -, *, and /.

There are no blank characters in the input.

Here is a small sample of the kind of input the program is required to deal with:


+*AB/CD
*A+B/CD
-*A+B/CDE
*+AB-CD
Here is a (recursive) grammar for generating any of the allowed prefix expressions:

  1. The symbol x is a prefix expression if x is any upper case letter.
  2. If e and f are prefix expressions, then the following are also prefix expressions:
    
        +ef
        -ef
        *ef
        /ef
    
We can discuss in class how to use this grammar to generate prefix expressions of any given size.


OUTPUT:

The program will not explicitly write output to any file. The program will write to standard output. By default standard output is the computer display screen. However under unix one can redirect standard output so that it goes to a file instead.

The output written must be like the sample below. It must contain all the prefix expressions from the input, in the same order they have in the input, appropriately labelled with the word "Prefix". After each prefix expression, the corresponding postfix expression must appear immediately below, also appropriately labelled, with the word "Postfix".

Here is the sample output that corresponds to the sample input above:


Prefix:      +*AB/CD
Postfix:     AB*CD/+

Prefix:      *A+B/CD
Postfix:     ABCD/+*

Prefix:      -*A+B/CDE
Postfix:     ABCD/+*E-

Prefix:      *+AB-CD
Postfix:     AB+CD-*
Note: Although your program will not process or print infix, here are the four infix expressions that correspond to the forms above:


A * B + C / D
A * (B + C / D)
A * (B + C / D) - E
(A + B) * (C - D)

DISCUSSION OF DATA STRUCTURES:

You will need to employ a stack. The elements stored in the stack will be C++ structs. Each struct will have two fields: a character and a boolean flag. The assignment directory contains code for a generic stack. You will rewrite the generic stack code slightly to adapt it for use in this program.


DISCUSSION OF ALGORITHM:

Below is a description of a simple prefix-to-postfix conversion algorithm that works on one input expression:

The Algorithm

Initialize the input string, the output string, and the
operators stack.

DO
{
  Get the next input symbol ;
  IF   it is an operator
       push it onto the stack with its associated flag off ;
  ELSE IF it is an operand
       {
         append it to the output string ;

         What happens next can be divided into three
	 cases, depending on what is on the stack:

         CASE 1. The stack is empty:
           do nothing.

	 CASE 2. The stack is not empty and there is at
	 least one element in the stack that has a flag
	 == false:

	   Let x be the element with flag == false nearest
	   to the top of the stack.  Pop any elements
	   above x and adjoin the characters in turn to
	   the end of the output string.  Change x's flag
	   to true.  Leave x as the top element of the
	   stack.  Do not put the character in x on the
	   output string at this time.

         Case 3. The stack is not empty and there are no
	 elements in the stack that have a flag == false:

     	   Just remove all elements from the stack and
	   adjoin the characters in turn to the end of the
	   output string.
       }
}
WHILE (there are remaining input symbols and
              the stack is not empty)
NOTE:

  1. If there is no error in the input, both termination conditions will become true at the same time: the stack will become empty and the end of the input string will be reached.

  2. The version of the algorithm given above is correct but do not take it too seriously. Your task is not to translate the algorithm word-for-word into C++ code. Your task is to write a well-structured C++ program that performs the same task as the algorithm given.



EXAMPLE RUN OF ALGORITHM:

Here is a hand simulation of the first sample input expression given above. The rightmost symbol under "Stack" is the top operator. Parentheses around an operator denote that the associated flag is on. The "Stack" and "Out so far" columns show the the contents of the stack and the output string following the processing of the symbol in the "Next prefix symbol" column.


Next prefix symbol		Stack		Out so far
	+			+
	*			+ *
	A			+ (*)		A
	B			(+)		A B *
	/			(+) /		A B *
	C			(+ /)		A B * C
	D					A B * C D / +

What To Turn In:

Please: It will really help me a lot in sorting my mail if you use exactly the subject line given. (Note there are no spaces.)


DUE DATES:

For due dates, see the class schedule.