#include #include /* * A Turing machine simulator * * ProgramName: turing.c * Author: Shinichi Asakawa */ /* IQ : inner state IA : index card OA : action OQ : change state */ int pretape = 10; #define maxtapelen 70 #define MAXRULE 100 int IQ[MAXRULE], OQ[MAXRULE]; char IA[MAXRULE], OA[MAXRULE]; char tape[maxtapelen]; int nrule = 0; int state = 1; int head = 0; int step = 1; void usage(void){ fprintf(stderr, "Turing machine simulator\n"); fprintf(stderr, "Usage: Turing \n"); exit (EXIT_FAILURE); } char read_tape(int pos) { if (tape[pretape+pos] == '0') return 'B'; else return tape[pretape+pos]; } void set_tape(int n, char str) { tape[pretape+n] = str; } void check_tapelen(int pos) { if (pos + pretape > maxtapelen){ fprintf(stderr, "Tap over flow\n"); exit (EXIT_FAILURE); } } void print_head(void) { int i, pos; pos = 6 + pretape + head; for (i=0;i (maxtapelen-pretape)){ fprintf(stderr, "Tape too long\n"); exit (EXIT_FAILURE); } for (i=0; i= 0) { printf(" Rule %d(%d %c %c %d) matched.\n", rule, IQ[rule], IA[rule], OA[rule], OQ[rule]); if (OA[rule] == 'R') head++; else if (OA[rule] == 'L') head--; else set_tape(head, OA[rule]); state = OQ[rule]; check_tapelen(head); } else { printf(" HALT: No rule matched.\n"); printf("Result = %d\n", calc_sum()); exit (EXIT_FAILURE); } step++; } }