#!/bin/csh # Turing machine simulator (c) 1996 Carl.R.White # v1.1 , Date 22 MAY 1996 run: if ( $#argv > 2 ) then echo "Usage: `basename $0` [Turing File [Word]]" exit 1 endif if ( $#argv == 2 ) then set machine = $1 set word = $2 endif if ( $#argv == 1 ) then set machine = $1 echo -n "Enter word: " set word = $< endif if ( $#argv == 0 ) then echo -n "Enter Turing Machine name: " set machine = $< echo -n "Enter word: " set word = $< endif if 1 - ( -r $machine ) set machine = $machine.tur if 1 - ( -r $machine ) then echo "File does not exist\!" goto redo endif set t = `grep -c "start" $machine` if ( $t != "1" ) then echo 'One "start" state allowed' goto redo endif set w = ( `grep "halt" $machine` ) @ t = ( `grep -c "$w[1]" $machine` ) @ t-= ( `grep -c ",$w[1]" $machine` ) if ( $t != "1" ) then echo 'One "halt" state allowed' goto redo endif set last = $w[1] set empty = "_" set word = ( `echo $word | sed 's/./& /g'` ) @ length = $#word unset t @ state = 1 @ head = 1 loop: set todo = `grep "^$state" $machine` set todo = ( `echo " "$todo | sed 's/ '$state'//g' | sed 's/start //g' | sed 's/halt/'$empty','$empty',R,'$state'/g'` ) @ flag = 0 foreach a ( $todo ) set a = ( `echo $a | sed 's/,/ /g'` ) if ( $#a != 4 ) then echo "Incorrect data at state $state" goto redo endif if ( $a[3] != "L" && $a[3] != "R" ) then echo "Illegal direction at state $state" goto redo endif if ( $a[1] == $word[$head] ) then set word[$head] = $a[2] if ( $state < 100 ) then echo -n "0" endif if ( $state < 10 ) then echo -n "0" endif echo $state" : $word" if ( $a[3] == "L" ) @ head-- if ( $a[3] == "R" ) @ head++ @ c = ( $head + 3 ) while ($c > 1) echo -n " " @ c-- end echo "-" if ( $head < 0 ) then echo "Word failed." goto redo endif if ( $head > $length ) then set word = ( $word[1-$length] $empty ) @ length++ endif @ state = $a[4] @ flag = 1 ; goto match endif end match: if ($flag == "0" && $state != $last) then echo "Word failed." goto redo endif if ( $state != $last ) goto loop echo "Word accepted." redo: if ( $#argv < 2 ) then echo -n "`basename $0`: another run (y/n)? " set yn = $< sleep 2 if ( $yn != "y" ) then exit 0 else goto run endif else exit 0 endif