initial commit

main
sejo 12 months ago
commit c25bc8bff1
  1. 2
      Makefile
  2. 0
      README.md
  3. 7
      machines/contador-alternado.txt
  4. 6
      machines/contador.txt
  5. 16
      machines/parentesis.txt
  6. 11
      machines/paridad.txt
  7. 7
      machines/test.txt
  8. 93
      turingsim.c

@ -0,0 +1,2 @@
build:
cc -std=c89 -Wall turingsim.c -o turingsim

@ -0,0 +1,7 @@
0
0000001
0
2
00110
10000
80

@ -0,0 +1,6 @@
0
0000001
0
1
00010
80

@ -0,0 +1,16 @@
a
A(()())A
1
11
a)bX1
a(a(0
aAcA1
aXaX0
b)b)1
b(aX0
bAH00
bXbX1
c(H00
cAH10
cXcX1
80

@ -0,0 +1,11 @@
a
101100B
0
6
a0a00
a1b00
aBH00
b0b00
b1a00
bBH10
80

@ -0,0 +1,7 @@
a
0000001
0
2
a0a10
a1a10
4

@ -0,0 +1,93 @@
#include <stdio.h>
#define TAPE_SIZE 80
#define RULES_SIZE 256
typedef struct {
char state; /* current state */
char symbol; /* current symbol */
int head; /* index of head */
char tape[TAPE_SIZE]; /* tape of characters */
int nrules; /* number of rules */
/* state, symbol, new state, new symbol, direction (even right, odd left ) */
char rules[RULES_SIZE][5];
} Machine;
Machine m;
int nsteps;
int main(int argc, char *argv[]){
int i,n=0;
FILE *f;
if(!(f = fopen(argv[1], "r"))){
fprintf(stderr, "failed to load file\n");
return 1;
}
printf("%s\n\n", argv[1]);
/* read machine description */
printf("initial state: ");
fscanf(f,"%c",&m.state);
printf("%c\n",m.state);
printf("initial tape: ");
fscanf(f,"%s",m.tape);
printf("%s\n",m.tape);
printf("initial position of head: ");
fscanf(f,"%d",&m.head);
m.head = (m.head+TAPE_SIZE)%TAPE_SIZE;
printf("%d\n",m.head);
printf("number of rules: ");
fscanf(f,"%d",&m.nrules);
printf("%d\n",m.nrules);
for(i=0; i<m.nrules; i++){
printf("rule number %d: ",i);
fscanf(f,"%s",m.rules[i]);
printf("%s\n",m.rules[i]);
}
printf("max number of simulation steps: ");
fscanf(f,"%d",&nsteps);
printf("%d\n\n",nsteps);
/* simulate */
int irules; /* rules index */
do{
printf("step %d\n",n);
/* print head and state */
for(i=0; i<m.head; i++)
printf(" ");
printf("%c\n",m.state);
/* print tape: */
printf("%s\n",m.tape);
/* get symbol at head position */
m.symbol = m.tape[ m.head ];
/* search for corresponding rule */
irules = -1;
for(i=0; i<m.nrules && irules<0; i++)
if(m.rules[i][0] == m.state && m.rules[i][1] == m.symbol)
irules = i;
if( irules == -1) /* halt if not found */
printf("halted\n");
else{ /* update machine otherwise */
m.state = m.rules[irules][2]; /* new state */
m.tape[ m.head ] = m.rules[irules][3]; /* new symbol */
/* move head */
if( m.rules[irules][4]%2 ) /* if odd, move to left */
m.head = (m.head-1+TAPE_SIZE)%TAPE_SIZE;
else /* if even, move to right */
m.head = (m.head+1)%TAPE_SIZE;
}
printf("\n");
}while( ++n<nsteps && irules != -1);
return 0;
}
Loading…
Cancel
Save