我过去曾使用pyparsing,但仅用于小型任务,这次我正尝试将其用于更复杂的事情。
我正在尝试跳过看起来像这样的VHDL架构块
Referer
这是我尝试过的:
import tensorflow #v1.4
for i in ensemble_model_paths_list:
saver = tf.train.import_meta_graph(i+'.meta',clear_devices=True)
config = tf.ConfigProto()
config.allow_soft_placement = True
config.log_device_placement = False
with tf.Session(config=config) as sess:
saver.restore(sess,i)
output = tf.get_collection('output')
#....process inputs here using feed_dict and placeholder names....
tf.reset_default_graph()
但是将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)
之间添加其他文本(我希望将其跳过),以确保使用“空跳过”不是问题,但这无济于事要么。
我在做什么错了?
答案 0 :(得分:1)
SkipTo
跳到匹配的文本,但默认情况下 not 不会解析该文本。因此,您正在将解析位置推进到“最终体系结构”,但实际上并未对其进行解析。
您可以:
Keyword('end') + Keyword('architecture')
表达式后SkipTo
,或者include=True
表达式的构造函数中传递SkipTo
,告诉它跳过和解析给定的字符串。