Discussion Assignment #3: Playing with the Postfix Evaluator

28 September 2011

Last Monday in lecture we reviewed the postfix evaluator from Section 3.5 of L&C. Today we will play with translating and evaluating postfix expressions.

Question 1:

The first set of questions asks you to evaluate some postfix expressions. Some are valid and some are not -- identify which ones are invalid and why, and evaluate the valid ones:

• (a) ` 1 1 + 1 + 1 1 + + 1 1 + 1 + *`

• (b) ` 2 3 + 1 * / 3 2 1 + 4 *`

• (c) ` 3 1 - 4 * 2 4 * 7 + 3 3 -`

• (d) ` 7 6 + 5 * 4 3 2 - + 1 - * *`

Question 2:

Translate the valid postfix expressions from Question 1 into infix expressions.

Question 3:

Next we will translate some infix expressions into postfix. (If you have time, evaluate these as well.) Remember the rule -- find the operator to be executed lasst, consider the infix experession as "R op S", translate R to R' and S to S' by the same rule, then write "R' S' op".

• (a) ` (1 - 2 + 3) * (2 * 3 + 4) - 8 / 2 + 3 + 5`

• (b) ` (1 + 2 * (1 + 2 * (1 + 2 * (1 + 2 * 3))))`

• (c) ` 7 / (2 + 2) + (3 * 7 - 1) / 5`

• (d) ` (((((7 / 6) / 5) / 4) / 3) / 2) / 1`

Question 4:

Every valid postfix expression has one more number than it has operators -- explain why.

Question 5:

An expression with k operators and k+1 numbers (and no parentheses) may or may not be a valid postfix expression. How can you tell?