The header pointer basically points to a location on the tape, therefore it is better to keep it as an integer variable pointing to the
base location at the beginning (initialized to 0).
static int HEADER;
// Position of the header pointer
Now, we need to fill the tape with the input symbols ending with the blank symbol. We expand the one-dimensional tape and put the
character “$” (representing “b”) at the end.
TAPE = (char *)calloc(INPUT _ size, 2);
for(i=0;i<INPUT _ size;i++)
TAPE[i] = INPUT[i];
TAPE[i] = ‘$’;
We also initialize the state (initial state ) and the symbol (first input symbol on the tape) with which the header pointer starts
the simulation. These can be global variables.
STATE = 0, SYMBOL = TAPE[0];
Now we write a separate function TRANSITION_rule() for simulating the transition rules depending upon the current symbol and
state. To provide the controlling instructions to the model, the transition function is written in a tab delimited file “Transition_
Function.txt” following the schema <Current State, Read Symbol, New State, Write Symbol, Direction>. The Direction values can be
either “L” or “R” denoting left and right shift of the header pointer, respectively. Based on the transition rule, the machine refixes
itself as follows.
fp = fopen(“Transition _Function.txt”, ”r”);
while(i=0;i<ENTRY _ no;i++){
TAPE[STATE] = W _ symbol;
HEADER _ shift(DIR);
STATE = NEW _ state;
SYMBOL = W _ symbol;
break;
}
}
As an example, for the TM shown in Figure 1, the entries in “Transition_Function.txt” will be as follows:
0 x
1 x
2 x
2 Y
2 y
3 Y
3 x
3 X
4 x
4 X
5 Y
5 b
1
2
2
2
3
3
4
5
4
1
5
6
X
X
x
Y
Y
Y
x
X
x
X
Y
b
R
R
R
R
L
L
L
R
L
R
R
R
There may be two problems in shifting the header pointer: underflow or overflow that can be overcome by shifting the entire array
or reallocating ne w spaces, respectively. Other wise, it is simply a shift operation to the left or right. The code for the HEADER_
shift() function can be written as the following: