解析器:解析模板文件中的公式
我将首先描述问题,然后描述我目前在库方面所看到的内容。
在我的应用程序中,我们有一组始终可用的变量。 例如:TOTAL_ITEMS、PRICE、CONTRACTS 等(我们大约有 15 个)。 应用程序的客户端希望使用这些变量执行和显示某些计算。 到目前为止,我一直在不断地将这些计算添加到应用程序中。 这是令人痛苦的事情,我想通过创建模板使其更加通用,用户可以在其中指定应用程序将解析和计算的一组公式。
这是一种情况:
total_cost = CONTRACTS*PRICE*TOTAL_ITEMS
因此,想要为用户在模板文件中定义类似的操作:
total_cost = CONTRACTS*PRICE*TOTAL_ITEMS 和一些元数据,例如显示它的屏幕。 因此,他们将通过屏幕指定公式。 该文件将包含许多这种性质的公式。
现在,我正在查看两个库: Spirit< /a> 和 matheval
有人会建议什么对这项任务更好,以及参考文献,示例,链接?
如果问题不清楚,请告诉我,我会尽力进一步澄清。
谢谢,
萨沙
I will first describe the problem and then what I currently look at, in terms of libraries.
In my application, we have a set of variables that are always available. For example: TOTAL_ITEMS, PRICE, CONTRACTS, ETC (we have around 15 of them). A clients of the application would like to have certain calculations performed and displayed, using those variables. Up until now, I have been constantly adding those calculations to the app. It's pain in the butt, and I would like to make it more generic by way of creating a template, where the user can specify a set of formulas that the application will parse and calculate.
Here is one case:
total_cost = CONTRACTS*PRICE*TOTAL_ITEMS
So, want to do something like that for the user to define in the template file:
total_cost = CONTRACTS*PRICE*TOTAL_ITEMS
and some meta-date, like screen to display it on. Hence they will be specifying the formula with a screen. And the file will contain many formulas of this nature.
Right now, I am looking at two libraies: Spirit and matheval
Would anyone make recommendations what's better for this task, as well as references, examples, links?
Please let me know if the question is unclear, and I will try to further clarify it .
Thanks,
Sasha
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有固定数量的变量,那么调用解析器可能有点矫枉过正。 尽管 Spirit 很酷,而且我一直想在项目中使用它。
我可能只是标记字符串,制作按名称键入的变量的映射(假设所有变量都是整数):
然后使用简单的后缀计算器函数进行实际的数学运算。
编辑:
看看MathEval,它似乎完全符合你的要求; 设置变量并使用这些变量评估数学函数。 我不确定您为什么要在语法解析器级别创建解决方案。 您是否有任何 MathEval 未满足的要求?
If you have a fixed number of variables it may be a bit overkill to invoke a parser. Though Spirit is cool and I've been wanting to use it in a project.
I would probably just tokenize the string, make a map of your variables keyed by name (assuming all your variables are ints):
Then use a simple postfix calculator function to do the actual math.
Edit:
Looking at MathEval, it seems to do exactly what you want; set variables and evaluate mathematical functions using those variables. I'm not sure why you would want to create a solution at the level of a syntax parser. Do you have any requirements that MathEval does not fulfill?
看起来使用 yacc 和 bison 并将其集成到您的代码中。
Looks like it shouldn't be too hard to generate a simple parser using yacc and bison and integrate it into your code.
我不了解数学,但 boost::spirit 可以非常有效地为您做到这一点:请参阅 那里。
如果您喜欢模板元编程,您可能需要查看 Boost::Proto,但是将需要一些时间才能开始使用它。
I don't know about matheval, but boost::spirit can do that for you pretty efficiently : see there.
If you're into template metaprogramming, you may want to have a look into Boost::Proto, but it will take some time to get started using it.