求教一个单词计数程序的小问题
我用flex写了一个多文件的单词计数的程序,lex和gcc编译都能通过,就是运行的时候会报“段错误”,我用gdb调试了一下(刚刚学会),还是看不出什么有价值的信息,求哪位大虾帮我看一下是哪里出错了,多谢。
以下是lex文件:
%{ unsigned long charCount=0, wordCount=0, lineCount=0; #undef yywrap%}word [^ \t\n]+eol \n%%{word} {wordCount++; charCount +=yyleng;}{eol} {charCount++; lineCount++;}. charCount++;%%char **fileList;;unsigned currentFile=0;unsigned nFiles;unsigned long totalCC=0;unsigned long totalWC=0;unsigned long totalLC=0;main(int argc, char **argv){ FILE *file; fileList=argv+1; nFiles=argc-1; if(argc==2) { currentFile=1; if(!file) { fprintf(stderr, "could not open %s\n",argv[1]); exit(1); } yyin=file; } if(argc>2) yywrap(); yylex(); if(argc>2) { printf("%8lu %8lu %8lu %s\n", lineCount, wordCount, charCount, fileList[currentFile-1]); totalCC+=charCount; totalWC+=wordCount; totalLC+=lineCount; printf("%8lu %8lu %8lu total\n", totalLC, totalWC, totalCC); } else printf("%8lu %8lu %8lu\n", lineCount, wordCount, charCount); return 0;}yywrap(){ FILE *file=NULL; if((currentFile!=0)&&(nFiles>1)&&(currentFile<nFiles)) { printf("%8lu %8lu %8lu %s\n", lineCount, wordCount, charCount, fileList[currentFile-1]); totalCC+=charCount; totalWC+=wordCount; totalLC+=lineCount; charCount=wordCount=lineCount=0; fclose(yyin); } while(fileList[currentFile]!=(char *)0) { file=fopen(fileList[currentFile++], "r"); if(file!=NULL) { yyin=file; break; } fprintf(stderr, "could not open %s\n", fileList[currentFile-1]); } return (file?0:1);}