Expression and calculation method of a forward expression These things are mentioned in algorithms. The following are records made based on memories. Generally, expressions in programming are read from left to right. Considering the operator priority, these expressions are in the middle order. (Compared with pre-order and post-order, the operator is placed first or later, so the Middle-order operator is placed in the middle) When processing such expressions, two stacks are generally required to store operators and operands. If the priority of operators is higher When an operator is used, the operand is taken from the stack and the operator is used to calculate the cost. The following is a simple example: (I will understand the commonly confused PHP expression calculation) $ I + $ X/$ Y-$ J 0) create two stacks. the stack of the operand stack and operator is empty. Read the expression from left to right. If an operand is read, it is placed in the operand stack, and the operator is placed in the stack of the operator. 1) read $ I + ------- + | $ I | -- + ------- + -- Operand stack operator Stack 2) read + + ------- ++ ------- + | $ I | + | -- + ------- + -- Operand stack operator Stack 3) read $ x + ------- + | $ X | + ------- ++ ------- + | $ I | + | -- + ------- + -- Operand stack operator Stack 3) read/Because/has a higher priority than + in the stack,/is directly stored in the stack. + ------- ++ ------- + | $ X |/| + ------- ++ ------- + | $ I | + | -- + ------- + -- Operand stack operator Stack 4) read $ y + ------- + | $ Y | + ------- ++ ------- + | $ X |/| + ------- ++ ------- + | $ I | + | -- + ------- + -- Operand stack operator Stack 5) then read-. Because the-operator ratio is/low, extract/and obtain the corresponding operand from the operand stack. Calculate, because/is a binary operation, take out $ y, $ X for the/operation, and then save the-symbol stack, and then Read to $ J + ------- + | $ J | + ------- ++ ------- + | $ X/$ Y |-| + ------- ++ ------- + | $ I | + | -- + ------- + -- Operand stack operator Stack 6) because there are still uncomputed symbols in the operator stack, each time an operator is taken out, corresponding numbers are retrieved from the operand stack. The row operation is performed until the operator stack is empty. When expression calculation is completed, the content in the operand stack is the result of the expression. Lazy I have to draw more pictures. 6.1) retrieve-, binary operations $ X/$ Y-$ J 6.2) retrieve +, binary operations $ I + $ X/$ Y-$ J __________________________________________________________________________________ Let's go back to PhP. Let's look at two examples. The previous example in Geel and quiz I sent was: P 1) $ I = 2; echo ($ I ++ $ I ); 1.1) obtain the following stack Diagram Based on the basic knowledge above + ------ ++ ------ + | $ I | ++ | -- + ------ + -- 1.2) then read the plus sign, and found that the Priority Ratio of the plus sign is lower than that of the plus sign. Therefore, the plus sign is first calculated and then pushed to the stack (single object operation) + ---------------------------- ++ ------ + | $ I ++ (Note: Stack value = 2, $ I = 3) | + | -- + ---------------------------- + -- + ------ + -- 1.3) read $ I into the operand stack ($ I = 3) + ---------- + | $ I (= 3) | + ---------- ++ ------ + | 2 | + | -- + ---------- + -- + ------ + -- 1.2) the operator Stack has been fully completed, so the operation is complete, and the value in the operand is the result. That is, 5. + ---------- + | 3 + 2 | -- + ---------- + -- + ------ + -- 2) $ A = 1; $ B = & $ A; $ B = $ A ++; 2.1) starting from ($ B = $ A ++), the following stack diagram is obtained first. + ------ ++ ------ + | $ B | = | -- + ------ + -- 2.2) then read the operand $. + ------ + | $ A | + ------ ++ ------ + | $ B | = | -- + ------ + -- 2.3) when we read the operator ++, because ++ has a higher priority than =, we first extract $ A for ++ operations. + ---------------------- + | $ A ++ (stack value: 1, $ A = 2) | + ---------------------- ++ ------ + | $ B | = | -- + ---------------------- + -- + ------ + -- 2.4) read = for the final operation + ----------- ++ ------ + | $ B = 1 | = | -- + ----------- + -- + ------ + -- 2.5) because $ B and $ A are interrelated, the values of $ A and $ B are the values of the last value ($ B = 1), so $ A = $ B = 1 |