void getToken(){ char* p = currToken.mStr; /* skip the blank */ while ((*p = getchar()) == ' '){}; if (isalnum(*p)){ /* copy string until find the next blank */ while(isalnum( *++p = getchar())); ungetc(*p,stdin); *p = '\0'; currToken.mType = classify_string(); return ; } /* the 'type' descriptor is follow with the '*' */ if(*p == '*'){ strcpy(currToken.mStr,"pointer to"); currToken.mType = *p; return; } currToken.mStr[1] = '\0'; currToken.mType = *p; return; }
/* Read the next token into this.string if it is alphanumeric, classify_string else it must be a single character token this.type = the token itself; terminate this.string with a nul. */ void get_token() { char c = 'a'; while((c = getchar()) != EOF) { if(isspace(c)) continue; else break; } if(c == EOF) { curr_token.type = END; return; } if(isalnum(c) || c == '_') { int i = 0; while(isalnum(c) || c == '_') { curr_token.string[i++] = c; c = getchar(); } curr_token.string[i] = 0; ungetchar(c); curr_token.type = classify_string(); return; } curr_token.type = c; if(c == '*') { strcpy(curr_token.string, "pointer to "); } else { curr_token.string[0] = c; curr_token.string[1] = 0; } }
void gettoken(void){ /* 读入下一个标记,保存在"cur"中 */ char *p = cur.string; /* 略过所有空白符 */ while((*p = getchar()) == ' '); if(isalnum(*p)){ /* 在标识符中读入a-z, 0-9开头 */ while(isalnum(*++p = getchar())); ungetc(*p, stdin); *p = '\0'; cur.type = classify_string(); return; } if(*p == '*'){ strcpy(cur.string, "pointer to "); cur.type = '*'; return; } cur.string[1] = '\0'; cur.type = *p; return; }