|
关于Basic程序解释器及编译原理的简单化(1)--词法分析和代数式求值(7) } if (isdigit(*prog)) { /* number */ while (!isdelim(*prog)) *temp++=*prog++; *temp = '\0'; return (token_type = NUMBER); } if (isalpha(*prog)) { /* var or command */ while (!isdelim(*prog)) *temp++=*prog++; token_type = STRING; } *temp = '\0'; /* see if a string is a command or a variable */ if (token_type == STRING) { tok = look_up(token); /* convert to internal rep */ if (!tok) token_type = VARIABLE; else token_type = COMMAND; /* is a command */ } return token_type; } /* return a token to input stream */ void putback() { char *t; t = token; for (;*t;t++) prog--; }
look_up(char *s) { register int i,j; char *p;
/* convert to lowercase */ p = s; while (*p) { *p = tolower(*p); p++; } /* see if token is in table */ for (i=0;*table[i].command;i++) if (!strcmp(table[i].command,s)) return table[i].tok; return 0; /* unknown command */ } /* return true if c is a delimiter */ isdelim(char c) { if (strchr(";,+-<>/*%^=() ",c)c==9c=='\r'c==0)
|