首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求教一个单词计数程序的小疑点

2012-02-05 
求教一个单词计数程序的小问题我用flex写了一个多文件的单词计数的程序,lex和gcc编译都能通过,就是运行的

求教一个单词计数程序的小问题
我用flex写了一个多文件的单词计数的程序,lex和gcc编译都能通过,就是运行的时候会报“段错误”,我用gdb调试了一下(刚刚学会),还是看不出什么有价值的信息,求哪位大虾帮我看一下是哪里出错了,多谢。

以下是lex文件:

C/C++ code
%{    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);}



c文件就不发了,太长,怕吓到大家。

gdb的信息如下:

ys@gates-debian:~/Desktop/lex$ gdb ch2-03 core
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...

warning: Can't read pathname for load map: Input/output error.
Reading symbols from /lib/i686/cmov/libc.so.6...done.
Loaded symbols for /lib/i686/cmov/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
Core was generated by `./ch2-03 ch2-03.l'.
Program terminated with signal 11, Segmentation fault.
[New process 6285]
#0 0xb7ed9d85 in _IO_sgetn () from /lib/i686/cmov/libc.so.6
(gdb) 

按网上教的方法做的,但好像没有找到错误出在什么地方,向各位求教。



[解决办法]
友情up
[解决办法]
if(argc==2) {
currentFile=1;              //只有一个额外参数的时候,打开什么文件?为何没看到open操作啊?
if(!file) {
fprintf(stderr, "could not open %s\n",argv[1]);
exit(1);
}
yyin=file;
}
 if(argc>2)
yywrap();

热点排行