Reverse polish expression

Source: Internet
Author: User
An Inverse polish expression is generally composed of operand and operators. For example, in an arithmetic expression, an operator is usually placed in the middle of two operands. This is called an infix expression ), for example, A + B. The Polish mathematician Jan Lukasiewicz proposed another mathematical representation, which has two forms: the operator is written before the operand, called the polish expression (Polish expression) or prefix expression (prefix expression ), for example, + AB; the operator is written after the operand, which is called a reverse polish expression (reverse polish expression) or a suffix expression (suffix expression), such as AB +; where, reverse Polish expressions are widely used in compilation technology. Algorithm: 1. convert an infix expression to a suffix expression algorithm: 1. Scan an infix expression from left to right. 2. If an operand is read, the type of the operand is determined, and the operand is saved to the operand stack. 3. If an operator (1) is read) this operator is left parenthesis "(", and is directly stored in the operator stack. (2) If this operator is ")", the operator in the operator stack is output to the operand stack until the left parenthesis is encountered. (3) This operator is not a bracket OPERATOR: (a) If the operator at the top of the operator stack is a bracket, it is directly stored in the operator stack. (B) If the operator has a higher or equal priority than the operator at the top of the operator stack, it is directly stored in the operator stack. (C) If the operator has a lower priority than the operator at the top of the operator stack, the operator at the top of the stack is output to the operand stack, and the current operator is pushed to the operator stack. 4. When an operator still exists in the operator stack after the expression is read, the operator is retrieved in sequence until the operator stack is empty. Ii. Inverse polish expression evaluation algorithm: 1. Items of the cyclic scan syntax unit. 2. If the scanned item is an operand, press it into the operand stack and scan the next project. 3. If the scanned item is a binary operator, this operation is performed on the top two operands of the stack. 4. If the scanned item is a unary operator, this operation is performed on the top operand of the stack. 5. Re-Press the calculation result into the stack. 6. Repeat steps 2-5, and the result value is in the stack.

The C language explains the compound replication operator, which is concise and produces machine code with high efficiency. Why is efficiency high,

This requires understanding of the [inverse Polish style] mentioned in [Compilation Principle ]. A reverse polish expression is also called a suffix expression ]. By extension

There are [prefix expressions] and [infix expressions .] (Sometimes it is also a forward, middle, and post-order ). The infix expression is commonly used.

The so-called standard expression, such as "A + B", is named infix notation in mathematics because:

The operator number is in the middle of two operation objects.

Advantages:Only two simple operations can be used to perform the operation of any common expressions (including only the expressions of +-*/and () on the inbound and outbound stacks.

Its basic calculation method: If the current character is a variable or number, the stack is pressed. If it is an operator, the two elements at the top of the stack are displayed for calculation. The result is re-written to the stack. Finally, after the expression is scanned, stack is the result.

Why is it highly efficient to generate machine code against the Polish style:This is because it is very easy to process computers. This is the reason. Example:

3 32 + 5 3 *-
12 34 2-* 8/
At first glance, the two statements above are very strange, right? They are the inverse polish expression.
Now, prepare a very narrow cylinder with a bottom, like a slim Cup, which is exactly the same as a coin. Then, write "3", "32", "+" 5 "," 3 "," * ", and"-"on the paper. Remember, each piece of paper can either write only one number or only one operator number, and sort them in the above Order. Well, now let me know carefully, pick up the small disc one by one in sequence, and execute the following rules repeatedly:
1. If you are holding a number, simply put it into the cylinder;
2. If you are holding an operator number, do not put it in. First, extract two numbers from the cylinder (of course, the first is the top, the cylinder is very fine), and then place these two numbers for the operation specified by the operator number, write the result on a new piece of paper and put it in the tube. For example, if you are holding "+", You need to extract "32" and "3" in sequence and add them to "35 ", write "35" on a new piece of paper (now "34" and "12" can be thrown away) and put the new piece of paper into the cylinder.
When there is only one number in the cylinder, you can stop. I guess this number is 20. Yes, this is the value of this expression!
What we just operated on is actually a "stack". The stack is a data structure with a nature-LIFO-last input first output. You already have a deep understanding, just like a pile of dishes, you can only take them from the top, and put them on the top. The action to put in is called "inbound stack", and the action to take out is called "pop-up ". In the future, you can think of a stack as a pile of plates, or the small cylinder and small pieces of paper mentioned above. The stack is so simple!
Although the reverse polish expression looks complicated, it is actually very useful in computers. The computer does not know how to multiply, multiply, add, and subtract. It must be enclosed in parentheses and then parentheses. It must convert the formula you entered into a reverse polish expression, so that it can continuously execute the above two fixed rules, until the result is calculated ., During processing, the compiler reads the inverse polish expression in the order from left to right. When an operation object is pushed into the stack, the compiler extracts the two objects from the stack for calculation, this process is in line with the principles of computer computing. Therefore, the anti-Polish formula is very suitable for computer processing.

The following problem is:

An algorithm that converts an infix expression to an inverse polish expression:The following is an example:

A) returns an infix expression 1*(2 + 3)


