解析用户的查询
这就是我想要实现的目标。 我想为我的用户提供一个类似谷歌的文本框,他们可以在其中输入查询。 我希望他们能够表达半自然语言,例如,
"view all between 1/1/2008 and 1/2/2008"
如果语法必须相当结构化并仅限于这个特定领域,那就可以了……这些都是将使用它的专家用户。
最终,我想我希望解析结果可以作为某种表达式树提供。 但是,如果您对哪种数据结构有其他一些想法可能会更好。
这是 C# 中的:-)
So here's what I'm looking to achieve. I would like to give my users a single google-like textbox where they can type their queries. And I would like them to be able to express semi-natural language such as
"view all between 1/1/2008 and 1/2/2008"
it's ok if the syntax has to be fairly structured and limited to this specific domain ... these are expert users who will be using this.
Ultimately, I think I'd like the parse results to be available as some sort of expression tree. But if you've got some other ideas about what data structure might be better.
This is in C# :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您正在描述一种编程语言。 当然,它是一种小语言(通常称为小语言,或领域特定语言(DSL))。 如果您从未听说过递归下降解析器这个术语,那么您最好遵循 Paul 的建议并使用某些描述的下拉框。
然而,我再次同意他的观点,如果你想这样做,Antlr 就是你的最佳选择。 该网站上有一些教程可以帮助您入门。 基本上,您需要使用 Backus-Naur Form 表示法来描述语法。
然后,您将在您的语法上运行 Antlr,它将生成您的解析器。 然后,您可以将教科书中的输入输入到抽象语法树中。 然后,您可以使用该树来生成查询。 这并不像听起来那么困难,但也有一些困难。
如果您真的对此感兴趣并且/或想要扩展您的编程翅膀,您可以通过 Dragon Book(又名编译器:原理、技术和工具)阅读有关该主题的更多信息。
祝你好运,我的朋友。
You are describing a programming language. Granted it's a small language (often called a little language, or Domain Specific Language (DSL)). If you've never heard the term recursive descent parser, you are probably better off following Paul's advice and using drop down boxes of some description.
However, again, I would have to agree with him, that if you want to do it, Antlr is the way to go. There are tutorials on the site that might help you get started. Basically, you will need to describe how the syntax using Backus-Naur Form notation.
You will then run Antlr over your grammer, and it will generate your parser. You can then feed the input from your textbook into an Abstract Syntax Tree. You can then use that Tree to generate your query. It's not as difficult as it all sounds, but there's a bit to it.
If you're really into this and/or want to stretch your programming wings a bit, you could read more on the topic with the Dragon Book, AKA Compilers: Principles, Techniques and Tools.
Good luck my friend.
对于一种非常简单的语言,我会使用正则表达式。 主要好处是您不必处理任何代码生成。 不过,模式匹配的调试基本上为零。
如果您的语言相当复杂(您不介意在单个语法文件中指定整个内容),我会选择 Coco/R ——它速度快、易于使用,并且可以生成极其可调试的代码。
对于更复杂的语言,我目前最喜欢的是 Antlr v3。 支持多文件语法(通过“导入”语句),这非常好。 生成的代码是可调试的,但在调试被认为“容易”之前需要一些时间来适应。
For a very simple language, I'd go with regexps. Main benefit there is you don't have to deal with any code generation. Debugging of the pattern matching is basically nil, though.
If your language is moderately complex (you wouldn't mind specifying the entire thing in a single grammar file), I'd go with Coco/R -- it's fast, easy to use, and makes extremely debuggable code.
For a more complex language, my current favorite is Antlr v3. Supports multi-file grammars (via the 'import' statement), which is very nice. The generated code is debuggable, but takes a bit of getting used to before debugging could be considered 'easy.'
我以前也遇到过这种情况。 经过多次讨论,我们认为上下文相关的下拉菜单是比文本框更好的解决方案。
例如,有 4 个下拉菜单,但最后 3 个被禁用。 然后,当用户从第一个选项中选择一个选项时,它会填充并启用其他选项。 因此,他们会选择“查看全部”,然后选择“之间”,然后可能会弹出最后两个的文本框或日历。
这是一个与我正在谈论的示例类似的示例
I have been in this situation before. After much discussion, we decided that context-sensitive dropdowns were a better solution than just a textbox.
For instance, have 4 dropdowns, but the last 3 are disabled. Then, when the user selects an option from the first, it populates and enables the others. So they would select "view all" then "between", and then maybe pop a textbox or a calendar for the last two.
Here's an example that's kind of like what I am talking about
表达式树是个好主意。 有许多优秀的通用解析器和解析器生成器,有开源的也有商业的,它们可以将正确的查询字符串转换为表达式树。
An expression tree is a good idea. There are many good general parsers and parser generators out there, open-source as well as commercial, which can transform correct query strings into expression trees.
使用 Oslo,它是专门为此设计的...
Use Oslo, it's designed specifically for this...
我不知道这是否会对您有帮助,但我们所做的是:我们为用户提供了预定义的标准,例如日期选择,我们为他们提供了两个日历弹出窗口,以便他们可以选择日期范围和日期范围...我们做到了不强制执行...因此日期过滤器仅在用户指定日期时才应用...类似地,您也可以为用户提供预定义的标准选择...除此之外,表达式树似乎是一个很好的解决方法。
I dont know if this will help you , but what we did was : we gave the user pre-defined criterias like for date selection we gave them two calendar pop-ups so that they can select from and to date range... we did not make it compulsory.. so date filter use to apply only when user specifies the dates... similarly you can also give the users a pre-defined criteria selection... other then that expression tree seems to be a nice workaround for that.
GOLD Parser Generator 有一个有用的 UI,可用于设计和测试语法,还有相当好的教程和文档,并且很容易在 C# 中使用。
The GOLD Parser Generator has a helpful UI for designing and testing your grammar, reasonably good tutorials and documentation, and is easy to use from C#.
尝试解析这些东西将是一场灾难,最终对用户来说非常有限,因此让他们感到沮丧而不是帮助他们。 我建议使用预定义的查询子句,以及某种查询生成器工具,该工具以下拉形式提供所有可用选项。 对于不同的数据类型,您可以使用不同的布尔运算符(大于、小于数字,不像字符串等),但是我认为这比实际尝试解析用户可能输入的内容更有意义。
Trying to parse that stuff would be a disaster, and ultimatley very limiting to the user, thus frustrating them more then helping them. I would suggest using pre-defined Query clasues, with some kind of query builder tool that has all the available options in drop down form. You can have different boolean operators for different Data Types (greater than, less than for numeric, like not like for strings etc), however I think that would make a helluva lot more sense than actually trying to parse out what a user may type.