# include<stdio.h> # include<stdlib.h> # include<string.h>
int panduan(char tmp[]) { char b[6][10] = {"begin", "if", "then", "while", "do", "end", }; int k = 0;
while(k < 6) // 临时变量tmp 与 字符串b[k] 进行比较 { if(strcmp(tmp, b[k]) == 0) { printf("<%s, %d> ", tmp, k+1); return 0; } k++; } printf("<%s, 10> ", tmp);
return 0; }
void cifafenxi(char a[]) { int i = 0, j = 0; char tmp[10]; int flag = 0;
while(a[i] != ' ') { if(a[i] >= 'a' && a[i] <= 'z') // 判断标识符 { tmp[j] = a[i]; j++; while((a[i+1] >= 'a' && a[i+1] <= 'z') || (a[i+1] >= '0' && a[i+1] <= '9')) { i++; tmp[j] = a[i]; j++; } tmp[j] = ' '; j = panduan(tmp); }
if(a[i] >= '0' && a[i] <= '9') // 判断数字 { tmp[j] = a[i]; j++; while(a[i+1] >= '0' && a[i+1] <= '9') { i++; tmp[j] = a[i]; j++; } tmp[j] = ' '; printf("<%s, 11> ", tmp); j = 0; }
switch(a[i]) // 判断符号 { case '+':printf("<+, 13> ");break; case '-':printf("<-, 14> ");break; case '*':printf("<*, 15> ");break; case '/':printf("</, 16> ");break; case ':': { if(a[i+1] == '=') printf("<:=, 18> "); else printf("<:, 17> "); }break; case '<': { if(a[i+1] == '=') printf("<<=, 21> "); else if(a[i+1] == '>') printf("<<>, 22> "); else printf("<<, 20> "); }break; case '>': { if(a[i+1] == '=') printf("<>=, 24> "); else printf("<>, 23> "); }break; case '=':printf("<=, 25> ");break; case ';':printf("<;, 26> ");break; case '(':printf("<(, 27> ");break; case ')':printf("<), 28> ");break; case '#':printf("<#, 0> ");break; } i++; } }
int main(void) { char a[300];
printf("input : "); gets(a); cifafenxi(a);
return 0; }