1

What would be a good algorithm to convert infix to postfix of an expression which uses user-defined functions: For example:

def get_random_integer(a, b, c):
   import random

   return random.choice([a,b,c])

# Expression to be parsed
expression_string = '1 + 2 - (2 * 4) + get_random_integer(2,1,3) + 7 + 9'
   

What's the best way to convert this to a postfix expression The user-defined function could have any name and number of parameters

I followed https://github.com/KaylenTPillay/SSBOMAS-Calculator for the normal expression conversion.

Any help is appreciated.

visuman
  • 140
  • 2
  • 12
  • 1
    I don't think you really want to convert the expression to postfix. You probably want to evaluate it, or perhaps you want to parse it into a parse tree, or perhaps you want to generate three-address code for some virtual machine. Or some variant. In all cases, the postfix expression is an unnecessary intermediate result which is present because people like the one whose code you cited also copied their code from someone who thought the unnecessary postfix expression was somehow necessary. Concentrate on the result you actually want. – rici Mar 02 '22 at 19:51
  • Possibly related - https://stackoverflow.com/q/11708195/427192 – Dan Pichelman Jun 30 '22 at 13:17

0 Answers0