Computer Science · Ch 3 — Stack
Notations for Arithmetic Expressions
Notations for Arithmetic Expressions
We write arithmetic expressions in a natural way, placing operators like +, -, *, / between their operands. For example, x + y or 2 - 3 * y. To control the order in which operators are evaluated in complex expressions, we use parentheses ( ). This familiar style is called infix notation, and it is evaluated using the BODMAS rule (Brackets, Orders, Division/Multiplication, Addition/Subtraction).
In the 1920s, Polish mathematician Jan Lukasiewicz introduced a different way of representing arithmetic expressions, known as Polish notation. In this notation, operators are written before their operands, so the order of operators and operands alone determines the result, making parentheses unnecessary. For instance, x + y in Polish notation becomes +xy. This is also called prefix notation, since the operator is prefixed before its operands.
By reversing this logic, we can write an expression by putting operators after their operands. For example, x + y becomes xy+. This is called reverse Polish notation or postfix notation.
To summarise, any arithmetic expression can be represented in any of the three notations: Infix, Prefix, and Postfix. Table 3.1 lists them with examples.
| Type of Expression | Description | Example |
|---|---|---|
| Infix | Operators are placed in between the operands | x * y + z · 3 *(4 + 5) · (x + y)/(z * 5) |
| Prefix (Polish) | Operators are placed before the corresponding operands | +z*xy · *3+45 · /+xy*z5 |
| Type of Expression | Description | Example |
|---|---|---|
| Infix | Operators are placed in between the operands | x * y + z 3 *(4 + 5) (x + y)/(z * 5) |
| Prefix (Polish) | Operators are placed before the corresponding operands | +z*xy 3+45 /+xyz5 |