Skip to content

Instantly share code, notes, and snippets.

@cflewis
Created August 7, 2010 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cflewis/512263 to your computer and use it in GitHub Desktop.
Save cflewis/512263 to your computer and use it in GitHub Desktop.
#!/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
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
return_string = ""
last_line = nil
@states.each do |state|
if not last_line.nil? then
last_line.each do |column, value|
if value != state[column] && column != "step" then
return_string += "Step #{state["step"]}: " \
+ "#{column} - #{value} => #{state[column]}\n"
end
end
end
last_line = state
end
return return_string
end
end
parser = PrismMachinationsParser.new("prism_output.txt", \
Regexp.new(".*tokens$"))
puts parser.to_s_transitions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment