运算符的 Pyparsing 问题
我用 pyparsing 做了语法,但遇到了问题。 该语法尝试解析搜索查询(使用运算符优先级、括号等),并且我需要空格来像 and 运算符一样工作。
例如,这工作正常:
(word and word) or word
但这失败:
(word word) or word
我希望第二个查询像第一个查询一样工作。
我的实际语法是:
WWORD = printables.replace("(", "").replace(")", "")
QUOTED = quotedString.setParseAction(removeQuotes)
OAND = CaselessLiteral("and")
OOR = CaselessLiteral("or")
ONOT = "-"
TERM = (QUOTED | WWORD)
EXPRESSION = operatorPrecedence(TERM,
[
(ONOT, 1, opAssoc.RIGHT),
(OAND, 2, opAssoc.LEFT),
(OOR, 2, opAssoc.LEFT)
])
STRING = OneOrMore(EXPRESSION) + StringEnd()
I did a grammar with pyparsing, and I have a problem.
The grammar tries to parse a search query (with operator precedence, parenthesis, etc), and I need for spaces to work like the and operator.
For example, this works fine:
(word and word) or word
But this fails:
(word word) or word
And I want the second query to works like the first one.
My actual grammar is:
WWORD = printables.replace("(", "").replace(")", "")
QUOTED = quotedString.setParseAction(removeQuotes)
OAND = CaselessLiteral("and")
OOR = CaselessLiteral("or")
ONOT = "-"
TERM = (QUOTED | WWORD)
EXPRESSION = operatorPrecedence(TERM,
[
(ONOT, 1, opAssoc.RIGHT),
(OAND, 2, opAssoc.LEFT),
(OOR, 2, opAssoc.LEFT)
])
STRING = OneOrMore(EXPRESSION) + StringEnd()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决问题的一种方法是将 AND 定义为可选运算符。如果您这样做,则必须格外小心,确保“and”和“or”等真正的关键字不会被误解为搜索词。此外,使用Optional,您可以添加默认字符串,这样即使原始搜索查询中缺少“and”,您的解析文本也会为您插入它(以便于解析后处理)。
给出:
One way to address your problem is to define AND as an Optional operator. If you do this, you'll have to take extra care that real keywords like 'and' and 'or' aren't misinterpreted as search words. Also, with Optional, you can add a default string, so that even if the "and" is missing in the original search query, your parsed text will insert it for you (for easier post-parse processing).
Gives: