
只想加入運算能力
|
129
#<struct PDAConfiguration state=2, stack=#<Stack (a)bba$>>,
#<struct PDAConfiguration state=3, stack=#<Stack ($)>>
}>
最後是直接測試字串的
NPDADesign
類別:
class NPDADesign < Struct.new(:start_state, :bottom_character,
:accept_states, :rulebook)
def accepts?(string)
to_npda.tap { |npda| npda.read_string(string) }.accepting?
end
def to_npda
start_stack = Stack.new([bottom_character])
start_configuration = PDAConfiguration.new(start_state, start_stack)
NPDA.new(Set[start_configuration], accept_states, rulebook)
end
end
現在可以檢測我們的 NPDA 真的能識別回文:
>> npda_design = NPDADesign.new(1, '$', [3], rulebook)
=> #<struct NPDADesign …>
>> npda_design.accepts?('abba') ...