#!/usr/local/bin/perl # -*- perl -*- $file = $ARGV[0]; $input = $ARGV[1]; # input Turing machine open(TM, $file); while ($line = ) { ($st, $sym, $st0, $sym0, $dir) = split(/\s+/,$line); $nextst{$st,$sym} = $st0; $nextsym{$st,$sym} = $sym0; $nextdir{$st,$sym} = $dir; } # initialize Turing machine $tape[0] = "_"; $tape[1] = "_"; foreach $i (0 .. length($input)-1) { $tape[$i+2] = substr($input,$i,1); } push(@tape,"_"); push(@tape,"_"); $st = "q0"; $pos = 2; $sym = $tape[2]; $offset = 8; while (1) { # print configuration # print the state foreach $i (1 .. $offset + $pos) {print " ";} print "$st\n"; foreach $i (1 .. $offset + $pos) {print " ";} print "|\n"; foreach $i (1 .. $offset) {print " ";} foreach $i (0 .. $#tape) {print $tape[$i];} print "\n"; # wait for user input if () { }; # perform transition ($st, $sym, $dir) = ($nextst{$st,$sym}, $nextsym{$st,$sym}, $nextdir{$st,$sym}); last unless defined($dir); $tape[$pos] = $sym; if ($dir eq "L" || $dir eq "l") { $pos--; if ($pos < 0) { $pos = 0; $offset--; unshift(@tape,"_"); } } elsif ($dir eq "R" || $dir eq "r") { $pos++; if ($pos > $#tape) {push(@tape,"_");} } $sym = $tape[$pos]; }