简体   繁体   English

操作员的Pyparsing问题

[英]Pyparsing problem with operators

I did a grammar with pyparsing, and I have a problem. 我做了一个带有pyparsing的语法,我有一个问题。 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()

One way to address your problem is to define AND as an Optional operator. 解决问题的一种方法是将AND定义为Optional运算符。 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). 此外,使用Optional,您可以添加默认字符串,这样即使原始搜索查询中缺少“和”,您的解析文本也会为您插入(以便于解析后处理)。

from pyparsing import *

QUOTED = quotedString.setParseAction(removeQuotes)  
OAND = CaselessLiteral("and") 
OOR = CaselessLiteral("or") 
ONOT = Literal("-")
WWORD = ~OAND + ~OOR + ~ONOT + Word(printables.replace("(", "").replace(")", ""))
TERM = (QUOTED | WWORD)  
EXPRESSION = operatorPrecedence(TERM,
    [
    (ONOT, 1, opAssoc.RIGHT),
    (Optional(OAND,default="and"), 2, opAssoc.LEFT),
    (OOR, 2, opAssoc.LEFT)
    ])

STRING = OneOrMore(EXPRESSION) + StringEnd()

tests = """\
word and ward or wird
word werd or wurd""".splitlines()

for t in tests:
    print STRING.parseString(t)

Gives: 得到:

[[['word', 'and', 'ward'], 'or', 'wird']]
[[['word', 'and', 'werd'], 'or', 'wurd']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM