-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFA.rb
48 lines (42 loc) · 1.08 KB
/
DFA.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class FARule < Struct.new(:state, :character, :next_state)
def applies_to?(state, character)
self.state == state && self.character == character
end
def follow
next_state
end
def inspect
"#<FARule #{state.inspect} --#{character}--> #{next_state.inspect}>"
end
end
class DFARulebook < Struct.new(:rules)
def next_state(state, character)
rule_for(state, character).follow
end
def rule_for(state, character)
rules.detect do |rule|
rule.applies_to?(state, character)
end
end
end
class DFA < Struct.new(:current_state, :accept_states, :rulebook)
def accepting?
accept_states.include?(current_state)
end
def read_character(character)
self.current_state = rulebook.next_state(current_state, character)
end
def read_string(str)
str.chars.each do |character|
read_character(character)
end
end
end
class DFADesign < Struct.new(:start_state, :accept_states, :rulebook)
def to_dfa
DFA.new(start_state, accept_states, rulebook)
end
def accepts?(str)
to_dfa.tap{ |dfa| dfa.read_string(str) }.accepting?
end
end