Infix, Prefix and Postfix expression with example

Infix, Postfix and Prefix notations are the ways of writing and evaluating Arithmetic & Algebraic expressions.

Infix notation: A + B

When we write any arithmetic expression in infix notation, operators are written in-between their operands.

For example

(A+B) or A * ( B + C ) / D is in infix notation.


An expression such as A * ( B + C ) / D is usually taken to mean something like: “First add B and C together, then multiply the result by A, then divide by D to give the final answer.”

Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D.

Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction.

Postfix notation (“Reverse Polish notation”): A B +

When we write any arithmetic expression in Postfix notation, operators are written after their operands.

For example

A B C + * D / is in postfix notation

The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the “+” is to the left of the “*” in the example above, the addition must be performed before the multiplication.
Operators act on values immediately to the left of them. For example, the “+” above uses the “B” and “C”.

We can add (totally unnecessary) brackets to make this explicit:
( (A (B C +) *) D /)

Thus, the “*” uses the two values immediately preceding: “A”, and the result of the addition. Similarly, the “/” uses the result of the multiplication and the “D”.

Prefix notation (“Polish notation”): + A B

When we write any arithmetic expression in Prefix notation, operators are written before their operands.

For example

/ * A + B C D is in Prefix notation

As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear:
(/ (* A (+ B C) ) D)

Although Prefix “operators are evaluated left-to-right”, they use values to their right, and if these values themselves involve computations then this changes the order that the operators have to be evaluated in. In the example above, although the division is the first operator on the left, it acts on the result of the multiplication, and so the multiplication has to happen before the division (and similarly the addition has to happen before the multiplication).


Because Postfix operators use values to their left, any values involving computations will already have been calculated as we go left-to-right, and so the order of evaluation of the operators is not disrupted in the same way as in Prefix expressions.

In all three versions, the operands occur in the same order, and just the operators have to be moved to keep the meaning correct. (This is particularly important for asymmetric operators like subtraction and division: A – B does not mean the same as B – A; the former is equivalent to A B – or – A B, the latter to B A – or – B A).

Examples:

InfixPostfixPrefixNotes
A * B + C / DA B * C D / ++ * A B / C Dmultiply A and B,
divide C by D,
add the results
A * (B + C) / DA B C + * D // * A + B C Dadd B and C,
multiply by A,
divide by D
A * (B + C / D)A B C D / + ** A + B / C Ddivide C by D,
add B,
multiply by A

Point to be consider while Parsing Expression

We need to take care of operator precedence and associativity to parse any arithmetic expression.

What is Precedence?

To describe precedence we can say it as priority of operator mean which operator will compute oprend first. Suppose we have two different operators which is in between of operands. Now which operator will take the operand first to perform evaluation, is decided by the precedence of an operator over others.

For example −

a*b+c

Multiplication operator has higher precedence than the addition operator. So a * b will be evaluated first and then (result of a*b) + c will be evaluated.

Associativity

When in an expression there are two operators with the same precedence then we will see the Associativity of operators. Associativity is of two types left Associativity and right Associativity.

For example, in expression a – b + c, both – and + have the same precedence, then which part of the expression will be evaluated first, is determined by the associativity of those operators. Here, both – and + are left-associative, so the expression will be evaluated as (a – b) + c.

Precedence and associativity determines the order of evaluation of an expression.

Operator precedence and associativity table (highest to lowest)

S. No.OperatorPrecedenceAssociativity
1Exponentiation ^HighestRight Associative
2Multiplication ( ∗ ) & Division ( / )Second HighestLeft Associative
3Addition ( + ) & Subtraction ( − )LowestLeft Associative

The above table shows the Precedence and associativity of operators.


[wpusb]