Parent

Included Modules

Journey::NFA::TransitionTable

Attributes

accepting[RW]
memos[R]

Public Class Methods

new() click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 11
11:       def initialize
12:         @table     = Hash.new { |h,f| h[f] = {} }
13:         @memos     = {}
14:         @accepting = nil
15:         @inverted  = nil
16:       end

Public Instance Methods

[]=(i, f, s) click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 34
34:       def []= i, f, s
35:         @table[f][i] = s
36:       end
accepting?(state) click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 18
18:       def accepting? state
19:         accepting == state
20:       end
accepting_states() click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 22
22:       def accepting_states
23:         [accepting]
24:       end
add_memo(idx, memo) click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 26
26:       def add_memo idx, memo
27:         @memos[idx] = memo
28:       end
alphabet() click to toggle source
     # 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
eclosure(t) click to toggle source
 

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
following_states(t, a) click to toggle source
 

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
generalized_table() click to toggle source
 

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
memo(idx) click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 30
30:       def memo idx
31:         @memos[idx]
32:       end
merge(left, right) click to toggle source
    # 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
move(t, a) click to toggle source
 

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
states() click to toggle source
    # File lib/journey/nfa/transition_table.rb, line 43
43:       def states
44:         (@table.keys + @table.values.map(&:keys).flatten).uniq
45:       end
transitions() click to toggle source
     # File lib/journey/nfa/transition_table.rb, line 136
136:       def transitions
137:         @table.map { |to, hash|
138:           hash.map { |from, sym| [from, sym, to] }
139:         }.flatten(1)
140:       end

Private Instance Methods

inverted() click to toggle source
     # 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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.