static void initialize_state_id_to_token_map_for_dfa(Lexer *lexer, const char *id_to_token_type_path) {
FILE *fp = fopen(id_to_token_type_path, "r");
if (!fp) { report_file_error(id_to_token_type_path); }
HashMap *string_to_token_map = lexer->string_to_token_type_map;
int BUF_SIZE = MAX_TOKEN_SIZE;
char buffer[BUF_SIZE];
DFA *dfa = lexer->dfa;
for (int i = 0; i < dfa->N_STATES; ++i) {
fgets(buffer, BUF_SIZE, fp);
buffer[strcspn(buffer, "\r\n")] = 0;
// THIS LINE: &buffer does not work but changing buffer to dynamic memory // and then doing &buffer works
int *token = get_from_hashmap(string_to_token_map, &buffer);
if (token == NULL) {
fprintf(stderr, "Invalid token type %s\n", buffer);
exit(1);
}
dfa->states[i].token_associated = *token;
}
}
This is the function causing the error;
// hash function will receive pointer to string aka pointer to char*
size_t hash_string_djb2(const void *key) {
char **str_ptr = (char **) key;
const char *str = *str_ptr;
size_t hash = 5381;
int c;
while (true) {
c = (unsigned char) *str++;
if (c == 0) return hash;
hash = ((hash << 5) + hash) + c;
}
}