polish to BNF via recrusive

  1. Implement a recursive function evalPolish(expression) that shall evaluate (calculate) an expression written in the prefix (“Polish”) notation. The expression shall be defined recursively in the Backus-Naur form (BNF):

    1. expression ::= integer_or_floating_point_number # A number is an expression; its value is the number itself
    2. expression ::= (operator, expression, expression) # A 3-element tuple of an operator and another two expressions is also an expression; its value is the operator applied to the values of the expressions
    3. operator ::= “+” | “-” | “*” | “/” # The list of supported operators; feel free to extend it

    Here is a sample expression and its evaluation:

    >>> good = ("+", ("-", 3, 1), ("*", ("/", 3, 4), 7)) # Same as

Leave a Reply

Your email address will not be published. Required fields are marked *