// load automata for scanning valid identifiers void Scanner::load_id_machine() { // create the id scan automata auto id_machine = FSMachinePtr( new FiniteMachineContainer(get_token_info(TokType::MP_ID).first, true)); // detects ids without two underscores id_machine->add_state("0", true, false); id_machine->add_state("1", false, true); id_machine->add_state("2", false, true); id_machine->add_state("3", false, false); id_machine->add_state("4", false, false); // add transitions id_machine->add_alphabet("0", "1"); id_machine->add_alphabet("1", "1"); id_machine->add_digits("1", "1"); id_machine->add_transition("1", '_', "4"); id_machine->add_alphabet("4", "1"); id_machine->add_digits("4", "1"); // other part id_machine->add_transition("0", '_', "3"); id_machine->add_alphabet("3", "2"); id_machine->add_digits("3", "2"); id_machine->add_alphabet("2", "2"); id_machine->add_digits("2", "2"); id_machine->add_transition("2", '_', "3"); // set priority id_machine->set_priority(2); // add to finite machines this->fsmachines->push_back(FSMachinePtr(id_machine)); }
void Scanner::load_strand_machines(unsigned int within) { // comments and strings, take that! // add nested strings and comments feature using within variable auto comment_machine = FSMachinePtr( new FiniteMachineContainer(get_token_info(TokType::MP_COMMENT).first, true)); // add states comment_machine->add_state("0", true, false); comment_machine->add_state("1", false, false); comment_machine->add_state("2", false, true); // for state 1 comment_machine->add_alphabet("1", "1"); comment_machine->add_symbols("1", "1"); comment_machine->add_digits("1", "1"); comment_machine->add_transition("1", ' ', "1"); comment_machine->add_transition("1", '\n', "1"); comment_machine->add_transition("1", '\t', "1"); comment_machine->add_transition("1", '\v', "1"); comment_machine->add_transition("1", '\r', "1"); comment_machine->add_transition("1", '\f', "1"); comment_machine->remove_transition("1", '{'); comment_machine->remove_transition("1", '}'); comment_machine->add_transition("0", '{', "1"); comment_machine->add_transition("1", '}', "2"); // new machine auto string_machine = FSMachinePtr( new FiniteMachineContainer(get_token_info(TokType::MP_STRING_LITERAL).first, true)); // add states string_machine->add_state("0", true, false); string_machine->add_state("1", false, false); string_machine->add_state("2", false, true); // add transitions string_machine->add_transition("0", '\'', "1"); string_machine->add_alphabet("1", "1"); string_machine->add_symbols("1", "1"); string_machine->add_digits("1", "1"); string_machine->add_transition("1", ' ', "1"); string_machine->remove_transition("1", '\''); string_machine->remove_transition("1", '\''); string_machine->add_transition("1", '\'', "2"); // set machine priority comment_machine->set_priority(1); string_machine->set_priority(1); // add the machines to our FSMachines list this->fsmachines->push_back(comment_machine); this->fsmachines->push_back(string_machine); }
int map_sequences(STRING *s1, STRING *s2, STRING **sary, int arylen) { int i, j, num, same, alpha; char *s, *t, map[128], characters[129]; ALPHABET *tptr; /* * Check the simple case where all have the same alphabet. */ same = 1; alpha = ALPHA_UNKNOWN; if (s1 != NULL) alpha = s1->raw_alpha; if (s2 != NULL) { if (alpha == ALPHA_UNKNOWN) alpha = s2->raw_alpha; else if (alpha != s2->raw_alpha) same = 0; } if (sary != NULL) { for (i=0; i < arylen; i++) { if (sary[i] != NULL) { if (alpha == ALPHA_UNKNOWN) alpha = sary[i]->raw_alpha; else if (alpha != sary[i]->raw_alpha) same = 0; } } } /* * If not, print a warning message and create a new alphabet formed * by the combination of all the characters in the alphabets. */ if (!same) { fprintf(stderr, "Warning: Alphabets are not identical...\n"); for (i=0; i < 128; i++) map[i] = '\0'; if (s1 != NULL) { tptr = &alpha_tables[s1->raw_alpha]; for (i=0; i < 128; i++) if (tptr->rawmap[i] != '\0') map[(int) tptr->rawmap[i]] = 1; } if (s2 != NULL) { tptr = &alpha_tables[s2->raw_alpha]; for (i=0; i < 128; i++) if (tptr->rawmap[i] != '\0') map[(int) tptr->rawmap[i]] = 1; } if (sary != NULL) { for (num=0; num < arylen; num++) { if (sary[num] != NULL) { tptr = &alpha_tables[sary[num]->raw_alpha]; for (i=0; i < 128; i++) if (tptr->rawmap[i] != '\0') map[(int) tptr->rawmap[i]] = 1; } } } for (i=1,j=0; i < 128; i++) if (map[i] == 1) characters[j++] = (char) i; characters[j] = '\0'; if ((alpha = add_alphabet(characters)) == -1) return -1; } /* * Map the sequences to the new alphabet. */ if (s1 != NULL && (!same || s1->alphabet != alpha)) { s1->alphabet = alpha; s1->alpha_size = alpha_tables[alpha].alpha_size; for (i=0,s=s1->sequence,t=s1->raw_seq; i < s1->length; i++,s++,t++) if ((*s = mapchar(alpha, *t)) == -1) { fprintf(stderr, "\nError: Sequence %s contains unmappable characters.\n", s1->title); return -1; } } if (s2 != NULL && (!same || s2->alphabet != alpha)) { s2->alphabet = alpha; s2->alpha_size = alpha_tables[alpha].alpha_size; for (i=0,s=s2->sequence,t=s2->raw_seq; i < s2->length; i++,s++,t++) if ((*s = mapchar(alpha, *t)) == -1) { fprintf(stderr, "\nError: Sequence %s contains unmappable characters.\n", s2->title); return -1; } } if (sary != NULL) { for (num=0; num < arylen; num++) { if (sary[num] != NULL && (!same || sary[num]->alphabet != alpha)) { sary[num]->alphabet = alpha; sary[num]->alpha_size = alpha_tables[alpha].alpha_size; s = sary[num]->sequence; for (i=0,t=sary[num]->raw_seq; i < sary[num]->length; i++) if ((*s++ = mapchar(alpha, *t++)) == -1) { fprintf(stderr, "\nError: Sequence %s contains unmappable characters.\n", sary[num]->title); return -1; } } } } return alpha; }