我用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()