且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用boost :: spirit解析数学表达式并将其绑定到函数

更新时间:2023-10-08 17:31:16

您将学习Spirit.太好了!

You're going to learn Spirit. Great!

不过,似乎您在这里所咬的东西超出了您的咀嚼能力.

It seems you're biting off more than you can chew here, though.

首先,您的语法实际上尚未解析表达式.当然,它不会导致您可以绑定的功能.

Firstly, your grammar doesn't actually parse an expression yet. And it certainly doesn't result in a function that you can then bind.

  1. 实际上,您正在使用不产生任何结果的语法来解析输入.它只会创建一个副作用(将带有直接简单操作数的简单二进制表达式的结果打印到控制台).这种/relikes/解释性语言,尽管

  1. In fact you're parsing the input using a grammar that is not producing any result. It only creates a side-effect (which is to print the result of the simple binary expression with immediate simple operands to the console). This /resembles/ interpreted languages, although it would soon break up when

  • 您尝试解析类似2*8 + 9
  • 的表达式
  • 您将输入回溯的信息(糟糕,副作用已经触发)
  • you try to parse an expression like 2*8 + 9
  • you would have input that backtracks (oops, the side effect already fired)

接下来,您将绑定func(顺便说一句,这是多余的;您没有绑定任何参数,因此您只能在此处说function_Type multiplication(func);)并调用它.虽然很酷,但实际上与解析输入无关.

Next up you're binding func (which is redundant by the way; you're not binding any arguments so you could just say function_Type multiplication(func); here), and calling it. While cool, this has literally nothing to do with the parsing input.

最后,您的问题是关于第三的问题,上面的任何地方都没有涉及.这个问题是关于符号表和标识符查找的.

Finally, your question is about a third thing, that wasn't even touched upon anywhere in the above. This question is about symbol tables and identifier lookup.

  • 这意味着您应该解析源以获取实际的标识符(例如xt)
  • 您需要将它们存储到符号表中,以便可以将它们映射到一个值(也许是作用域/生命周期)
  • 问题中存在一个逻辑漏洞,您无需定义形式参数列表"的来源(您在此处的文本中提到了它: function = x*t; 解析器不处理它,您也没有对任何此类元数据进行硬编码);因此我们甚至无法开始将xt映射到正式参数列表(因为它不存在).

  • This would imply you should parse the source for actual identifiers (x or t, e.g.)
  • you'd need to store these into a symbol table so they could be mapped to a value (and perhaps a scope/lifetime)
  • There is a gaping logic hole in the question where you don't define the source of the "formal parameter list" (you mention it in the text here: function = x*t; but the parser doesn't deal with it, neither did you hardcode any such metadata); so there is no way we could even start to map the x and t things to the formal argument list (because it doesn't exist).

让我们暂时假设事实上参数是位置参数(按原样,并且在使用位置参数调用绑定函数时,您似乎想要这么做.(因此,我们不必担心名称,因为没人会看到名称.)

  • 调用方应在上下文中传递给函数,以便可以在评估期间通过标识符名称查找值.

  • the caller should pass in a context to the functions, so that values can be looked up by identifier name during evaluation.

    因此,尽管我可以尝试让您坐下来,与您讨论所有首先需要创建的细节,然后您甚至可以梦想以自己想要的奇妙方式将它们粘合在一起,但不要这样.

    So, while I could try to sit you down and talk you through all the nuts and bolts that need to be created first before you can even dream of glueing it together in fantastic ways like you are asking for, let's not.

    这将花费我太多时间,并且您可能不知所措.

    It would take me too much time and you would likely be overwhelmed.

    我只能建议看一些简单的资源.从教程开始

    I can only suggest to look at simpler resources. Start with the tutorials