是否有任何好的教程来描述如何使用ANTLR来解析布尔搜索字符串 [英] Are there any good tutorials that describe how to use ANTLR to parse boolean search strings

查看:83
本文介绍了是否有任何好的教程来描述如何使用ANTLR来解析布尔搜索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将关键字和运算符的布尔搜索字符串解析为要从C#代码执行的SQL查询.我认为像ANTLR这样的东西是我完成这项任务所需要的,但是我不确定如何去做.

I need to parse a boolean search string of keywords and operators into a SQL query to be executed from C# code. I think something like ANTLR is what I need for this task, but I'm not sure how to do it.

关于如何执行此操作,是否有任何好的教程?或者也许我需要其他工具?

Are there any good tutorials on how to do this? Or maybe I need a different tool?

下面是我的意思的示例.我唯一需要的运算符是AND和OR.我还希望能够使用括号.

An example of what I mean is below. The only operators I need are AND and OR. I would also like to be able to use parenthesis.

输入表达式:(蓝色和绿色)或黄色

input expression: (blue AND green) OR yellow

输出:

选择* 从表 在哪里(包含(描述,蓝色")和包含(描述,绿色"))或>包含(描述,黄色")

SELECT * FROM table WHERE (CONTAINS(Description, "blue") AND CONTAINS(Description, "green")) OR >CONTAINS(Description, "yellow"

如果可能的话,我也想支持隐式AND插入.因此,如果未指定任何默认运算符,则为AND.

If possible I would also like to support implicit AND insertion. So the default operator if none is specified is AND.

输入:绿色(蓝色或黄色)

input : green (blue OR yellow)

输出: 选择 * 从表 包含(描述,绿色")和(包含(描述,蓝色")或>包含(描述,黄色"))的

output: SELECT * FROM table WHERE CONTAINS(Description, "green") AND (CONTAINS(Description, "blue") OR >CONTAINS(Description, "yellow"))

推荐答案

我可能会使用纯文本处理来做到这一点.本质上,您要执行的操作是采用原始表达式,提取所有颜色并将其替换为CONTAINS子句.用伪代码:

I'd probably do this with straight text processing. Essentially what you want to do is take the original expression, pull out all the colors and replace them with the CONTAINS clause. In pseudocode:

list of tokens = split input expression into tokens separated by spaces
foreach token in tokens
    if token is not keyword
        text = "CONTAINS(Description, \"" + token + "\")"
    else 
        text = token

    output = output + text
end

其中的关键字为(","AND"等,然后将输出放在SQL语句中

Where the keywords are "(", "AND", etc. You then just put the output in your SQL statement

<在此处插入有关SQL注入攻击的警告>

< insert warning about SQL injection attacks here >

这篇关于是否有任何好的教程来描述如何使用ANTLR来解析布尔搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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