Class: Aurum::Automata

Inherits:
Object
  • Object
show all
Defined in:
lib/aurum/lexical_table_generator.rb

Defined Under Namespace

Classes: Transition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table = []) ⇒ Automata

Returns a new instance of Automata.



185
186
187
188
189
190
191
192
193
# File 'lib/aurum/lexical_table_generator.rb', line 185

def initialize(table=[])
    case table
    when Array
        @table = table
    when Fixnum
        @table = []
        table.times {@table << []}
    end
end

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



182
183
184
# File 'lib/aurum/lexical_table_generator.rb', line 182

def table
  @table
end

Instance Method Details

#all_statesObject



254
255
256
# File 'lib/aurum/lexical_table_generator.rb', line 254

def all_states
    (0..table.length - 1).to_a
end

#alphabet(states) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/aurum/lexical_table_generator.rb', line 225

def alphabet states
    points = states.inject([]) do |result, state|
        @table[state].inject(result){|r, s|r += s.symbols.to_points s.destination}
    end
    points.sort! do |x, y|
        x.char == y.char ? (x.is_start ? (y.is_start ? 0 : -1) : (y.is_start ? 1 : 0)) : (x.char < y.char ? -1 : 1)
    end
    reachable_states = []
    points.each_with_index do |point, index|
        if point.is_start
            reachable_states << point.destination
        else
            reachable_states.delete point.destination
            next if reachable_states.empty?
        end
        symbols = range(point, points[index + 1])
        yield reachable_states.uniq, symbols if symbols
    end
end

#connect(start, symbols, destination) ⇒ Object



195
196
197
198
# File 'lib/aurum/lexical_table_generator.rb', line 195

def connect start, symbols, destination
    @table[start]  << Transition.new(symbols, destination)
    destination
end

#determinize(accepts) ⇒ Object



245
246
247
# File 'lib/aurum/lexical_table_generator.rb', line 245

def determinize accepts
    SubsetDeterminizer.new(self, accepts).determinize
end

#dupObject



219
220
221
222
223
# File 'lib/aurum/lexical_table_generator.rb', line 219

def dup
    dup_table = []
    @table.each {|x| dup_table << x.dup}
    Automata.new dup_table
end

#merge!(other) ⇒ Object



200
201
202
203
204
205
206
207
208
# File 'lib/aurum/lexical_table_generator.rb', line 200

def merge! other
    start = @table.length
    other_table = other.instance_eval{@table}
    other_table.each do |trans|
        @table << []
        trans.each {|tran| @table.last << Transition.new(tran.symbols, tran.destination + start)}
    end
    start
end

#new_stateObject



249
250
251
252
# File 'lib/aurum/lexical_table_generator.rb', line 249

def new_state
    @table << []
    @table.length - 1
end

#reverseObject



210
211
212
213
214
215
216
217
# File 'lib/aurum/lexical_table_generator.rb', line 210

def reverse
    reverse = []
    @table.length.times {reverse << []}
    @table.each_with_index do |trans, index|
        trans.each {|tran| reverse[tran.destination] << Transition.new(tran.symbols, index)}
    end
    Automata.new reverse
end