Writing a simple equation parser

前端 未结 10 1184
-上瘾入骨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: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.

提交回复
热议问题