在 pyparsing 中评估 bool

evaluating bool in pyparsing

我的代码基于 http://pyparsing.wikispaces.com/file/view/simpleBool.py/451074414/simpleBool.py 我想像这样解析生成单词的语言:

ABC:"a" and BCA:"b" or ABC:"d"

解析后我想评估这个表达式的布尔值。 在代码中,我有带有键 ABC 和 BCA 的字典,而 ABC:"a" 在字典 [ABC] 中表示 "a"。

我在某个地方犯了错误,但找不到哪里,总是转换为 bool return 正确。

输出:

DEBUG self.value=True

[ABC:"a"[True]] True

DEBUG self.value=False

[ABC:"h"[False]] True

代码:

from pyparsing import infixNotation, opAssoc, Keyword, Word, alphas, dblQuotedString, removeQuotes

d = {
    "ABC": "TEST abc TEST",
    "BCA": "TEST abc TEST",
}


class BoolOperand:
    def __init__(self, t):
        self.value = t[2] in d[t[0]]
        print(F"DEBUG self.value={self.value}")
        self.label = f"{t[0]}:\"{t[2]}\"[{str(self.value)}]"

    def __bool__(self):
        print("GET V")
        return self.value

    def __str__(self):
        return self.label

    __nonzero__ = __bool__
    __repr__ = __str__


class BoolBinOp:
    def __init__(self, t):
        self.args = t[0][0::2]

    def __str__(self):
        sep = " %s " % self.reprsymbol
        return "(" + sep.join(map(str, self.args)) + ")"

    def __bool__(self):
        print("DEBUG BoolBinOp")
        return self.evalop(bool(a) for a in self.args)

    __nonzero__ = __bool__
    __repr__ = __str__


class BoolAnd(BoolBinOp):
    reprsymbol = '&'
    evalop = all


class BoolOr(BoolBinOp):
    reprsymbol = '|'
    evalop = any


class BoolNot:
    def __init__(self, t):
        self.arg = t[0][1]

    def __bool__(self):
        print("DEBUG BoolNot")
        v = bool(self.arg)
        return not v

    def __str__(self):
        return "~" + str(self.arg)

    __repr__ = __str__
    __nonzero__ = __bool__


EXPRESSION = Word(alphas) + ":" + dblQuotedString().setParseAction(removeQuotes)
TRUE = Keyword("True")
FALSE = Keyword("False")
boolOperand = TRUE | FALSE  | EXPRESSION
boolOperand.setParseAction(BoolOperand)

boolExpr = infixNotation(boolOperand,
                         [
                             ("not", 1, opAssoc.RIGHT, BoolNot),
                             ("and", 2, opAssoc.LEFT, BoolAnd),
                             ("or", 2, opAssoc.LEFT, BoolOr),
                         ])

if __name__ == "__main__":
    res = boolExpr.parseString('ABC:"a"')
    print(res, "\t", bool(res))
    print("\n\n")
    res = boolExpr.parseString('ABC:"h"')
    print(res, "\t", bool(res))

如果在程序末尾添加:

print(type(res), bool(res))
print(type(res[0]), bool(res[0]))

你会看到

<class 'pyparsing.ParseResults'> True
GET V
<class '__main__.BoolOperand'> False

res 不是您解析的操作数,它是您解析的操作数的 ParseResults 容器。如果你评估 res[0] 你会看到你的操作数是如何评估的。

ParseResults 将具有与关于 bool 的列表类似的行为。如果非空,它们将是 True,如果是空的,它们将是 False。将这些行添加到您的程序中:

res.pop(0)
print(bool(res))

然后你会看到ParseResultsFalse,说明它没有内容。