Exemple #1
0
/* Creer un automate a partir d'un fichier texte
 * Valeur de retour : Automate cree
 */
bool parse(FILE *data, struct fsm *fsm) {
    char *param, *tmp;
    char type[TYPE_SIZE], s[LINE_SIZE];
    bool alphabet = false;
    struct state *st, *st2;
    struct transition *tr;
    int i = 0, j;
    char **trans = NULL;

    fsm->alphabet = NULL;
    fsm->initials = NULL;
    fsm->states = NULL;
    fsm->ilen = fsm->alen = fsm->slen = 0;

    while(EOF != fscanf(data, "%18s%255[^\n]", type, s)) { /* TYPE_SIZE et LINE_SIZE */
        param = s;
        while((*param == ' ' || *param == '\t') && *param != '\0') {
            param++;
        }
        if(!(*param)) {
        	fprintf(stderr, "parse: `%s %s': Invalid format.\n", type, s);
            return false;
        }

        if(!strcmp(type, "alphabet_terminal")) {
            alphabet = true;
            tmp = strtok(param, " ");
            while(tmp != NULL) {
                if(!addAlphabet(fsm, tmp))
                    return false;

                tmp = strtok(NULL, " ");
            }
        }

        else if(!strcmp(type, "initial")) {
            tmp = strtok(param, " ");
            while(tmp != NULL) {
                if(!addInitial(fsm, tmp))
                    return false;

                tmp = strtok(NULL, " ");
            }
        }

        else if(!strcmp(type, "terminal")) {
            tmp = strtok(param, " ");

            while(tmp != NULL) {
                if(!addState(fsm, tmp, true))
                    return false;

                tmp = strtok(NULL, " ");
            }
        }

        else {
            i = 0;
            trans = NULL;

            tmp = strtok(param, " ");
            if(((trans = realloc(trans, (i+1)*sizeof(char *))) == NULL) ||
                ((trans[i] = malloc((strlen(tmp)+1)*sizeof(char))) == NULL)) {
					fprintf(stderr, "parse: Memory allocation failed.\n");
					return false;
			}
            strcpy(trans[i++], tmp);

            if((tmp = strtok(NULL, " ")) == NULL) {
            	fprintf(stderr, "parse: `%s %s': Invalid format; destination stateis not specified.\n", type, s);
                return false;
            }

            do {
                if(((trans = realloc(trans, (i+1)*sizeof(char *))) == NULL) ||
                    ((trans[i] = malloc((strlen(tmp)+1)*sizeof(char))) == NULL)) {
						fprintf(stderr, "parse: Memory allocation failed.\n");
						return false;
				}
                strcpy(trans[i++], tmp);
            } while((tmp = strtok(NULL, " ")) != NULL);

            if(((st = addState(fsm, type, false)) == NULL) ||
                ((st2 = addState(fsm, trans[i-1], false)) == NULL)) {
                    return false;
            }

            for(j = 0; j < i-1; j++) {
                if(alphabet && (!containsLetter(fsm, trans[j])) && strcmp(trans[j], EPSILON)) {
                    fprintf(stderr, "parse: `%s' does not belong to the alphabet.\n", trans[j]);
                    for(j = 0; j < i; j++)
                        free(trans[j]);
                    free(trans);
                    return false;
                }
                else if(!alphabet && strcmp(trans[j], EPSILON)) {
                    if(!addAlphabet(fsm, trans[j]))
                        return false;
                }
                if(((tr = createTransition(trans[j], st2)) == NULL) ||
                    (!addTransition(st, tr)))
                        return false;
            }

            for(j = 0; j < i; j++)
                free(trans[j]);
            free(trans);
        }
    }

    return true;
}
Exemple #2
0
/**
 * The goOn function from Gordon's paper.
 * @param pos    The position relative to the current anchor square.
 * @param L      A letter from 'A' to 'Z'.
 * @param word   A possible word being built.
 * @param rack   An integer describing the rack.
 * @param NewArc The next arc based on the L parameter.
 * @param OldArc The old arc.
 */
void go_on(int pos, char L, char* word, uint8_t* rack, ARC* NewArc,
    ARC* OldArc, int spaces) {
    #ifdef DEBUG_
    print_spaces(spaces);
    printf("in go_on, %d, Letter: %c, Word: %s,", pos, L, word);
    print_rack(rack);
    #endif
    char word_c[16];
    char word_copy[16];
    word_c[0] = L;
    word_c[1] = '\0';
    if (pos <= 0) {
        // word <- L || word
        #ifdef DEBUG_
        print_spaces(spaces);
        printf("pos <= 0, here\n");
        #endif
        strcat(word_c, word);
        // word_c now prepends word with L. Can I do this faster?
        // IF L on oldArc & no letter directly left then RecordPlay
        if (containsLetter(OldArc->destination, L)) {
            // TODO - check if no letter directly left, this is also a
            // required condition.
            #ifdef DEBUG_
            print_spaces(spaces);
            printf("containsLetter(OldArc->destination, %c)\n", L);
            #endif
            record_play(word_c);
        }
        if (NewArc) {
            #ifdef DEBUG_
            print_spaces(spaces);
            printf("New arc\n");
            #endif
            // If there is room to the left, Gen(pos-1, word, rack, NewArc)
            // TODO, check if there is room to the left.
            if (1) {
                gen(pos - 1, word_c, rack, NewArc, spaces + 1);
            }
            NewArc = next_arc(NewArc, SEPARATION_TOKEN);
            // Now shift direction.
            if (NewArc) {
                #ifdef DEBUG_
                print_spaces(spaces);
                printf("Switched direction\n");
                #endif
                // TODO check if there is no letter directly left, AND
                // there is room to the right.
                if (1) {
                    gen(1, word_c, rack, NewArc, spaces + 1);
                }
            }
        }
    } else if (pos > 0) {
        #ifdef DEBUG_
        print_spaces(spaces);
        printf("pos > 0\n");
        #endif
        strcpy(word_copy, word);
        strcat(word_copy, word_c);
        if (containsLetter(OldArc->destination, L)) {
            #ifdef DEBUG_
            print_spaces(spaces);
            printf("containsLetter(OldArc->destination, %c)\n", L);
            #endif
            // TODO check if no letter directly right.
            if (1) {
                record_play(word_copy);
            }
        }
        if (NewArc) {
            #ifdef DEBUG_
            print_spaces(spaces);
            printf("NewArc\n");
            #endif
            // TODO check if room to the right.
            if (1) {
                gen(pos + 1, word_copy, rack, NewArc, spaces + 1);
            }
        }
    }
}