免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1886 | 回复: 3
打印 上一主题 下一主题

lex生成的程序怎样正常结束 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-07 22:44 |只看该作者 |倒序浏览
比如这个程序
%{
        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个文件中信息,第三个文件信息和总计信息都显示不出来?

论坛徽章:
3
2015年迎新春徽章
日期:2015-03-04 09:56:11数据库技术版块每日发帖之星
日期:2016-08-03 06:20:00数据库技术版块每日发帖之星
日期:2016-08-04 06:20:00
2 [报告]
发表于 2008-07-07 23:17 |只看该作者
return (file ?  1:0);

论坛徽章:
0
3 [报告]
发表于 2008-07-07 23:34 |只看该作者

回复 #2 cjaizss 的帖子

thanks a lot

论坛徽章:
0
4 [报告]
发表于 2008-07-07 23:34 |只看该作者
stupid me-_-#
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP