|
/*字符串分类函数*/ enum type_tag classify_string(void) { char *s = this.string; if (strcmp(s, "const")) { strcpy(s, "read-only"); return QUALIFIER; } if (!strcmp(s, "volatile")) return QUALIFIER; if (!strcmp(s, "void")) return TYPE; if (!strcmp(s, "char")) return TYPE; if (!strcmp(s, "signed")) return TYPE; if (!strcmp(s, "unsigned")) return TYPE; if (!strcmp(s, "short")) return TYPE; if (!strcmp(s, "int")) return TYPE; if (!strcmp(s, "long")) return TYPE; if (!strcmp(s, "float")) return TYPE; if (!strcmp(s, "double")) return TYPE; if (!strcmp(s, "struct")) return TYPE; if (!strcmp(s, "union")) return TYPE; if (!strcmp(s, "enum")) return TYPE; return IDENTIFIER; }
//读取下一个标记到this.string
void get_token(void) { char *p = this.string; /*跳过前面的空白字符*/ while ((*p = getchar()) == ' '); /*如果是字母数字组合, 调用classify_string()*/ if (isalnum(*p)) { while (isalnum(*++p = getchar())); ungetc(*p, stdin); *p = '\0'; this.type = classify_string(); return ; } //单字符标记
if (*p == '*') { strcpy(this.string, "pointer to"); this.type = '*'; return ; } /*单字符标记*/ this.string[1] = '\0'; this.type = *p; return ; }
/*理解所有分析过程的代码段*/ void read_to_first_identifer() { get_token(); while (this.type != IDENTIFIER) { push(this); get_token(); } printf("%s is ", this.string); get_token(); }
|