我需要一个快速的运行时表达式解析器 [英] I need a fast runtime expression parser

查看:30
本文介绍了我需要一个快速的运行时表达式解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到一个快速、轻量级的表达式解析器.

I need to locate a fast, lightweight expression parser.

理想情况下,我想向它传递一个名称/值对列表(例如变量)和一个包含要计算的表达式的字符串.我所需要的只是一个真/假值.

Ideally I want to pass it a list of name/value pairs (e.g. variables) and a string containing the expression to evaluate. All I need back from it is a true/false value.

表达式的类型应该遵循:

The types of expressions should be along the lines of:

varA == "xyz" and varB==123

基本上,只是一个简单的逻辑引擎,其表达式在运行时提供.

Basically, just a simple logic engine whose expression is provided at runtime.

更新
至少它需要支持 ==, !=, >, >=, <, <=

UPDATE
At minimum it needs to support ==, !=, >, >=, <, <=

关于速度,我预计每个请求大约执行 5 个表达式.我们会看到每秒 100 个/请求附近的某个地方.我们当前的页面往往在 50 毫秒内执行.通常任何表达式中只涉及 2 或 3 个变量.但是,在执行之前,我需要将大约 30 个加载到解析器中.

Regarding speed, I expect roughly 5 expressions to be executed per request. We'll see somewhere in the vicinity of 100/requests a second. Our current pages tend to execute in under 50ms. Usually there will only be 2 or 3 variables involved in any expression. However, I'll need to load approximately 30 into the parser prior to execution.

更新 2012/11/5
关于性能的更新.我们在将近 2 年前实施了 nCalc.从那时起,我们扩展了它的用途,以便我们平均覆盖 300 多个变量的 40 多个表达式在回传中.现在每秒发生数千次回传,性能下降绝对为零.

UPDATE 2012/11/5
Update about performance. We implemented nCalc nearly 2 years ago. Since then we've expanded it's use such that we average 40+ expressions covering 300+ variables on post backs. There are now thousands of post backs occurring per second with absolutely zero performance degradation.

我们还对其进行了扩展,以包含一些附加功能,同样没有性能损失.简而言之,nCalc 满足了我们的所有需求并超出了我们的预期.

We've also extended it to include a handful of additional functions, again with no performance loss. In short, nCalc met all of our needs and exceeded our expectations.

推荐答案

你见过 https://ncalc.codeplex.com/https://github.com/sheetsync/NCalc ?

它可扩展、快速(例如,有自己的缓存)使您能够在运行时通过处理 EvaluateFunction/EvaluateParameter 事件提供自定义函数和变量.它可以解析的示例表达式:

It's extensible, fast (e.g. has its own cache) enables you to provide custom functions and varaibles at run time by handling EvaluateFunction/EvaluateParameter events. Example expressions it can parse:

Expression e = new Expression("Round(Pow(Pi, 2) + Pow([Pi2], 2) + X, 2)");

  e.Parameters["Pi2"] = new Expression("Pi * Pi");
  e.Parameters["X"] = 10;

  e.EvaluateParameter += delegate(string name, ParameterArgs args)
    {
      if (name == "Pi")
      args.Result = 3.14;
    };

  Debug.Assert(117.07 == e.Evaluate());

它还处理 unicode &许多数据类型本机.如果您想更改语法,它会附带一个鹿角文件.还有一个 fork 支持 MEF 加载新功能.

It also handles unicode & many data type natively. It comes with an antler file if you want to change the grammer. There is also a fork which supports MEF to load new functions.

它还支持逻辑运算符、日期/时间字符串和 if 语句.

It also supports logical operators, date/time's strings and if statements.

这篇关于我需要一个快速的运行时表达式解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