moonstruck2008 发表于 2008-07-07 22:44

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, "r");
                if(!file)
                {
                        fprintf(stderr, "could not open %s\n", argv);
                        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, "r");
                if(!file)
                {
                        fprintf(stderr, "could not open %s\n", argv);
                        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);
                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);
                totalCC += charCount;
                totalWC += wordCount;
                totalLC += lineCount;
                charCount = wordCount = lineCount = 0;
                fclose(yyin);
        }
       
        while(fileList != (char *)0)
        {
                file = fopen(fileList, "r");
                if(file != NULL)
                {
                        yyin = file;
                        break;
                }
                fprintf(stderr, "could not open %s\n", fileList);
        }
        return (file ? 0 : 1);
}

统计多个文件中单词的程序,假如有3个文件,为什么能统计出前2个文件中信息,第三个文件信息和总计信息都显示不出来?

cjaizss 发表于 2008-07-07 23:17

return (file ?1:0);

moonstruck2008 发表于 2008-07-07 23:34

回复 #2 cjaizss 的帖子

thanks a lot

moonstruck2008 发表于 2008-07-07 23:34

stupid me-_-#
页: [1]
查看完整版本: lex生成的程序怎样正常结束