识别递归PAR的最外层调用

2024-09-30 00:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一些代码只在对递归解析器的最外层调用中运行,但是想不出用pyparsing实现这一点的方法。这是我的密码:

from pyparsing import *
from itertools import count

L_PAR, R_PAR, = map(Suppress, '()')


def process_stmt(counter):
    """Closure counts up when nesting occurs"""

    def parse_action(tokens):
        for t in tokens:
            t["count"] = next(counter)
            # if not outermost:
            t["outermost"] = False

            # if outermost:
            #     t["outermost"] = True

    return parse_action


def outermost_true(tokens):
    tokens['outermost'] = True
    tokens['other'] = True


counter = count(0)
OR_stmt = Forward()
AND_stmt = Group(OneOrMore(Word("XYZ", exact=3)
                           ^ OR_stmt))("AND*")
AND_stmt.setParseAction(process_stmt(counter))
OR_stmt <<= Group(L_PAR
                  + OneOrMore(AND_stmt)
                  + R_PAR)("OR*")
OR_stmt.setParseAction(process_stmt(counter))
AND_stmt.addParseAction(outermost_true)

data = "(XXXYYY)ZZZ"

AND_stmt.runTests(data)

产生以下结果

(XXXYYY)ZZZ
[[[['XXX', 'YYY']], 'ZZZ']]
- AND: [[[['XXX', 'YYY']], 'ZZZ']]
  [0]:
    [[['XXX', 'YYY']], 'ZZZ']
    - OR: [[['XXX', 'YYY']]]
      [0]:
        [['XXX', 'YYY']]
        - AND: [['XXX', 'YYY']]
          [0]:
            ['XXX', 'YYY']
            - count: 0
            - outermost: False
        - count: 1
        - other: True
        - outermost: False
    - count: 2
    - outermost: False
- other: True
- outermost: True

如何将最外层的outermost属性设置为True?你知道吗


Tags: orandfalsetruedefcountcounterprocess
1条回答
网友
1楼 · 发布于 2024-09-30 00:28:54

当我看到你把outermost设为TrueFalse时,我想我误解了你的意图。重新阅读您的问题,您希望代码只在对递归解析器的最外层调用中运行。正如我在评论中猜测的那样,解决方案是创建一个外部Forward容器并将解析操作附加到该容器上。以下是您的代码,其中删除了对process_stmt的所有中间调用,只对包含Forward的最外层进行了一次调用:

counter = count(0)
OR_stmt = Forward()
AND_stmt = Group(OneOrMore(Word("XYZ", exact=3)
                           ^ OR_stmt))("AND*")
OR_stmt <<= Group(L_PAR
                  + OneOrMore(AND_stmt)
                  + R_PAR)("OR*")
outermost = Forward()
outermost <<= AND_stmt
outermost.addParseAction(outermost_true)

data = "(XXXYYY)ZZZ"

outermost.runTests(data)

它给出:

(XXXYYY)ZZZ
[[[['XXX', 'YYY']], 'ZZZ']]
- AND: [[[['XXX', 'YYY']], 'ZZZ']]
  [0]:
    [[['XXX', 'YYY']], 'ZZZ']
    - OR: [[['XXX', 'YYY']]]
      [0]:
        [['XXX', 'YYY']]
        - AND: [['XXX', 'YYY']]
          [0]:
            ['XXX', 'YYY']
- other: True
- outermost: True

如果您仍然希望为层次结构的每个级别调用一个解析操作,我修改了process stmt,以便在每次调用时打印其进度:

def process_stmt(counter):
    """Closure counts up when nesting occurs"""

    def parse_action(tokens):
        for t in tokens:
            t["count"] = next(counter)
            # t["outermost"] = False
            t["outermost"] = (False, "used to be {!r}".format(t.outermost))

        tokens['outermost'] = True

        print(tokens.dump())
        print()
    return parse_action

显示这些中间步骤-更好地可视化每个调用如何将内部级别outermost重置为False,将自己的最外层级别重置为True

[['XXX', 'YYY']]
- AND: [['XXX', 'YYY']]
  [0]:
    ['XXX', 'YYY']
    - count: 0
    - outermost: (False, "used to be ''")
- outermost: True

[[['XXX', 'YYY']]]
- OR: [[['XXX', 'YYY']]]
  [0]:
    [['XXX', 'YYY']]
    - AND: [['XXX', 'YYY']]
      [0]:
        ['XXX', 'YYY']
        - count: 0
        - outermost: (False, "used to be ''")
    - count: 1
    - outermost: (False, 'used to be True')
- outermost: True

[[[['XXX', 'YYY']], 'ZZZ']]
- AND: [[[['XXX', 'YYY']], 'ZZZ']]
  [0]:
    [[['XXX', 'YYY']], 'ZZZ']
    - OR: [[['XXX', 'YYY']]]
      [0]:
        [['XXX', 'YYY']]
        - AND: [['XXX', 'YYY']]
          [0]:
            ['XXX', 'YYY']
            - count: 0
            - outermost: (False, "used to be ''")
        - count: 1
        - outermost: (False, 'used to be True')
    - count: 2
    - outermost: (False, 'used to be True')
- outermost: True

相关问题 更多 >

    热门问题