Created
August 7, 2010 00:19
-
-
Save cflewis/512253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class PrismMachinationsParser | |
attr_reader :header_line, :state_lines | |
def initialize(file_name, token_regex) | |
@prism_file = File.open(file_name, "r") | |
@header_line = @prism_file.gets.split(" ") | |
@array_length = header_line.length | |
@states = self.parse | |
@range_columns = self.get_range_columns(header_line, token_regex) | |
end | |
def get_range_columns(header_line, token_regex) | |
start_column = -1 | |
end_column = -1 | |
for i in (0..header_line.length - 1) do | |
if token_regex.match(header_line[i]) then | |
start_column = i if start_column < 0 | |
end_column = i | |
end | |
end | |
puts "#{start_column} #{end_column}" | |
return (start_column..end_column) | |
end | |
def parse | |
states = [] | |
@prism_file.each_line do |line| | |
state_line = line.split(" ") | |
states << Hash[@header_line.zip(state_line)] | |
end | |
return states | |
end | |
def to_s | |
return @states.to_s | |
end | |
def to_s_transitions | |
last_line = Array.new(@array_length, "") | |
@prism_file.each_line do |line| | |
state_line = line.split(" ") | |
if not last_line.nil? then | |
for i in @range_columns do | |
if last_line[i] != state_line[i] then | |
puts "Step #{state_line[0]}: " + \ | |
"#{@header_line[i]} - #{last_line[i]} => " + \ | |
"#{state_line[i]}" | |
end | |
end | |
last_line = state_line | |
end | |
end | |
end | |
end | |
parser = PrismMachinationsParser.new("prism_output.txt", \ | |
Regexp.new(".*tokens$")) | |
puts parser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment