algorithmshunting-yardpolish-notation

How to count number of arguments of a method while converting infix expression to reverse polish notation


I have an expression like below. MIN(MAX(AVG(AVG(4,2),2,3),SUM(1,2))) I have implemented shunting yard algorithm to convert infix to reversed polish notation. I add the function MAX , MIN and AVG with two arguments. But suppose if I want to implement variable arguments then I must know how many arguments each function had in infix expression. Can someone tell me how could I modify the shunting yard algorithm to include no. of arguments of each function while converting infix to rpn ?


Solution

  • This is how I finally did. When the token is an open parenthesis , I add it to the output queue. Then, when converting or executing the RPN output, and I encounter a function call token, I pop items from the stack until I encounter an open parenthesis , discard it, and consider everything in between to be an argument to the function.

    Probably not a neat solution but worked like a charm :)