# CS 112Polish Notation

The name comes from the Polish mathematician/logician Lukasiewicz, who introduced it. Three types:
• Infix form
• Prefix form
• Postfix form

## Infix form

Is exactly the fully parenthesized notation we have just introduced. Let me remind you once again the Recursive definition

infix-expression := (infix-expression operand infix-expression)
infix-expression := atom

## Examples

(3 * 7)
((1 + 3) * 2)
((1 + 3) * ( 2 - 3))

Main Feature: the binary operator is between the two operands.

Question: what if we do not put all the parentheses? Then there are ambiguities on how to interpret an expression: is 1+2*3 the same as (1+2)*3 or the same as 1+(2*3)? The precedence of operators solves this problem.

## Prefix form

Main Feature: the operator preceeds the two operands.

Recursive definition of fully parenthesized version:

prefix-expression := (operand prefix-expression prefix-expression)
prefix-expression := atom

Recursive definition of classic version, without parentheses (we do not need them, because there is no longer any ambiguity on how to match the operands to the operators):

prefix-expression := operand prefix-expression prefix-expression
prefix-expression := atom

## Examples

(* 3 7) or simply * 3 7
(* ( + 1 3) 2) or simply * + 1 3 2
( * ( + 1 3) ( - 2 3)) or simply * + 1 3 - 2 3

## Postfix form

Main Feature: the operator is after the two operands. Recursive definition

postfix-expression := (operand postfix-expression postfix-expression)
postfix-expression := atom

Recursive definition of classic version, without parentheses (we do not need them, because there is no longer any ambiguity on how to match the operands to the operators):

postfix-expression := operand postfix-expression postfix-expression
postfix-expression := atom

## Examples

(3 7 *) or simply 3 7 *
((1 3 + ) 2 *) or simply 1 3 + 2 *
((1 3 +) ( 2 3 -) * ) or simply 1 3 + 2 3 - *

In class: do several examples of the same expression, in each of the three Polish forms.
Ileana Streinu