How to parse a mathematical expression with boost::spirit and bind it to a function

こ雲淡風輕ζ 提交于 2019-12-03 17:28:24
sehe

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. 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

    • you try to parse an expression like 2*8 + 9
    • you would have input that backtracks (oops, the side effect already fired)
  2. 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.

  3. 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.

    • 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).

      Let's assume for the moment that in fact arguments are positional (as they are, and you seem to want this as you call the bound function with positional arguments anyways. (So we don't have to worry about a name because no one will ever see a name.)

    • 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.

Suggestions

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

Ask freely if you have any questions along the way and you're at risk of getting stuck. But at least then we have a question that is answerable and answers that genuinely help you.

For now, look at some other answers of mine where I actually implemented grammars like this (somewhat ordered by increasing complexity):

The benefits of each approach are described in these answers. These parsers do not have a symbol table nor a evaluation context.

More examples:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!