使用pyparsing运算符优先权来解析空查询

2024-05-15 11:51:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用pyparsing.OperatorReceidence解析中缀表示法查询。代码是

 filterExpr = pp.quotedString.setParseAction(pp.removeQuotes) |\
                 pp.Word(pp.printables, excludeChars="()")

searchTerm = (~and_ + ~or_ + ~not_) + filterExpr

searchExpr = pp.operatorPrecedence(
                searchTerm,
                [
                (not_, 1, pp.opAssoc.RIGHT, SearchNotOperation),
                (pp.Optional(and_, default="AND"), 2, pp.opAssoc.LEFT, SearchAndOperation),
                (or_, 2, pp.opAssoc.LEFT, SearchOrOperation),
                ])

这将解析诸如'(foo bar)或baz'之类的表达式,但在边缘大小写时失败,其中提供了空字符串或空方括号“()”。 我如何解决这个问题?在


Tags: orand代码notpyparsingleftpp表示法