lex生成的程序怎样正常结束
比如这个程序
%{
int wordCount = 0, lineCount = 0, charCount = 0;
%}
word [^\t\n]+
eol \n
%%
{word} {wordCount++; charCount += yyleng;}
{eol} {charCount++; lineCount++;}
. charCount++;
%%
/*main()
{
yylex();
printf("charCount:%d wordCount:%d lineCount:%d\n", charCount, wordCount, lineCount);
}
main(int argc, char **argv)
{
if(argc >1)
{
FILE *file;
file = fopen(argv[1], "r");
if(!file)
{
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}
yylex();
printf("charCount:%d wordCount:%d lineCount:%d\n", charCount, wordCount, lineCount);
return 0;
}
int
yywrap()
{
return 1;
}
*/
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)
{
/*only 1 file to be handled*/
currentFile = 1;
file = fopen(argv[1], "r");
if(!file)
{
fprintf(stderr, "could not open %s\n", argv[1]);
exit(1);
}
yyin = file;
}
if(argc > 2)
{
yywrap();
}
yylex();
if(argc > 2)
{
printf("lineCount:%8lu wordCount:%8lu charCount:%8lu in file:%s\n", lineCount, wordCount, charCount, fileList[currentFile - 1]);
totalCC += charCount;
totalWC += wordCount;
totalLC += lineCount;
printf("totalLC:%8lu totalWC:%8lu totalCC%8lu %s\n", totalLC, totalWC, totalCC);
}
else
printf("lineCount:%8lu wordCount:%8lu charCount:%8lu", lineCount, wordCount, charCount);
}
int
yywrap()
{
FILE *file;
if((currentFile != 0) && (nFiles > 1) && (currentFile < nFiles))
{
printf("lineCount:%8lu wordCount:%8lu charCount:%8lu in file:%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);
}
统计多个文件中单词的程序,假如有3个文件,为什么能统计出前2个文件中信息,第三个文件信息和总计信息都显示不出来?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
return (file ? 1:0);
thanks a lot
stupid me-_-#