我正在查看此 ACM problem 的输入格式,具体来说:
An NTA description is given by the number of states
n
followed by the number of accepting states on one line separated by whitespace. Then × n
transition table follows in row-major order; each transition string is given on a separate line.
(忽略其余部分,它不相关。)
例如:
3 1
a
a
c
ca
a
b
c
b
a
这意味着第一行之后的 9 (3²) 行是转换。在我的代码中,我需要保留 3 值和 1 值,以及 9 个转换的列表。理想情况下,我想要一个能够给出我的表达式:
- 3
- 1
['ab', 'a', 'c', 'a', 'ab', 'b', 'c', 'b', 'ab']
我的第一个想法是尝试基于 countedArray()
的表达式:
from pyparsing import pyparsing_common, Word, alphas, countedArray
table_start = pyparsing_common.integer*2
table_start.addParseAction(lambda toks: toks[0]**2)
table_transitions = countedArray(Word(alphas), table_start)
但是 countedArray()
抑制了计数表达式,这意味着我丢失了 1 的值(接受状态的数量),只能通过取平方根来取回 3结果列表的长度。
我不太关心这个问题的完整解析,因为 ACM 问题让您假设输入的格式正确。因此我可以轻松地对结果使用更简单的表达式和简单的 Python 操作。但我正在学习 Pyparsing,并且想知道是否可以使用该库以直接的方式实现这一点(特别是因为我在实际项目中确实遇到了类似的语法,我想使用 Pyparsing 来简化)。
最佳答案
这是一种黑客行为,但如果您扩展 table_start
上的解析操作以在 table_transitions
上设置解析操作,则可以将接受状态添加为table_transitions
上的命名结果:
def replace_count(toks):
table_transitions.setParseAction(lambda t: t.__setitem__('num_accepting_states', toks[1]))
toks[0] *= toks[0]
table_start.addParseAction(replace_count)
result = table_transitions.parseString(data)
print(result[0])
print(result.num_accepting_states)
打印:
['a', 'a', 'c', 'ca', 'a', 'b', 'c', 'b', 'a']
1
关于python - Pyparsing 如何在不丢弃复杂计数表达式的情况下匹配计数数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59762452/