infix postfix prefix expressions
Infix Notation
Infix is the day to day notation that we use of format A + B type. The general form can be classified as (a op b) where a and b are operands(variables) and op is Operator.
Example 1 : A + B
Example 2 : A * B + C / D
Postfix Notation
Postfix is notation that compiler uses/converts to while reading left to right and is of format AB+ type. The general form can be classified as (ab op) where a and b are operands(variables) and op is Operator.
Example 1 : AB+
Example 2 : AB*CD/+
Prefix Notation
Prefix is notation that compiler uses/converts to while reading right to left (some compilers can also read prefix left to right) and is of format +AB type. The general form can be classified as (op ab) where a and b are operands(variables) and op is Operator.
Example 1 : +AB
Example 2 : +*AB/CD
infix:
A*B+C/D
postfix:
AB*CD/+
prefix:
+*AB/CD
Postfix/Prefix Expressions are faster than Infix:
For Infix Expression which is format A+B*C, if the compiler is reading left to right then it can’t evaluate A+B first until it has read whole expression and knows expression is actually A + (B*C) i.e. B * C needs to be implemented first
Postfix for above infix is ABC*+. Now, as soon as compiler sees two operands followed by operator it can implement it without caring for precedence.
Assume ABC*+
ABC*+ (BC* is implemented as B*C and result is put back)
AX+ (Assuming X stores result of BC* i.e. B*C)
Now finally AX+ can be implemented as A+X
No comments