Untitled
unknown
c_cpp
2 years ago
2.4 kB
3
Indexable
TokenSet getToken(void) { int i = 0; char c = '\0'; while ((c = fgetc(stdin)) == ' ' || c == '\t'); //printf("%c\n", c); if (isdigit(c)) { lexeme[0] = c; c = fgetc(stdin); i = 1; while (isdigit(c) && i < MAXLEN) { lexeme[i] = c; ++i; c = fgetc(stdin); } ungetc(c, stdin); lexeme[i] = '\0'; return INT; } else if (c == '+' || c == '-') { int cnt = 0; lexeme[0] = c, lexeme[1] = '\0'; c = fgetc(stdin); if(c == lexeme[0]){ lexeme[1] = c, lexeme[2] = '\0'; //ungetc(c, stdin); return INCDEC; } if(c == '='){ lexeme[1] = '=', lexeme[2] = '\0'; //ungetc(c, stdin); return ADDSUB_ASSIGN; } ungetc(c, stdin); return ADDSUB; } else if (c == '*' || c == '/') { lexeme[0] = c; lexeme[1] = '\0'; return MULDIV; } else if (c == '\n') { lexeme[0] = '\0'; return END; } else if (c == '=') { strcpy(lexeme, "="); return ASSIGN; } else if (c == '(') { strcpy(lexeme, "("); return LPAREN; } else if (c == ')') { strcpy(lexeme, ")"); return RPAREN; } else if(c == '&'){ lexeme[0] = '&', lexeme[1] = '\0'; return AND; } else if(c == '|'){ lexeme[0] = '|', lexeme[1] = '\0'; return OR; } else if(c == '^'){ lexeme[0] = '^', lexeme[1] = '\0'; return XOR; } else if (c == '_' || isalpha(c)) { lexeme[0] = c, lexeme[1] = '\0'; c = fgetc(stdin); i = 1; while ( (isalpha(c) || isdigit(c) || c == '_') && i < MAXLEN) { lexeme[i] = c; ++i; c = fgetc(stdin); } ungetc(c, stdin); lexeme[i] = '\0'; return ID; } else if (c == EOF) { return ENDFILE; } else { return UNKNOWN; } } void advance(void) { curToken = getToken(); } int match(TokenSet token) { if (curToken == UNKNOWN) advance(); return token == curToken; } char *getLexeme(void) { return lexeme; }
Editor is loading...