NodeVisitor class for PEG parser in Python

前端 未结 2 1297
一生所求
一生所求 2020-12-12 00:10

Imagine the following types of strings:

if ((a1 and b) or (a2 and c)) or (c and d) or (e and f)

Now, I\'d like to get the expressions in pa

2条回答
  •  再見小時候
    2020-12-12 00:40

    If you only want to return each outermost factor, return early and do not descend into its children.

    def walk(node, level = 0):
        if node.expr.name == "factor":
            print(level * "-", node.text)
            return
        for child in node.children:
            walk(child, level + 1)
    

    Output:

    ----- ((a1 and b) or (a2 and c))
    ----- (c and d)
    ------ (e and f)
    

提交回复
热议问题