Evaluate formula in Go

前端 未结 8 1017
梦如初夏
梦如初夏 2021-01-02 12:46

Using Go (golang) I\'d like to take a string with a formula and evaluate it with pre-defined values. Here\'s a way to do it with python\'s parser module:

<
8条回答
  •  误落风尘
    2021-01-02 13:28

    For expression or program evaluation, you can build a lexer and parser using lex and yacc, and specify exactly the syntax and semantics of your mini-language. A calculator has always been a standard yacc example, and the go versions of lex and yacc are no different.

    Here's a pointer to the calc example: https://github.com/golang-samples/yacc/tree/master/simple

提交回复
热议问题