Writing a simple equation parser

前端 未结 10 1153
-上瘾入骨i
-上瘾入骨i 2020-12-01 03:56

What sorts of algorithms would be used to do this (as in, this is a string, and I want to find the answer):

((5 + (3 + (7 * 2))) - (8 * 9)) / 72
相关标签:
10条回答
  • 2020-12-01 04:35

    If the expressions are known to be fully-parenthesized (that is, all possible parentheses are there), then this can easily be done using recursive-descent parsing. Essentially, each expression is either of the form

     number
    

    or of the form

     (expression operator expression)
    

    These two cases can be distinguished by their first token, and so a simple recursive descent suffices. I've actually seen this exact problem given out as a way of testing recursive thinking in introductory programming classes.

    If you don't necessarily have this guarantee, then some form of precedence parsing might be a good idea. Many of the other answers to this question discuss various flavors of algorithms for doing this.

    0 讨论(0)
  • 2020-12-01 04:41

    You can use Shunting yard algorithm or Reverse Polish Notation, both of them are using stacks to handle this, wiki said it better than me.

    From wiki,

    While there are tokens to be read:
    
        Read a token.
        If the token is a number, then add it to the output queue.
        If the token is a function token, then push it onto the stack.
        If the token is a function argument separator (e.g., a comma):
    
            Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue. If no left parentheses are encountered, either the separator was misplaced or parentheses were mismatched.
    
        If the token is an operator, o1, then:
    
            while there is an operator token, o2, at the top of the stack, and
    
                    either o1 is left-associative and its precedence is less than or equal to that of o2,
                    or o1 is right-associative and its precedence is less than that of o2,
    
                pop o2 off the stack, onto the output queue;
    
            push o1 onto the stack.
    
        If the token is a left parenthesis, then push it onto the stack.
        If the token is a right parenthesis:
    
            Until the token at the top of the stack is a left parenthesis, pop operators off the stack onto the output queue.
            Pop the left parenthesis from the stack, but not onto the output queue.
            If the token at the top of the stack is a function token, pop it onto the output queue.
            If the stack runs out without finding a left parenthesis, then there are mismatched parentheses.
    
    When there are no more tokens to read:
    
        While there are still operator tokens in the stack:
    
            If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses.
            Pop the operator onto the output queue.
    
    Exit.
    
    0 讨论(0)
  • 2020-12-01 04:41

    The easiest way to solve this is to implement the Shunting Yard algorithm to convert the expression from infix notation to postfix notation.

    It's Easy-with-a-capital-E to evaluate a postfix expression.

    The Shunting Yard algorithm can be implemented in under 30 lines of code. You'll also need to tokenize the input (convert the character string into a sequence of operands, operators, and punctuators), but writing a simple state machine to do that is straightforward.

    0 讨论(0)
  • 2020-12-01 04:43

    You could use either a state machine parser (yacc LALR, etc.), or a recursive descent parser.

    The parser could emit RPN tokens to evaluate or compile later. Or, in an immediate interpreter implementation, a recursive descent parser could calculate subexpressions on the fly as it returns from the leaf tokens, and end up with the result.

    0 讨论(0)
提交回复
热议问题