#include #include #include "getch.h" #include "token.h" #include "err.h" #define ARR_SIZE(X) sizeof(X)/sizeof(X[0]) char tokentype; char token[MAXTOKEN]; char prevtoken; char *typqual[] = { "const", "volatile" }; char *storspec[] = { "auto", "extern", "register", "static" }; char *datatyp[] = { "char", "int", "long", "short", "signed", "unsigned", "void" }; int gettoken(void) { char c, *p = token; char getch(void); void ungetch(char); if (prevtoken == YES) { prevtoken = NO; return tokentype; } while(isblank(c = getch())) ; if (c == '/') { /* ignore comments */ if ((c = getch()) == '/') { while ((c = getch()) != '\n') ; } else if (c == '*') { while (getch() != '*' || (c = getch()) != '/') if (c == '*') ungetch('*'); return gettoken(); } else { ungetch(c); c = '/'; } } if (c == '(') { if ((c = getch()) == ')') { strcpy(token, "()"); return tokentype = PARENS; } else { ungetch(c); return tokentype = '('; } } else if (c == '[') { for (*p++ = '['; (*p++ = getch()) != ']';) ; *p = '\0'; return tokentype = BRACKETS; } else if (isalpha(c)) { for (*p++ = c; isalnum(c = getch());) *p++ = c; *p = '\0'; ungetch(c); return tokentype = NAME; } else { return tokentype = c; } } int peaktoken() { char type; if (prevtoken == YES) err("prevtoken overflow"); type = gettoken(); prevtoken = YES; return type; } int isdatatyp(void) { int i, len; for (i = 0, len = ARR_SIZE(typqual); i < len; ++i) if(!strcmp(token, typqual[i])) return 1; for (i = 0, len = ARR_SIZE(storspec); i < len; ++i) if(!strcmp(token, storspec[i])) return 1; for (i = 0, len = ARR_SIZE(datatyp); i < len; ++i) if(!strcmp(token, datatyp[i])) return 1; return 0; }