Shunting yard: no argument to operator

I implement a shunt yard algorithm . I'm having trouble finding when there are no arguments to the operators. Wikipedia entries are very bad in this thread, and their code will also fail for the example below.

For example, it is 3 - (5 + )incorrect because there is +no argument.

Before the algorithm reaches ), the operator stack contains - ( +, and the operand stack contains 3 5. Then it looks like this:

  • it pops the +operator stack
  • discovers what +is a binary operator
  • pushes two operands, applies the operator and pushes the result ( 8) onto the operand stack
  • then he pulls the appropriate one (from the stack and continues

So how can I find that an +argument is missing? super reach if you also update wikipedia :-)

+3
source share
2 answers

For binary operators only expressions, the postfix expression has an invariant that in any expression prefix, operand numbers> number of operators and, in the end, this difference is exactly the same.

, RPN , - . , .

, , , , .

(: , , )

+3

. , - .

, . , , .

, / .

postfix .

, , .

, , .

, / .

ifs .:)

0

Source: https://habr.com/ru/post/1755555/


All Articles