我以前使用过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)
我还尝试在两个文本之间添加其他文本
is
和
end
(我希望被跳过)确保没有“空跳过”的问题,但这也没有帮助。