我过去曾使用pyparsing,但仅用于小型任务,这一次,我试图将其用于更复杂的事情。

我试图跳过一个VHDL架构块,看起来像这样

architecture Behav of Counter is
  ...many statements I'm not interested in at this point...
end architecture;


这是我尝试过的:

import pyparsing as pp
pp_identifier = pp.Regex(r'([a-zA-Z_][\w]*)')('identifier')
def Keyword(matchString):
    'VHDL keywords are caseless and consist only of alphas'
    return pp.Keyword(matchString, identChars=pp.alphas, caseless=True)

pp_architecture = (
    Keyword('architecture')
    + pp_identifier
    + Keyword('of').suppress()
    + pp_identifier
    + Keyword('is').suppress()
    + Keyword('end')
    + Keyword('architecture')
    )

print(pp_architecture.parseString('''
architecture beh of sram is end architecture
''', parseAll=True))

# this works as I expected, it prints
# ['architecture', 'beh', 'sram', 'end', 'architecture']


但是将pp_architecture更改为使用SkipTo后,它将失败:

pp_architecture = (
    Keyword('architecture')
    + pp_identifier
    + Keyword('of').suppress()
    + pp_identifier
    + Keyword('is').suppress()
    + pp.SkipTo(Keyword('end') + Keyword('architecture'))
    )

print(pp_architecture.parseString('''
architecture beh of sram is end architecture
''', parseAll=True))
Traceback (most recent call last):
  File "<stdin>", line 3, in <module>
  File "C:\Python27\lib\site-packages\pyparsing.py", line 1125, in parseString
    raise exc
pyparsing.ParseException: Expected end of text (at char 29), (line:2, col:29)


我还尝试在isend之间添加其他文本(我希望将其跳过),以确保它不是“空跳过”的问题,但这也无济于事。

我究竟做错了什么?

最佳答案

SkipTo跳到匹配的文本,但是默认情况下不解析该文本。因此,您正在将解析位置推进到“最终体系结构”,但实际上并未对其进行解析。

您可以:


Keyword('end') + Keyword('architecture')表达式后添加SkipTo,或者
include=True表达式的构造函数中传递SkipTo,告诉它跳过并解析给定的字符串。

关于python - 正确使用SkipTo,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52712042/

10-12 21:22