-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathconcatenate.rb
31 lines (25 loc) · 895 Bytes
/
concatenate.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
require_relative '../finite_automata/fa_rule'
require_relative '../finite_automata/nfa_design'
require_relative '../finite_automata/nfa_rulebook'
require_relative 'pattern'
class Concatenate < Struct.new(:first, :second)
include Pattern
def to_s
[first, second].map { |pattern| pattern.bracket(precedence) }.join
end
def precedence
1
end
def to_nfa_design
first_nfa_design = first.to_nfa_design
second_nfa_design = second.to_nfa_design
start_state = first_nfa_design.start_state
accept_states = second_nfa_design.accept_states
rules = first_nfa_design.rulebook.rules + second_nfa_design.rulebook.rules
extra_rules = first_nfa_design.accept_states.map { |state|
FARule.new(state, nil, second_nfa_design.start_state)
}
rulebook = NFARulebook.new(rules + extra_rules)
NFADesign.new(start_state, accept_states, rulebook)
end
end