2

我有一个解析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 丢失?

4

1 回答 1

2

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

改变

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

netref = MatchFirst([concat_netref, single_netref]).setName("Net Reference")
于 2012-07-31T18:20:09.790 回答