基本思路:
程序代码:
#include TOKEN[0]=ch; ch=fgetc(fp); i=1; while(isdigit(ch)||ch == '.' || ch == 'E'||ch == 'e') { if (ch == '.'){checkdot =1;} else if (ch == 'e'||ch == 'E') { checke = 1; TOKEN[i]=ch; i++; ch=fgetc(fp); if(ch == '+'){TOKEN[i]=ch; i++; ch=fgetc(fp); { TOKEN[i]=ch; i++; ch=fgetc(fp); continue; } } else {TOKEN[i]=ch; 6 e的指数 小数 i++; ch=fgetc(fp); { TOKEN[i]=ch; i++; ch=fgetc(fp); continue; } } *if (ch == '-')TOKEN[i]=ch; i++; ch=fgetc(fp); { TOKEN[i]=ch; i++; ch=fgetc(fp); continue; }* } TOKEN[i]=ch; i++; ch=fgetc(fp); } TOKEN[i]= '\\0'; fseek(fp,-1,1); if(checkdot==1) { out(UCON,TOKEN); } else if (checke == 1) 7 { out(UCON,TOKEN);二进制数 } else { out(INT,TOKEN);整型 } } else { case '<': ch = fgetc(fp); if (ch == '=')out(LE, \else if (ch == '>') out(NE, \else { fseek(fp, -1, 1);TOKEN[i]=ch; switch (ch) 输出 i++; ch=fgetc(fp); } break; out(LT, \ case '=': out(EQ, \ case ':': ch = fgetc(fp); if (ch == '=')out(IS, \ else { fseek(fp,-1,1); report_error( ); } 8 break; case '>': ch = fgetc(fp); if (ch == '=')out(GE, \else { } break; fseek(fp, -1, 1); out(GT, \ case '+':out(PL,\ case '-':out(MI,\ case '*':out(MU,\case '':out(DI,\ case '\\n': break; fseek(fp, -1, 1);report_enter(); break; case '\\r' :out(ENT,\ } ch=fgetc(fp); case ' ': break; case '\\t': break; default: report_error(); break; t=fgetc(fp); } char *KeyWordTable[MAX_KEY_NUMBER] = { \\关键字 int lookup(char *token) { int n = 0; 9 fseek (fp,-1,1); } return; while (strcmp(KeyWordTable[n], KEY_WORD_END)) *strcmp比较两串是 否相同,若相同返回0* { if (!strcmp(KeyWordTable[n], token)) *比较token所指向的关键字和保 留字表中哪个关键字相符* { return n + 1; *根据单词分类码表I,设置正确的关键字类别码,并 返回此类别码的值* } void report_error(){ } void out(int c, char* v){ char* cl = \switch (c) { case case case case case case 1: cl = \ 2: cl = \3: cl = \ printf(\ } return 0; *单词不是关键字,而是标识符* } n++; break; 4: cl = \ 5: cl = \6: cl = \7: cl = \8: cl = \9: cl = \ 10 case case case