Query filter expression tree parser

c# expression-trees parsing tsql


A parser that can work with a query filter is what I'm searching for. It's challenging for me since I'm not completely clear of the terminology. I'm hoping someone can assist me. I've heard about "Recursive descent parsers," but I'm not sure whether they are for logical expression evaluation or full-fledged language parsers.

I'd like to find.NET code (C#), but I'm also open to suggestions for T-SQL parsers that are comparable.

I want something that can parse something like:


The operators should ideally be defined (e.g., "" vs. "lt," "=" vs. "==," "eq," etc.), and function-type labels should be specified (e.g., "left(x,1)='e'"). The parser reads this, follows order precedence (and ideally deals with the absence of any brackets), and then makes a callback to my code with expressions that should be evaluated to a boolean result, such as "a=b"? The custom functions in the expression wouldn't be expected to be understood by the parser (though some basic ones would be useful, like string splitting). It would be excellent to divide the phrase into left- and right-hand sections.

For example, if one side of an AND is false, there is no point in 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). It is preferable that the parser asks the fewest questions necessary to determine the final result.

This must be a lot of labor, yet it seems to happen often. Can somebody provide me some advice? Are there any simple parsers I can use as a start if there aren't any parsers that are as adaptable as those listed above?

Thank You


9/17/2009 10:12:59 AM

Popular Answer

You may want to look into Irony. It allows you to create your language using a syntax that is similar to bnf in C# programming. They even offer a straightforward example (expression evaluator) on their website that seems to be fairly similar to what you're trying to do.

Edit: This year's Lang.Net symposium saw a discuss irony.

Hope this is useful!

11/2/2009 3:23:38 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow