char *get_token(char *lexeme , int mode){ char *token=(char*)calloc(strlen(lexeme)+50,sizeof(char)); //printf("Getting token\n"); if(is_long(lexeme)){ sprintf(token,"%d",LONG); } else if(is_static(lexeme)){ sprintf(token,"%d",STATIC); } else if(is_union(lexeme)){ sprintf(token,"%d",UNION); } else if(is_default(lexeme)){ sprintf(token,"%d",DEFAULT); } else if(is_break(lexeme)){ sprintf(token,"%d",BREAK); } else if(is_case(lexeme)){ sprintf(token,"%d",CASE); } else if(is_continue(lexeme)){ sprintf(token,"%d",CONTINUE); } else if(is_goto(lexeme)){ sprintf(token,"%d",GOTO); } else if(is_struct(lexeme)){ sprintf(token,"%d",STRUCT); } else if(is_const(lexeme)){ sprintf(token,"%d",CONST); } else if(is_void(lexeme)){ sprintf(token,"%d",VOID); } else if(is_switch(lexeme)){ sprintf(token,"%d",SWITCH); } else if(is_for(lexeme)){ sprintf(token,"%d",FOR); } else if(is_while(lexeme)){ sprintf(token,"%d",WHILE); } else if(is_do(lexeme)){ sprintf(token,"%d",DO); } else if(is_return(lexeme)){ sprintf(token,"%d",RETURN); } else if(is_bool(lexeme)){ sprintf(token,"%d",BOOL); } else if(is_char(lexeme)){ sprintf(token,"%d",CHAR); } else if(is_signed(lexeme)){ sprintf(token,"%d",SIGNED); } else if(is_unsigned(lexeme)){ sprintf(token,"%d",UNSIGNED); } else if(is_short(lexeme)){ sprintf(token,"%d",SHORT); } else if(is_int(lexeme)){ sprintf(token,"%d",INT); } else if(is_float(lexeme)){ sprintf(token,"%d",FLOAT); } else if(is_double(lexeme)){ sprintf(token,"%d",DOUBLE); } else if(is_l_square(lexeme)){ sprintf(token,"%d",L_SQUARE); } else if(is_r_square(lexeme)){ sprintf(token,"%d",R_SQUARE); } else if(is_l_paraen(lexeme)){ sprintf(token,"%d",L_PARAEN); } else if(is_r_paraen(lexeme)){ sprintf(token,"%d",R_PARAEN); } else if(is_l_cbrace(lexeme)){ sprintf(token,"%d",L_CBRACE); } else if(is_r_cbrace(lexeme)){ sprintf(token,"%d",R_CBRACE); } else if(is_comma(lexeme)){ sprintf(token,"%d",COMMA); } else if(is_semicol(lexeme)){ sprintf(token,"%d",SEMICOL); } else if(is_eq_eq(lexeme)){ sprintf(token,"%d",EQ_EQ); } else if(is_lesser(lexeme)){ sprintf(token,"%d",LESSER); } else if(is_less_eq(lexeme)){ sprintf(token,"%d",LESS_EQ); } else if(is_div(lexeme)){ sprintf(token,"%d",DIV); } else if(is_greater(lexeme)){ sprintf(token,"%d",GREATER); } else if(is_great_eq(lexeme)){ sprintf(token,"%d",GREAT_EQ); } else if(is_plus_eq(lexeme)){ sprintf(token,"%d",PLUS_EQ); } else if(is_minus_eq(lexeme)){ sprintf(token,"%d",MINUS_EQ); } else if(is_div_eq(lexeme)){ sprintf(token,"%d",DIV_EQ); } else if(is_mult_eq(lexeme)){ sprintf(token,"%d",MULT_EQ); } else if(is_minus_minus(lexeme)){ sprintf(token,"%d",MINUS_MINUS); } else if(is_plus_plus(lexeme)){ sprintf(token,"%d",PLUS_PLUS); } else if(is_percent(lexeme)){ sprintf(token,"%d",PERCENT); } else if(is_div(lexeme)){ sprintf(token,"%d",DIV); } else if(is_mult(lexeme)){ sprintf(token,"%d",MULT); } else if(is_minus(lexeme)){ sprintf(token,"%d",MINUS); } else if(is_plus(lexeme)){ sprintf(token,"%d",PLUS); } else if(is_int_const(lexeme)){ printf("int"); sprintf(token,"%d\t%s",INT_CONST,lexeme); } else if(is_flo_const(lexeme)){ printf("float"); sprintf(token,"%d\t%s",FLO_CONST,lexeme); } else if(is_comment_start(lexeme)){ sprintf(token,"$start"); } else if(is_comment_end(lexeme)){ sprintf(token,"$end"); } else if(is_identifier(lexeme)){ printf("Identifier"); if(mode==1) ht_set( symboltable, lexeme, "1"); sprintf(token,"%d\t%s",IDNTIFIER,lexeme); } else sprintf(token,"%d",NOTOK); return token; }
/******************************************************************************\ Read a token out of a token file. A token is either a series of characters unbroken by spaces or comments or a single or double-quote enclosed string. The kind of enclosed string (or zero) is returned via [quoted]. Enclosed strings are parsed for backslash symbols. Token files support Bash, C, and C++ style comments. Always returns a non-NULL string. A zero-length string indicates end of file. FIXME: Symbols in identifiers should be read as individual tokens. \******************************************************************************/ const char *C_token_file_read_full(c_token_file_t *tf, int *quoted) { int parsing_comment, parsing_string; if (!tf->pos || tf->pos < tf->buffer || tf->pos >= tf->buffer + sizeof (tf->buffer)) C_error("Invalid token file"); *tf->pos = tf->swap; /* Skip space */ parsing_comment = FALSE; while (C_is_space(*tf->pos) || parsing_comment || is_comment(tf->pos)) { if (!parsing_comment) parsing_comment = is_comment(tf->pos); else if (is_comment_end(tf->pos, parsing_comment)) parsing_comment = 0; token_file_check_chunk(tf); tf->token = ++tf->pos; } if (!*tf->pos) { if (quoted) *quoted = FALSE; return ""; } /* Read token */ parsing_string = FALSE; if (*tf->pos == '"' || *tf->pos == '\'') { parsing_string = *tf->pos; token_file_check_chunk(tf); tf->token = ++tf->pos; } while (*tf->pos) { if (parsing_string) { if (*tf->pos == parsing_string) { token_file_check_chunk(tf); *(tf->pos++) = NUL; break; } else if (parsing_string == '"' && *tf->pos == '\\') { token_file_check_chunk(tf); memmove(tf->pos, tf->pos + 1, tf->buffer + sizeof (tf->buffer) - tf->pos - 1); if (*tf->pos == 'n') *tf->pos = '\n'; else if (*tf->pos == 'r') *tf->pos = '\r'; else if (*tf->pos == 't') *tf->pos = '\t'; } } else if (C_is_space(*tf->pos) || is_comment(tf->pos)) break; token_file_check_chunk(tf); tf->pos++; } tf->swap = *tf->pos; *tf->pos = NUL; if (quoted) *quoted = parsing_string; return tf->token; }