Object
# File lib/journey/nfa/transition_table.rb, line 34 34: def []= i, f, s 35: @table[f][i] = s 36: end
# File lib/journey/nfa/transition_table.rb, line 18 18: def accepting? state 19: accepting == state 20: end
# File lib/journey/nfa/transition_table.rb, line 22 22: def accepting_states 23: [accepting] 24: end
# File lib/journey/nfa/transition_table.rb, line 26 26: def add_memo idx, memo 27: @memos[idx] = memo 28: end
# File lib/journey/nfa/transition_table.rb, line 111 111: def alphabet 112: inverted.values.map(&:keys).flatten.compact.uniq.sort_by { |x| x.to_s } 113: end
Returns a set of NFA states reachable from some NFA state s in set t on nil-transitions alone.
# File lib/journey/nfa/transition_table.rb, line 118 118: def eclosure t 119: stack = Array(t) 120: seen = {} 121: children = [] 122: 123: until stack.empty? 124: s = stack.pop 125: next if seen[s] 126: 127: seen[s] = true 128: children << s 129: 130: stack.concat inverted[s][nil] 131: end 132: 133: children.uniq 134: end
Returns set of NFA states to which there is a transition on ast symbol a from some state s in t.
# File lib/journey/nfa/transition_table.rb, line 96 96: def following_states t, a 97: Array(t).map { |s| inverted[s][a] }.flatten.uniq 98: end
Returns a generalized transition graph with reduced states. The states are reduced like a DFA, but the table must be simulated like an NFA.
Edges of the GTG are regular expressions
# File lib/journey/nfa/transition_table.rb, line 52 52: def generalized_table 53: gt = GTG::TransitionTable.new 54: marked = {} 55: state_id = Hash.new { |h,k| h[k] = h.length } 56: alphabet = self.alphabet 57: 58: stack = [eclosure(0)] 59: 60: until stack.empty? 61: state = stack.pop 62: next if marked[state] || state.empty? 63: 64: marked[state] = true 65: 66: alphabet.each do |alpha| 67: next_state = eclosure(following_states(state, alpha)) 68: next if next_state.empty? 69: 70: gt[state_id[state], state_id[next_state]] = alpha 71: stack << next_state 72: end 73: end 74: 75: final_groups = state_id.keys.find_all { |s| 76: s.sort.last == accepting 77: } 78: 79: final_groups.each do |states| 80: id = state_id[states] 81: 82: gt.add_accepting id 83: save = states.find { |s| 84: @memos.key?(s) && eclosure(s).sort.last == accepting 85: } 86: 87: gt.add_memo id, memo(save) 88: end 89: 90: gt 91: end
# File lib/journey/nfa/transition_table.rb, line 30 30: def memo idx 31: @memos[idx] 32: end
# File lib/journey/nfa/transition_table.rb, line 38 38: def merge left, right 39: @memos[right] = @memos.delete left 40: @table[right] = @table.delete(left) 41: end
Returns set of NFA states to which there is a transition on ast symbol a from some state s in t.
# File lib/journey/nfa/transition_table.rb, line 103 103: def move t, a 104: Array(t).map { |s| 105: inverted[s].keys.compact.find_all { |sym| 106: sym === a 107: }.map { |sym| inverted[s][sym] } 108: }.flatten.uniq 109: end
# File lib/journey/nfa/transition_table.rb, line 143 143: def inverted 144: return @inverted if @inverted 145: 146: @inverted = Hash.new { |h,from| 147: h[from] = Hash.new { |j,s| j[s] = [] } 148: } 149: 150: @table.each { |to, hash| 151: hash.each { |from, sym| 152: if sym 153: sym = Nodes::Symbol === sym ? sym.regexp : sym.left 154: end 155: 156: @inverted[from][sym] << to 157: } 158: } 159: 160: @inverted 161: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.