How to be a programmer: Part 1

 Implementing a parsing algorithm:

- You will need to look at state machines. You will need to think about state machines a lot. Your program is basically a state machine glued to a table.

- Basically, a state machine is a computer that knows something about the past.

For example:

for(int i = 0; i < n; i++){
    table[i];
}

Does not know about (i-1). So we can add it like this:

data_prev[n];
for(int i = 0; i < n; i++){
    (check data_prev[i]);
    table[i];
    data_prev[i+1]=set(with i);
}

However, keep in mind that you do not need the data after you are done with it.

data_prev;
for(int i = 0; i < n; i++){
    (check data_prev, which was set with i - 1);
    table[i];
    data_prev = set(with i);
}

You can also implement state machines with any kind of loop, while, iterator. Since there is only one version of it running at a time, it is a kind of state machine.

data_prev;
while(temp->next != NULL){
    data_prev = set(with temp);
    temp = temp->next;
}

The set() function basically is there to tell the reader, "you do something with this data that you need". For example, to implement a state machine to check for spaces, you would do:

int wordcount = 0;
int data_prev = 0;
for(int i = 0; i < n; i++){
    if(buffer[i] is a space){
        if(data_prev == 1){
            // don't create a word
        } else {
            table[wordcount] = word;
            wordcount++;
        }
    }
    data_prev = buffer[i] is a space;
}

The intent is that you don't want to create words that are 0 length long. So here, data_prev is basically because you want to create a sandwich between the last space and the next space, and you don't want the space sandwich to be empty inside because that will create an empty word.

Comments

Post a Comment

Popular Posts