Convert infix to prefix notation
What is infix notation?
An infix notation is a notation in which an expression is written in a usual or normal format. It is a notation in which the operators lie between the operands. The examples of infix notation are A+B, A*B, A/B, etc.
As we can see in the above examples, all the operators exist between the operands, so they are infix notations. Therefore, the syntax of infix notation can be written as:
<operand> <operator> <operand>
Parsing Infix expressions
In order to parse any expression, we need to take care of two things, i.e., Operator precedence and Associativity. Operator precedence means the precedence of any operator over another operator. For example:
A + B * C → A + (B * C)
As the multiplication operator has a higher precedence over the addition operator so B * C expression will be evaluated first. The result of the multiplication of B * C is added to the A.
Associativity means when the operators with the same precedence exist in the expression. For example, in the expression, i.e., A + B - C, '+' and '-' operators are having the same precedence, so they are evaluated with the help of associativity. Since both '+' and '-' are left-associative, they would be evaluated as (A + B) - C.
Let's understand the associativity through an example.
1 + 2*3 + 30/5
Since in the above expression, * and / have the same precedence, so we will apply the associativity rule. As we can observe in the above table that * and / operators have the left to right associativity, so we will scan from the leftmost operator. The operator that comes first will be evaluated first. The operator * appears before the / operator, and multiplication would be done first.
1+ (2*3) + (30/5)
1+6+6 = 13
What is Prefix notation?
A prefix notation is another form of expression but it does not require other information such as precedence and associativity, whereas an infix notation requires information of precedence and associativity. It is also known as polish notation. In prefix notation, an operator comes before the operands. The syntax of prefix notation is given below:
<operator> <operand> <operand>
For example, if the infix expression is 5+1, then the prefix expression corresponding to this infix expression is +51.
If the infix expression is:
a * b + c
+*abc (Prefix expression)
Consider another example:
A + B * C
First scan: In the above expression, multiplication operator has a higher precedence than the addition operator; the prefix notation of B*C would be (*BC).
A + *BC
Second scan: In the second scan, the prefix would be:
In the above expression, we use two scans to convert infix to prefix expression. If the expression is complex, then we require a greater number of scans. We need to use that method that requires only one scan, and provides the desired result. If we achieve the desired output through one scan, then the algorithm would be efficient. This is possible only by using a stack.
Conversion of Infix to Prefix using Stack
K + L - M * N + (O^P) * W/U/V * T + Q
If we are converting the expression from infix to prefix, we need first to reverse the expression.
The Reverse expression would be:
Q + T * V/U/W * ) P^O(+ N*M - L + K
To obtain the prefix expression, we have created a table that consists of three columns, i.e., input expression, stack, and prefix expression. When we encounter any symbol, we simply add it into the prefix expression. If we encounter the operator, we will push it into the stack.
The above expression, i.e., QTVUPO^*//*NM*LK+-++, is not a final expression. We need to reverse this expression to obtain the prefix expression.
Rules for the conversion of infix to prefix expression:
Pseudocode of infix to prefix conversion