/* * cc lexa.c * ./a.out < jazyk.txt * * */ #include #include #define MAXTOKENLEN 1023 #define TOKEN_string 256 #define TOKEN_EOF 257 char char_tokens[256]; #define ISCHARTOKEN(x) (char_tokens[x]) struct token { int tok_type; char buffer[MAXTOKENLEN+1]; }; struct token yyred; int line = 0; int xfgetc(FILE *f) { int c = fgetc(f); if (c=='\n') line++; return c; } void xungetc(int c, FILE *f) { if (c=='\n') line--; ungetc(c, f); } /* * Vraci: * 0 - token nacten OK * -1 - chyba (nezparovane "") * -2 - Nacten prilis velky token * * */ int next_token(struct token *t) { int c, i; do { c = xfgetc(stdin); } while (isspace(c)); switch (c) { case EOF: t->tok_type = TOKEN_EOF; return 1; break; case '"': t->tok_type = TOKEN_string; for(i=0;;i++) { if (i==MAXTOKENLEN) return -2; c = xfgetc(stdin); if (c==EOF) return -1; if (c=='"') { t->buffer[i] = '\0'; return 0; } t->buffer[i] = c; } break; default: if (ISCHARTOKEN(c)) { t->tok_type = c; return 0; } t->tok_type = TOKEN_string; for (i=0;;i++) { if (i==MAXTOKENLEN) return -2; if (ISCHARTOKEN(c) || c=='\0' || c=='"') { xungetc(c, stdin); t->buffer[i] = '\0'; return 0; } if (isspace(c)) { t->buffer[i] = '\0'; return 0; } t->buffer[i] = c; c = xfgetc(stdin); } break; } } void init(void) { int i; for (i=0;i<256;i++) char_tokens[i] = 0; char_tokens['{'] = 1; char_tokens['}'] = 1; char_tokens['$'] = 1; char_tokens['%'] = 1; } int main(int argc, char **argv) { init(); while (next_token(&yyred)==0) { if (yyred.tok_type==TOKEN_string) { printf("TOKEN STERING: "); printf("%s\n", yyred.buffer); } if (yyred.tok_type<256) { printf("TOKEN %c\n", yyred.tok_type); } } return 0; }