简体   繁体   English

解析器:解析模板文件中的公式

[英]parser: parsing formulas in template files

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). 例如:TOTAL_ITEMS,PRICE,CONTRACTS,ETC(我们大约有15个)。 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. total_cost = CONTRACTS*PRICE*TOTAL_ITEMS和一些元日期,就像屏幕一样显示它。 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 萨沙

看起来使用yaccbison生成一个简单的解析器并将其集成到您的代码中应该不会太难。

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): 我可能只是将字符串标记化,按名称键入变量的映射(假设所有变量都是整数):

map<const char*,int*> vars;
vars["CONTRACTS"] = &contracts;
...

Then use a simple postfix calculator function to do the actual math. 然后使用简单的后缀计算器函数来进行实际的数学运算。

Edit: 编辑:

Looking at MathEval, it seems to do exactly what you want; 看看MathEval,它似乎完全符合你的要求; 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? 您是否有MathEval不满足的要求?

I don't know about matheval, but boost::spirit can do that for you pretty efficiently : see there . 我不知道matheval,但是boost :: spirit可以非常有效地为你做到:看到那里

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. 如果你到模板元编程,你可能想看看进入升压::原 ,但它需要一些时间来使用它上手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM