Download code
From LiteratePrograms
Back to Turing_machine_simulator_(C)
Download for Windows: zip
Download for UNIX: zip, tar.gz, tar.bz2
example_output.txt
1 $ simulate_turing_machine ab 2 v 3 ab############################################################################ 4 v 5 #b############################################################################ 6 v 7 #b############################################################################ 8 v 9 #b############################################################################ 10 v 11 ############################################################################## 12 v 13 ############################################################################## 14 v 15 ############################################################################## 16 v 17 ############################################################################## 18
simulate_turing_machine.c
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Turing_machine_simulator_(C)?action=history&offset=20070614192605 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Turing_machine_simulator_(C)?oldid=10481 25 */ 26 27 #include <stdlib.h> 28 #include <stdio.h> 29 #include <string.h> 30 31 #include <ctype.h> 32 33 #include "simulate_turing_machine.h" 34 35 36 struct turing_machine_state { 37 int control_state; 38 int head_position; 39 int tape_size; 40 symbol* tape; 41 }; 42 /* This structure need not be public */ 43 struct turing_machine_state 44 create_initial_state(int initial_control_state, int input_string_length, symbol* input_string) { 45 struct turing_machine_state state; 46 state.control_state = initial_control_state; 47 state.head_position = 0; /* Initially at left end */ 48 state.tape_size = input_string_length; 49 state.tape = malloc(sizeof(symbol)*input_string_length); 50 if (state.tape == NULL) { 51 printf("Out of memory"); 52 exit(-1); 53 } 54 55 memmove(state.tape, input_string, sizeof(symbol)*input_string_length); 56 return state; 57 } 58 59 void free_state(struct turing_machine_state* state) { 60 free(state->tape); 61 } 62 63 void update_state(struct turing_machine_state* state, int new_control_state, 64 enum direction dir, char write_symbol, char blank_symbol) 65 { 66 state->control_state = new_control_state; 67 state->tape[state->head_position] = write_symbol; 68 if (dir == DIR_LEFT && state->head_position > 0) { 69 state->head_position--; 70 } else { /* dir == DIR_RIGHT */ 71 state->head_position++; 72 } 73 if (state->head_position >= state->tape_size) { 74 int i, old_tape_size = state->tape_size; 75 symbol* new_tape = realloc(state->tape, old_tape_size*2 + 10); 76 if (new_tape == NULL) { 77 printf("Out of memory"); 78 exit(-1); 79 } 80 state->tape = new_tape; 81 state->tape_size *= 2; 82 for (i=old_tape_size; i < state->tape_size; i++) { 83 state->tape[i] = blank_symbol; 84 } 85 } 86 87 } 88 89 void trace_state(struct turing_machine_state* state, symbol blank_symbol) { 90 int i; 91 for(i=0; i < TRACE_TAPE_CHARS && i < state->head_position; i++) { 92 printf(" "); 93 } 94 if (i < TRACE_TAPE_CHARS) { 95 printf("v"); /* points down */ 96 } 97 printf("\n"); 98 for(i=0; i < TRACE_TAPE_CHARS; i++) { 99 printf("%c", i < state->tape_size ? state->tape[i] : blank_symbol); 100 } 101 printf("\n"); 102 } 103 104 105 106 int is_in_int_list(int value, int list_size, int list[]) { 107 int i; 108 for(i=0; i<list_size; i++) { 109 if (list[i] == value) { 110 return 1; 111 } 112 } 113 return 0; 114 } 115 116 void simulate(struct turing_machine machine, int input_string_length, symbol* input_string) { 117 struct turing_machine_state state = 118 create_initial_state(machine.initial_control_state, input_string_length, input_string); 119 trace_state(&state, machine.blank_symbol); 120 121 while (!is_in_int_list(state.control_state, machine.num_accepting_states, 122 machine.accepting_states)) { 123 struct transition_result next = 124 machine.transition_table[state.control_state] 125 [(int)state.tape[state.head_position]]; 126 update_state(&state, next.control_state, next.dir, 127 next.write_symbol, machine.blank_symbol); 128 trace_state(&state, machine.blank_symbol); 129 130 } 131 free_state(&state); 132 } 133 134
simulate_turing_machine.h
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Turing_machine_simulator_(C)?action=history&offset=20070614192605 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Turing_machine_simulator_(C)?oldid=10481 25 */ 26 27 #ifndef _SIMULATE_TURING_MACHINE_H_ 28 #define _SIMULATE_TURING_MACHINE_H_ 29 30 #define TAPE_ALPHABET_SIZE 256 31 32 #define TRACE_TAPE_CHARS 78 33 34 typedef char symbol; 35 36 enum direction { DIR_LEFT, DIR_RIGHT }; 37 38 struct transition_result { 39 int control_state; 40 symbol write_symbol; 41 enum direction dir; 42 }; 43 44 struct turing_machine { 45 int initial_control_state; 46 char blank_symbol; 47 int num_accepting_states; 48 int* accepting_states; 49 struct transition_result** transition_table; 50 }; 51 52 void simulate(struct turing_machine machine, int input_string_length, symbol* input_string); 53 54 55 #endif /* #ifndef _SIMULATE_TURING_MACHINE_H_ */ 56
test_driver.c
1 /* Copyright (c) 2010 the authors listed at the following URL, and/or 2 the authors of referenced articles or incorporated external code: 3 http://en.literateprograms.org/Turing_machine_simulator_(C)?action=history&offset=20070614192605 4 5 Permission is hereby granted, free of charge, to any person obtaining 6 a copy of this software and associated documentation files (the 7 "Software"), to deal in the Software without restriction, including 8 without limitation the rights to use, copy, modify, merge, publish, 9 distribute, sublicense, and/or sell copies of the Software, and to 10 permit persons to whom the Software is furnished to do so, subject to 11 the following conditions: 12 13 The above copyright notice and this permission notice shall be 14 included in all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 Retrieved from: http://en.literateprograms.org/Turing_machine_simulator_(C)?oldid=10481 25 */ 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 30 #include <string.h> 31 #include "simulate_turing_machine.h" 32 33 struct turing_machine get_example_turing_machine(void) { 34 struct turing_machine machine; 35 int i, j; 36 machine.initial_control_state = 0; 37 machine.blank_symbol = '#'; 38 machine.num_accepting_states = 1; 39 machine.accepting_states = malloc(sizeof(int)*machine.num_accepting_states); 40 if (machine.accepting_states == NULL) { 41 printf("Out of memory"); 42 exit(-1); 43 } 44 machine.accepting_states[0] = 5; 45 #define NUM_STATES 7 46 #define STATE_INVALID 6 47 48 machine.transition_table = malloc(sizeof(struct transition_result *) * NUM_STATES); 49 if (machine.transition_table == NULL) { 50 printf("Out of memory"); 51 exit(-1); 52 } 53 for (i=0; i<NUM_STATES; i++) { 54 machine.transition_table[i] = 55 malloc(sizeof(struct transition_result)*TAPE_ALPHABET_SIZE); 56 if (machine.transition_table[i] == NULL) { 57 printf("Out of memory"); 58 exit(-1); 59 } 60 for (j=0; j<TAPE_ALPHABET_SIZE; j++) { 61 machine.transition_table[i][j].control_state = STATE_INVALID; 62 machine.transition_table[i][j].write_symbol = machine.blank_symbol; 63 machine.transition_table[i][j].dir = DIR_LEFT; 64 } 65 } 66 machine.transition_table[0]['#'].control_state = 4; 67 machine.transition_table[0]['#'].write_symbol = '#'; 68 machine.transition_table[0]['#'].dir = DIR_RIGHT; 69 70 machine.transition_table[0]['a'].control_state = 1; 71 machine.transition_table[0]['a'].write_symbol = '#'; 72 machine.transition_table[0]['a'].dir = DIR_RIGHT; 73 74 machine.transition_table[4]['#'].control_state = 5; 75 machine.transition_table[4]['#'].write_symbol = '#'; 76 machine.transition_table[4]['#'].dir = DIR_RIGHT; 77 78 machine.transition_table[1]['a'].control_state = 1; 79 machine.transition_table[1]['a'].write_symbol = 'a'; 80 machine.transition_table[1]['a'].dir = DIR_RIGHT; 81 82 machine.transition_table[1]['b'].control_state = 1; 83 machine.transition_table[1]['b'].write_symbol = 'b'; 84 machine.transition_table[1]['b'].dir = DIR_RIGHT; 85 86 machine.transition_table[1]['#'].control_state = 2; 87 machine.transition_table[1]['#'].write_symbol = '#'; 88 machine.transition_table[1]['#'].dir = DIR_LEFT; 89 90 machine.transition_table[2]['b'].control_state = 3; 91 machine.transition_table[2]['b'].write_symbol = '#'; 92 machine.transition_table[2]['b'].dir = DIR_LEFT; 93 94 machine.transition_table[3]['a'].control_state = 3; 95 machine.transition_table[3]['a'].write_symbol = 'a'; 96 machine.transition_table[3]['a'].dir = DIR_LEFT; 97 98 machine.transition_table[3]['b'].control_state = 3; 99 machine.transition_table[3]['b'].write_symbol = 'b'; 100 machine.transition_table[3]['b'].dir = DIR_LEFT; 101 102 machine.transition_table[3]['#'].control_state = 0; 103 machine.transition_table[3]['#'].write_symbol = '#'; 104 machine.transition_table[3]['#'].dir = DIR_RIGHT; 105 106 107 return machine; 108 } 109 110 111 int main(int argc, char* argv[]) { 112 if (argc < 1 + 1) { 113 printf("Syntax: simulate_turing_machine <input string>\n"); 114 return -1; 115 } 116 simulate(get_example_turing_machine(), strlen(argv[1]), argv[1]); 117 return 0; 118 } 119
