查询过滤器表达式树的解析器 [英] Parser for query filter expression tree

查看:53
本文介绍了查询过滤器表达式树的解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以在查询过滤器上运行的解析器.但是,我不太确定该术语,因此证明它很辛苦.我希望有人能帮助我.我已经读过递归下降解析器",但我想知道它们是否用于成熟的语言解析器,而不是我想要的逻辑表达式评估.

理想情况下,我正在寻找.NET代码(C#),但也希望在T-SQL中使用类似的解析器.

我想要的是用于解析的内容,例如:

(((a = b)|(e = 1))&(c< = d)

理想情况下,运算符可以是可定义的(例如'<'vs'lt','='vs'=='vs'eq'等),我们可以指定函数类型标签(例如(left(x,1)='e')).解析器加载此代码,服从顺序优先级(理想情况下处理缺少的方括号),然后使用表达式回调到我的代码以求出布尔结果-例如'a = b'?).我不希望解析器理解表达式中的自定义函数(尽管一些基本的函数会很有用,例如字符串拆分).将表达式(分为左侧和右侧部分)会很好.

解析器最好问出必须解决最终结果的最少数量的问题-例如如果AND的一侧为假,则没有意义评估另一侧,并且首先评估最简单的一侧(即,在上面的表达式中,应假定'c <= d'更快,因此应首先评估./p>

我可以想象这是很多工作要做,但是相当普遍.谁能给我任何指示?如果没有像上面这样灵活的解析器,那么有没有可以用作入门的基本解析器?

非常感谢

解决方案

您可以查看 Irony .有了它,您就可以使用与bnf差不多的语法在C#代码中定义语法.他们甚至在自己的网站(表达式评估程序)上都有一个简单的示例,该示例似乎与您想要实现的目标非常接近.

关于反讽的话题今年的Lang.Net研讨会.

希望这会有所帮助!

I am looking for a parser that can operate on a query filter. However, I'm not quite sure of the terminology so it's proving hard work. I hope that someone can help me. I've read about 'Recursive descent parsers' but I wonder if these are for full-blown language parsers rather than the logical expression evaluation that I'm looking for.

Ideally, I am looking for .NET code (C#) but also a similar parser that works in T-SQL.

What I want is for something to parse e.g.:

((a=b)|(e=1))&(c<=d)

Ideally, the operators can be definable (e.g. '<' vs 'lt', '=' vs '==' vs 'eq', etc) and we can specify function-type labels (e.g. (left(x,1)='e')). The parser loads this, obeys order precedence (and ideally handles the lack of any brackets) and then calls-back to my code with expressions to evaluate to a boolean result - e.g. 'a=b'?). I wouldn't expect the parser to understand the custom functions in the expression (though some basic ones would be useful, like string splitting). Splitting the expression (into left- and right-hand parts) would be nice.

It is preferable that the parser asks the minimum number of questions to have to work out the final result - e.g. if one side of an AND is false, there is no point evaluating the other side, and to evaluate the easiest side first (i.e. in the above expression, 'c<=d' should be assumed to be quicker and thus evaluated first.

I can imagine that this is a lot of work to do, however, fairly common. Can anyone give me any pointers? If there aren't parsers that are as flexible as above, are there any basic parsers that I can use as a start?

Many Thanks

Lee

解决方案

You could check out Irony. With it you define your grammar in C# code using a syntax which is not to far from bnf. They even have a simple example on their site (expression evaluator) which seems to be quite close to what you want to achieve.

Edit: There's been a talk about Irony at this year's Lang.Net symposium.

Hope this helps!

这篇关于查询过滤器表达式树的解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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