B)The system first defines two advanced and later stacks: operator number stack (inbound stack in for short) and suffix expression output symbol stack (outbound stack out for short)


C)The System reads the infix expression from left to right.


D)Read the number and press it into the stack (out)


E)Read the first operator and press it directly into the stack (in)


F)Read "(" directly into the stack (in ).After reading the data several times according to the preceding rules, if the data in the two stacks is: in [*, (]; out [], the second operator that begins to read "+ ", and compare it with the stack top operator (") in the inbound stack (in,

G)Operators higher than the stack top operator level are directly pushed to the stack. Operators lower than or equal to the top stack level must be pushed to the stack (in) for unstack (Out stack) and pushed to the stack (out) on a per-time basis) medium. For example, the current operation order of the inbound stack is (, *,/. If the operator to be read by the system is + and the level ratio is lower /, * is pushed out of the stack in sequence, and the/and * symbols are released in the stack. Finally, the results of in (; out/, * are obtained.

F)Finally, when reading ")", find the nearest sign in the stack (", press all the symbols before it into the stack in the first-in-out order, and decompress it., "(" And ")" offset. The data in the two stacks is: in 1, 2, 3, +; out *


G)After the system reads the infix expression, all the symbols in the in stack are extracted in the order of forward and forward, and pushed to the out stack in sequence.The final output stack result should be 1, 2, 3, + ,*


H) decompress the out stack out in the first-in-first-out order to obtain the standard extension EXPRESSIONS 1, 2, 3, + ,*


Data of two stacks:










1, 2

*, (, +

1, 2

*, (, +

1, 2, 3


1, 2, 3, +


1, 2, 3, + ,*


During the process of converting the infix expression to the inverse polish expression, pay special attention to the processing of the infix-to-style braces.

1. Note that if the operator is "(", No matter why the top level of the stack in the stack (only depends on the top of the stack) is directly imported into the stack, the "(" Level

It is only used to compare the priorities of the operators that follow the stack. When "(" is entered into the stack, the priority is ignored.

Note: There are several priority levels used against Poland: level from high to low: 1, * \ 2, +-3, (4 ,)

2. When ")" is encountered, find the "(" that finally enters, and press all the symbols in front of it into the stack. It cannot be determined by the operator level alone.


An algorithm that reverts an inverse polish expression back to the infix expression:

This is quite simple, that is, a mechanical operation for inbound and outbound stack,

1) set a stack and operate the stack from left to right in reverse order. For example, 1, 2, 3, + ,*

2) directly press the stack on a number. For example, if the previous example is against Poland, it first performs three operations into the stack. The stack pattern is: 1, 2, 3 (top of the stack );

3) When an operator is encountered, two numbers in the stack are taken out of the stack. For example, after reading the "+" code, the system outputs 2 or 3 stacks for calculation. Note: When the stack is output, the elements of the first stack are the right operator, while the last stack is the left operator. The preceding example is 2 + 3, not 3 + 2;

4) press the result of the operation into the stack as a new operator. For example, the calculation result (2 + 3) goes into the stack, and the stack pattern is: 1, (2 + 3 );

5) after repeated operations from 1 to 4, the obtained medium-order expression is: 1*(2 + 3 );

Is the reverse Polish form generated by the central order table unique? :

It is unique and one-to-one correspondence with a fixed-form medium-order expression. However, pay attention to this concept,

For example, the values of A + (B-c) * d and (B-c) * D + A and A + D * (B-c) are exactly the same. However, their central order forms are different,

The inverse Polish formula generated must be different.

A + (B-c) * D: ABC-D * +

(B-c) * D + A: BC-D * A +

A + D * (B-c): ADBC-* +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.