该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
部分代码如下:
int CreatList(node *phd[8]){ node *new_node, *p; int line = 0, row = 1, num = 0; char *aline; char token[80]; char filename[30]; char ch; FILE *fin, *fout;
printf("请输入要分析的文件的文件名:\n"); scanf("%s", filename); if(fin = fopen(filename, "r") != NULL) { fout = fopen("lex.txt", "w"); fprintf(fout,"序号 token类型 token 行号 列号 出现次数\n"); fprintf(fout,"-----------------------------------------------------\n"); } else { printf("Fail to open the file for analysis!"); exit(-1); }
while(!feof(fin)) { printf("0"); aline = (char *)malloc(80); fgets(aline, 80, fin); line++; sscanf(aline, "%c", ch); if(ch != '\\') { num++; if((isalpha(ch))||(ch == '_')) { sscanf(aline, "%[a-zA-Z1-9_]", token); if(Keyword(token)) { AddNode(token,num,line,row,phd[1],p); p->ptoken->type = "关键字"; Print(p,fout); } else { AddNode(token,num,line,row,phd[0],p); p->ptoken->type = "标识符"; Print(p,fout); } } else if(isdigit(ch)) { sscanf(aline, "%[1-9.]", token); if(IntConst(token)) { AddNode(token,num,line,row,phd[2],p); p->ptoken->type = "整型常量"; Print(p,fout); } else { AddNode(token,num,line,row,phd[3],p); p->ptoken->type = "浮点常量"; Print(p,fout); } } else switch(ch) { case '"': sscanf(aline, "%[^\"]", token); AddNode(token,num,line,row,phd[5],p); p->ptoken->type = "字符串"; Print(p,fout); break; case '\'': sscanf(aline, "%[^']", token); AddNode(token,num,line,row,phd[4],p); p->ptoken->type = "字符常量"; Print(p,fout); break; default: sscanf(aline, "%[^1-9a-zA-Z_]", token); if(Operator(token)) { AddNode(token,num,line,row,phd[6],p); p->ptoken->type = "运算符"; Print(p,fout); } else { AddNode(token,num,line,row,phd[7],p); p->ptoken->type = "分隔符"; Print(p,fout); } } } } fclose(fin); fclose(fout); return 0;