www.pudn.com > lily11.rar > lily.c
#include#include #include #define max 50 //取字符串的数组最大数 void main() { FILE *in; char arr[max];// char currentchar;// int i=0;//定义整型 if((in=fopen("lily11","r"))==NULL) {printf("cannot open lily11\n"); //如果文件为空的话,则不能打开 exit(1); } AA: currentchar=fgetc(in); AA1: for (i=0;i<=50;i++) {arr[i]='\0';} while(currentchar!=EOF) { if(currentchar==' ')//判断空格,把它过滤 goto AA; else if(currentchar=='\n')goto AA;//识别运算符或界符 else if (currentchar=='(') {printf("out %c,%d\n",currentchar,28);goto AA;} else if (currentchar==')') {printf("out %c,%d\n",currentchar,29);goto AA;} else if (currentchar==';') {printf("out %c,%d\n",currentchar,18);goto AA;} else if (currentchar=='+') {printf("out %c,%d\n",currentchar,13);goto AA;} else if (currentchar=='-') {printf("out %c,%d\n",currentchar,14);goto AA;} else if (currentchar=='*') {printf("out %c,%d\n",currentchar,15);goto AA;} else if (currentchar=='/') {printf("out %c,%d\n",currentchar,16);goto AA;} else if (currentchar=='=') {printf("out %c,%d\n",currentchar,25);goto AA;} else if (currentchar=='#') {printf("out %c,%d\n",currentchar,0);goto AA;} else if (currentchar=='<') {currentchar=fgetc(in); if(currentchar=='>') {printf("out <>,%d\n",21);goto AA;} else if(currentchar=='=') {printf("out <=,%d\n",22);goto AA;} else {printf("out <,%d\n",20);goto AA1;} } else if (currentchar=='>') {currentchar=fgetc(in); if(currentchar=='=') {printf("out >=,%d\n",24);goto AA;} else {printf("out >,%d\n",23);goto AA1;} } else if (currentchar==':') {currentchar=fgetc(in); if(currentchar=='=') {printf("out :=,%d\n",17);goto AA;} else {printf(": is Error!\n");goto AA1;} } else if(currentchar>='0'&¤tchar<='9')//判断识常数 {i=0; currentchar=fgetc(in); while (currentchar>='0'&¤tchar<='9') {arr[i++]=currentchar;currentchar=fgetc(in);} printf("out %s,%6d\n",arr,11); for (i=0;i<=50;i++) {arr[i]='\0'; } } else if(currentchar>='a'&¤tchar<='z'||currentchar>='A'&¤tchar<='Z')//判断是不是关键字或字符 { i=0; arr[i++]=currentchar; currentchar=fgetc(in); while(currentchar>='0'&¤tchar<='9'||currentchar>='a'&¤tchar<='z'||currentchar>='A'&¤tchar<='Z') { arr[i++]=currentchar;currentchar=fgetc(in); } if(strcmp(arr,"begin")==0) {printf("out begin,%d\n",1);goto AA1;} else if(strcmp(arr,"end")==0) {printf("out end,%d\n",6);goto AA1;} else if(strcmp(arr,"if")==0) {printf("out if,%d\n",2);goto AA1;} else if(strcmp(arr,"then")==0) {printf("out then,%d\n",3);goto AA1;} else if(strcmp(arr,"do")==0) {printf("out do,%d\n",5);goto AA1;} else if(strcmp(arr,"while")==0) {printf("out while,%d\n",4);goto AA1;} else {printf("out %s,%d\n",arr,10);goto AA1;} for(i=0;i<=50;i++) { arr[i]='\0'; } } else {printf("The %c is Error!\n",currentchar);goto AA;} } fclose(in); }