
Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.If the token is a left parenthesis, then push it onto the stack.O 1 is associative or left-associative and its precedence is less than (lower precedence) or equal to that of o 2, or o 1 is right-associative and its precedence is less than (lower precedence) that of o 2, pop o 2 off the stack, onto the output queue while there is an operator, o 2, at the top of the stack, and either.If the token is an operator, o 1, then:.If w is true, pop arg count into a, increment a and push back into arg count. If no left parentheses are encountered, either the separator was misplaced or parentheses were mismatched. Until the topmost element of the stack is a left parenthesis, pop the element onto the output queue.If the token is a function argument separator (e.g., a comma):.If the were values stack has a value on it, pop it and push true. If the token is a function token, then push it onto the stack.If the token is a number, then add it to the output queue.In my case, I did it with a small class that took the function and an argument count, with one of these created during tokenisation for each function encountered. It also requires that you can attach the number of arguments to an instance of a function. This requires two more stacks, a ‘ were values‘ stack, and an ‘ arg count‘ stack. To do this, I extended the standard algorithm. In this case, I wanted to handle functions with a variable number of arguments, so ‘ max(1,2)‘ would work, and so would ‘ max(1,2,3,4,5)‘.

The standard shunting yard algorithm can handle functions, but with the restriction that the number of arguments to them is known (really, this is a limitation of the RPN algorithm, but it’s at this point in the process that we need to deal with the problem). This did the job nicely, however the time came when it had to be extended, and able to accept functions, such as ‘ max(1,2,3)‘. Evaluate the RPN form of the equation using an RPN parser, which is really easy to write.Convert the input tokens to RPN using the Shunting Yard algorithm.

