CSTheory

HomePage | RecentChanges | Preferences

CSE 355 :: Introduction to Theoretical ComputerScience

A Ruby program to convert our Turing machine syntax: 0 B/B R 1 into the syntax for this simulator: 1,_,2,_,>

# Wendy Smoak
# 2006-11-26
# For CSE 355 Chapter 8 on Turing Machines

# Converts Turing machine instructions int the form 
# 0 B/B R 1 to the form 1,_,2,_,> 
# for use with http://ironphoenix.org/tril/tm/
# input: initialState read/write move newState
# output: initialState,read,newState,write,move
# Blanks are B in the input, _ in the output
# state numbers are increased by one in the output
# move is L or R in the input, < or > in the output
# input file must not have a blank line at the end

class Converter 

  def Converter.convert( theLine)

     # split on space and slash
     myArray = theLine.split %r{[ /]}  # or .split(/[ \/]/)
     
     newArray = []
    
     newArray[0] = myArray[0].to_i + 1
     
     newArray[1] = myArray[1].sub( 'B','_' )
     
     newArray[2] = myArray[4].to_i + 1
     
     newArray[3] = myArray[2].sub( 'B','_' )
     
     newArray[4] = myArray[3].sub( 'L', '<' ).sub( 'R', '>' )
     
     puts newArray.join(",")

  end

end

aFile = File.new("input.txt", "r")

aFile.each_line {|line| Converter.convert(line) }

aFile.close


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited October 15, 2014 7:26 pm by WendySmoak (diff)
Search: