python - Pyparsing infixNotation 为解析树 : Only one operator ending up in tree?

标签 python pyparsing infix-notation

我的最终目标是应用 sql-where-clause-style 查询来过滤 pandas 数据帧。一些搜索让我找到了 pyparsing 的 infixNotation 方法。

我在这里找到了一个中缀表示法的示例:http://nullege.com/codes/show/src%40p%40y%40pyparsing-2.0.2%40examples%40simpleBool.py/15/pyparsing.infixNotation/python#

但这实际上处理了符号,我需要在不同的数据上多次使用树。在寻找解析树的帮助时我发现了这个: http://pyparsing.wikispaces.com/file/view/fourFn.py/30154950/fourFn.py

我从那里获取了堆栈实现。然而,它的行为并不像我预期的那样,所以我希望有人能告诉我我做错了什么。

这是我能想到的最简单的情况:

from pyparsing import Word, alphas, infixNotation, opAssoc

exprStack=[]
def pushFirst( strg, loc, toks ):
    exprStack.append( toks[0] )

def addAnd():
    exprStack.append("and")

varname = Word(alphas).setParseAction( pushFirst )

operators=[("and", 2, opAssoc.LEFT,addAnd)]

whereExpr = infixNotation(varname,operators)

exprStack=[]
teststring = "cheese and crackers and wine"
parsed=whereExpr.parseString(teststring)
for p in exprStack:
    print p

我从这段代码中得到的是:

cheese
crackers
wine
and

根据我对中缀表示法应该如何工作的理解,我期望得到的是:

cheese
crackers
wine
and
and

我也尝试用“奶酪、 cookies 、 Wine 和提示”来运行它,但我的列表中仍然只有一个“和”。

我在使用 infixNotation 方法时误解了什么?

谢谢

最佳答案

首先使用 pyparsing 的 traceParseAction 诊断装饰器装饰您的解析操作。

@traceParseAction
def addAnd():
    exprStack.append("and")

traceParseAction 将显示匹配的源代码行、匹配标记的起始位置、传递给解析操作的标记以及解析操作返回的值:

>>entering addAnd(line: 'cheese and crackers and wine', 0, 
                 ([(['cheese', 'and', 'crackers', 'and', 'wine'], {})], {}))
<<leaving addAnd (ret: None)

这些标记看起来有点令人困惑,因为您得到的是一个 pyparsing ParseResults 对象,它同时具有列表和字典语义,因此 Python repr 的该对象首先显示其列表内容,然后显示其命名内容。看起来像带有列表和字典的元组的实际上是 ParseResults,在本例中,它是一个 ParseResults,其第一个元素是另一个 ParseResults,并且此嵌套对象是包含匹配标记列表的对象。

如果您向解析操作添加 tokens 参数,然后打印出 tokens.dump():

def addAnd(tokens):
    print(tokens.dump())
    exprStack.append("and")

你会得到更多的可读性:

[['cheese', 'and', 'crackers', 'and', 'wine']]
[0]:
  ['cheese', 'and', 'crackers', 'and', 'wine']

您可以看到匹配的标记不仅包含“and”,还包含所有 and-ed 术语,因此您需要将与匹配标记中一样多的“and”推送到 exprStack 上。

def addAnd(tokens):
    exprStack.extend(tokens[0][1::2])

通过此更改,您现在应该将其视为返回的堆栈:

cheese
crackers
wine
and
and

关于python - Pyparsing infixNotation 为解析树 : Only one operator ending up in tree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251750/

相关文章:

python - 在pyparsing中检测变量类型

.net - F# 多种类型的通用中缀运算符(fmap、applicative、bind 等)

math - 计算一串简单的数学表达式

parsing - 我无法创建支持中缀、后缀和前缀函数等的语言有什么原因吗?

引用同一包中其他文件的 Python Egg 库文件

python:指数特殊方法?

python - 有效地将索引附加到 Python 中的重复字符串

python - 如何在 Pandas 数据框上应用 scipy 函数

python - pyparsing lineno 问题(bug?)

.net - .NET 中最接近 pyparsing 的是什么?