CS 351 Warm-up Exercise


Barney Maccabe Last modified: Sun Sep 5 11:15:07 MDT 1999

Introduction

This goal of this exercise is to review your C++ programming and get you warmed up for the kinds of things you'll need to know for CS 351 this semester.

Due Date: September 7, 1999 at the start of class

Description

You are to write a program that will read an expressions written in prefix notation, build an expression tree, and print the expression in infix notation with a minimal number of parentheses.

Each input expression will consist of operators and operands. The operators are limited to + (binary addition), - (binary subtraction), * (binary multiplication), / (binary division), and ~ (unary negation). Each operand is a sequence of alphanumeric characters (letters and digits).

There will be exactly one space between each element of an expression (operator or operand) and there will be one and only one expression per line of input. The input will be terminated by the end of the input file.

For this assignment, you should assume the normal associativity and precedence of operators: unary minus is highest precedence and is right associative; binary multiplication and division have the next highest precedence and are left associative; and binary addition and subtraction have the lowest precedence and are left associative.

Grammar

The following is a grammar for prefix expressions:

           <expr> :: <operand>
                   | <binary operator> <expr> <expr>
                   | <unary operator> <expr>

        <operand> :: <letter or digit>
                   | <letter or digit> <operand>

<binary operator> :: '*' | '/' | '+' | '-'

 <unary operator> :: '~'

<letter or digit> :: 'a' | 'A' | 'b' | 'B' | ... '8' | '9'
    

Examples

Input                         Output
* + 5 x y                     (5 + x) * y
+ * 5 x y                     5 * x + y
* x * y z                     x * (y * z)
* * x y z                     x * y * z