C#: How to parse arbitrary strings into expression trees?

后端 未结 3 980
旧巷少年郎
旧巷少年郎 2020-12-17 04:09

In a project that I\'m working on I have to work with a rather weird data source. I can give it a \"query\" and it will return me a DataTable. But the query is not a traditi

相关标签:
3条回答
  • 2020-12-17 04:46

    Try out Irony. Though the documentation is lacking, the samples will get you up and running very quickly. Irony is a project for parsing code and building abstract syntax trees, but you might have to write a little logic to create a form that suits your needs. The DLR may be the complement for this, since it can dynamically generate / execute code from abstract syntax trees (it's used for IronPython and IronRuby). The two should make a good pair.

    Oh, and they're both first-class .NET solutions and open source.

    0 讨论(0)
  • 2020-12-17 04:47

    Bison or JavaCC or the like will generate a parser from a grammar. You can then augment the nodes of the tree with your own code to transform the expression.

    OP comments: I really don't want to ship 3rd party executables with my soft. I want it to be compiled in my code.

    Both tools generate source code, which you link with.

    0 讨论(0)
  • 2020-12-17 05:02

    I wrote a parser for exaclty this usage and complexity level by hand. It took about 2 days. I'm glad I did it, but I wouldn't do it again. I'd use ANTLR or F#'s Fslex.

    0 讨论(0)
提交回复
热议问题