简体   繁体   English

Pyparsing:如何从组中收集所有命名结果?

[英]Pyparsing: How to collect all named results from groups?

I'm using pyparsing and I need to be able to collect all of the variable names from an expression.我正在使用 pyparsing,我需要能够从表达式中收集所有变量名。 It seems like this should be possible with setResultsName , but for expressions with parens or that are otherwise grouped, the variable names are nested. setResultsName似乎应该可以做到这一点,但是对于带有括号或以其他方式分组的表达式,变量名称是嵌套的。

For example,例如,

ParserElement.enablePackrat()
LPAREN, RPAREN, COMMA = map(Suppress, "(),")
expr = Forward()

number = pyparsing_common.number
fn_call = Group(CaselessKeyword('safe_divide') + LPAREN + expr + COMMA + expr + RPAREN)
reserved_words = CaselessKeyword('safe_divide')
variable = ~reserved_words + pyparsing_common.identifier

operand = number | fn_call | variable.setResultsName('var', listAllMatches=True)

unary_op = oneOf("! -")
power_op = Literal("^")
multiplicative_op = oneOf("* / %")
additive_op = oneOf("+ -")
logical_op = oneOf("&& ||")

expr <<= infixNotation(
    operand,
    [
        (unary_op, 1, opAssoc.RIGHT),
        (power_op, 2, opAssoc.RIGHT),
        (multiplicative_op, 2, opAssoc.LEFT),
        (additive_op, 2, opAssoc.LEFT),
        (logical_op, 2, opAssoc.LEFT),
    ],
)

parsed = expr.parseString('(a + b) + c', parse_all=True)
print(parsed.dump())

This gives这给

[[['a', '+', 'b'], '+', 'c']]
[0]:
  [['a', '+', 'b'], '+', 'c']
  - var: [['c']]
    [0]:
      ['c']
  [0]:
    ['a', '+', 'b']
    - var: [['a'], ['b']]
      [0]:
        ['a']
      [1]:
        ['b']
  [1]:
    +
  [2]:
    c

where the variables are returned, but not in an easily accessible format especially for more complex expressions.返回变量的地方,但不是以易于访问的格式,尤其是对于更复杂的表达式。 Is there a way to collect all of the nested variables?有没有办法收集所有嵌套变量?

There's a similar question here , but the workaround there would incorrectly label keywords as variables. 这里有一个类似的问题,但是那里的解决方法会错误地将 label 关键字作为变量。

You could add a parse action to variable to save its name off to a variable list (be sure to insert this code before calling setResultsName):您可以向变量添加解析操作以将其名称保存到变量列表中(请务必在调用 setResultsName之前插入此代码):

found_variables = []
def found_var(s, l, t):
    found_variables.append(t[0])
variable.add_parse_action(found_var)

Be sure to clear the list before calling parse_string a second time.确保在第二次调用 parse_string 之前清除列表。

As I understand it, you want the output to be the list of variables found throughout the tree as a single list.据我了解,您希望 output 作为单个列表在整个树中找到的变量列表。

def gather_named_elements(tree, name):
    named = []
    for i in range(len(tree)):
        if isinstance(tree[i], ParseResults):
            named += tree[i][name].as_list()
            named += gather_named_elements(tree[i], name)
    return list(set([x[0] for x in named]))

print(gather_named_elements(parsed, 'var'))
# OUTPUT: ['a', 'b', 'c']

The order is not deterministic, but you can sort the list if needed.顺序不确定,但您可以根据需要对列表进行排序。

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

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