As you might expect, there are algorithmic ways to perform the conversion that allow any expression of any complexity to be correctly transformed. The first technique that we will consider uses the notion of a fully parenthesized expression that was discussed earlier. On closer observation, however, you can see that each parenthesis pair also denotes the beginning and the end of an operand pair with the corresponding operator in the middle. If the addition operator were also moved to its corresponding right parenthesis position and the matching left parenthesis were removed, the complete postfix expression would result see Figure 6.
|Published (Last):||14 May 2009|
|PDF File Size:||14.7 Mb|
|ePub File Size:||17.55 Mb|
|Price:||Free* [*Free Regsitration Required]|
As you might expect, there are algorithmic ways to perform the conversion that allow any expression of any complexity to be correctly transformed. The first technique that we will consider uses the notion of a fully parenthesized expression that was discussed earlier. On closer observation, however, you can see that each parenthesis pair also denotes the beginning and the end of an operand pair with the corresponding operator in the middle.
If the addition operator were also moved to its corresponding right parenthesis position and the matching left parenthesis were removed, the complete postfix expression would result see Figure 6. The position of the parenthesis pair is actually a clue to the final position of the enclosed operator. Then move the enclosed operator to the position of either the left or the right parenthesis depending on whether you want prefix or postfix notation.
Figure 8 shows the conversion to postfix and prefix notations. To do this we will look closer at the conversion process. We have already noted that the operands A, B, and C stay in their relative positions. It is only the operators that change position.
The order of the operators in the original expression is reversed in the resulting postfix expression. As we process the expression, the operators have to be saved somewhere since their corresponding right operands are not seen yet. Also, the order of these saved operators may need to be reversed due to their precedence.
This is the case with the addition and the multiplication in this example. Since the addition operator comes before the multiplication operator and has lower precedence, it needs to appear after the multiplication operator is used. Because of this reversal of order, it makes sense to consider using a stack to keep the operators until they are needed.
We can now start to see how the conversion algorithm will work. When we see a left parenthesis, we will save it to denote that another operator of high precedence will be coming.
That operator will need to wait until the corresponding right parenthesis appears to denote its position recall the fully parenthesized technique. When that right parenthesis does appear, the operator can be popped from the stack.
As we scan the infix expression from left to right, we will use a stack to keep the operators. This will provide the reversal that we noted in the first example. The top of the stack will always be the most recently saved operator.
Whenever we read a new operator, we will need to consider how that operator compares in precedence with the operators, if any, already on the stack. Assume the infix expression is a string of tokens delimited by spaces. The operand tokens are the single-character identifiers A, B, C, and so on.
The following steps will produce a string of tokens in postfix order. Create an empty stack called opstack for keeping operators. Create an empty vector for output. Scan the current token of the input vector from left to right using a loop. If the token is an operand, append it to the end of the output list vector.
If the token is a left parenthesis, push it on the opstack. If the token is a right parenthesis, pop the opstack until the corresponding left parenthesis is removed.
Append each operator to the end of the output vector. However, first remove any operators already on the opstack that have higher or equal precedence and append them to the output vector. When the input expression has been completely processed, check the opstack. Any operators still on the stack can be removed and appended to the end of the output vector. This hash map will map each operator char to an integer that can be compared against the precedence levels of other operators we have arbitrarily used the integers 3, 2, and 1.
The left parenthesis will receive the lowest value possible. This way any operator that is compared against it will have higher precedence and will be placed on top of it.
Line 18 defines the operands to be any upper-case character or digit. The complete conversion function is shown in ActiveCode 1.
Infix Prefix Postfix Conversion (Stack)
We take the first part of the string. We then convert it into prefix. We take the third part of the string. Step 5: In the final step, We combine all the parts. Here we get the final output of an infix string converted into a prefix string. Infix to Postfix Conversion The infix to postfix conversion follows an algorithm given as follows. Scan the given infix string from left to right.
Infix, Postfix and Prefix notations are three different but equivalent ways of writing expressions. It is easiest to demonstrate the differences by looking at examples of operators that take two operands. This is the usual way we write expressions. 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.