在MatchFirst中解析Verilog网络连接

时间:2012-07-31 17:36:54

标签: python verilog string-concatenation pyparsing

我有一个简单的语法来解析verilog连接:

scalar = (Optional(Word(nums + "_") + "'" + oneOf("d h b")) + Word(nums + "_")).setName("Scalar")
operator = oneOf("+ - * /").setName("Operator")
identifier = Word(alphas, alphanums+"_").setName("Identifier")
expr = (scalar | identifier) + ZeroOrMore(operator + (scalar | identifier))
range = expr + ":" + expr
index = "[" + range + "]"
single_netref = (identifier + Optional(index)).setName("Single Net Reference")
single_netref.setDebug()
lbracket = Literal("{").setName("Left Bracket").setDebug()
rbracket = Literal("}").setName("Left Bracket").setDebug()
concat_netref = (lbracket + single_netref + ZeroOrMore( Suppress( "," ) + single_netref ) + rbracket).setName("Net Concatination Reference")
concat_netref.setDebug()

如果我使用parseString方法解析字符串“{LegacyStatus3gPreStageQ1,LegacyData3gPreStageQ1}”,它解析得很好;但是,我希望匹配网络引用的串联或单个网络引用。为此,我尝试过:

netref = MatchFirst(concat_netref, single_netref).setName("Net Reference").setDebug()

然而,这个(并使用Or())总是跳过最后一个rbracket,但是说它成功匹配了netref。这是我的调试输出:

Match Net Reference at loc 0(1,1)
Match Left Bracket at loc 0(1,1)
Matched Left Bracket -> ['{']
Match Single Net Reference at loc 1(1,2)
Matched Single Net Reference -> ['LegacyStatus3gPreStageQ1']
Match Single Net Reference at loc 28(1,29)
Matched Single Net Reference -> ['LegacyData3gPreStageQ1']
Matched Net Reference -> ['{', 'LegacyStatus3gPreStageQ1', 'LegacyData3gPreStageQ1']

为什么添加MatchFirst()元素会导致错过rbracket?

1 个答案:

答案 0 :(得分:2)

MatchFirst应该传递一个表达式列表。

更改

netref = MatchFirst(concat_netref, single_netref).setName("Net Reference")

netref = MatchFirst([concat_netref, single_netref]).setName("Net Reference")