/* Turing Machine Simulation. RJ Botting April 93 */ /* A Turing Machine is a good theoretical model of what a computer can do if there were no limits to its storage capacity. If a Turing Machine can't be programmed to do it, then it is certain that no normal computer can be programmed to handle it. From now on "TM" is an abreviation for "Turing Machine". */ #include #include #include typedef char * string; /* A TM has two parts - a finite processor and a unlimitted tape that is used for input, storage and output. We define each in turn: First the tape and then the processor(CPU). */ /* The tape is a large number of identical "cells" and each cell has a value in a finite set of values - including a special blank value. In theory the set of values can be any finite set - like { 0,1, blank} or a record. I model each cell as a character since this is easy to print. */ typedef char CELL; #define BLANK ' ' #define TAPE_LENGTH 70 CELL tape[TAPE_LENGTH]; /* In theory TAPE_LENGTH is infinite. This is a finite approximation therefore */ /* Stub function to request more tape if needed */ void out_of_tape(void) { fprintf(stderr, "Simulation ran out of tape\n"); exit(1); } /* The CPU is limitted to accessing one cell in the tape at a time - this is thought as being the cell underneath a read/write head. I choose to always start the simulation at the middle of the tape */ int head = TAPE_LENGTH / 2; /* The CPU is limited to four operations READ returns the value of the cell underneath the head. WRITE Change the value in the cell underneath the head. LEFT move the head to the cell on the left of the current. RIGHT move the head to the cell on the right of the current. */ #define READ(value) {if( 0<=head && head "); for (i = 0; i <= TAPE_LENGTH-1; i++) printf("%c", tape[i]); printf("\n"); printf("State= %*s%c\n", head+1, "", state); } void load( int argc, string argv[]); /* loads up the state table and tape */ void initialise(void); /* Puts blanks on tape and sets table to Halts*/ int askfor(string prompt);/* prompt user for a character */ main( int argc, string argv[]) { initialise(); load(argc, argv); do display(); while ( cycle() ); display(); exit(0); } void load( int argc, string argv[]) { int i, n; int file; FILE * fp; string fname="default_Turing_Machine.tm"; printf("Turing Machine Simulation\n"); printf(" Initial State\n"); printf("Input the head\'s position (between 0 and %d): ",TAPE_LENGTH-1); fflush(stdout); scanf("%d", &head); printf("Content of tape(a string with no spaces): "); fflush(stdout); scanf("%s", tape+head); if(argc==2){ fp=fopen(argv[1], "r"); if(fp==NULL){ fprintf(stderr,"Can not open %s\n", argv[1]); exit(1); } i=0; while( (fscanf(fp,"%s",table[i]))==1)i++; n=i-1; } else{ do file=askfor("Is the program on file (Y/N)? "); while(file!='Y' && file!='y' && file!='N' && file!='n'); if(toupper(file)=='Y') { do{ printf("File name? ");fflush(stdout);scanf("%s",fname); }while((fp=fopen(fname, "r"))==NULL); i=0; while( (fscanf(fp,"%s",table[i]))==1)i++; n=i-1; } else{ printf("Input up to %d rows for the Turing Machine Table.\n",NROWS+1); printf("The initial state is 0, and the Halting state is H\n"); for(i=0; i