Lex代码统计文件信息全为0,请求错误排查帮助
Lex程序统计结果全为0的问题排查
问题概述
编写Lex程序用于扫描文件并统计字符、单词、数字、元音、辅音、特殊字符及行数,但运行后所有统计结果均为0。
原代码
%{ #include<stdio.h> int lines = 0; int vowels = 0; int consonants = 0; int characters = 0; int digits = 0; int words = 0; int special_char = 0; %} %% [a-zA-Z] { characters++; if (strchr("aeiouAEIOU", yytext[0])) vowels++; else consonants++; } [0-9] { characters++; digits++; } [ ] { characters++; if (yytext[0] == ' ') lines++; } . { characters++; special_char++; } %% int yywrap(){ return 1; } int main(int argc, char* argv[]) { FILE* input_file = fopen("input.txt", "r"); if (!input_file) { printf("Error opening the file.\n"); return 1; } char buffer[100]; printf("Input text from the file:\n"); while (fgets(buffer, sizeof(buffer), input_file) != NULL) { printf("%s", buffer); } printf("\n"); yyin = input_file; yylex(); printf("Character count: %d\n", characters); printf("Word count: %d\n", words); printf("Digit count: %d\n", digits); printf("Vowel count: %d\n", vowels); printf("Consonant count: %d\n", consonants); printf("Special character count: %d\n", special_char); printf("Line count: %d\n", lines); fclose(input_file); return 0; }
编译与运行信息
编译命令
flex prac.l gcc -C lex.yy.c ./a.out
运行输出
Input text from the file: My name is xyz. 123123 @!$%(@#) 124adasd31231 Character count: 0 Word count: 0 Digit count: 0 Vowel count: 0 Consonant count: 0 Special character count: 0 Line count: 0
错误原因分析
- 文件指针位置错误:调用
yylex()前,先用fgets读取完所有文件内容,导致文件指针移动到末尾,yylex()无法读取任何内容,所有统计值为0。 - 编译命令错误:
gcc -C仅执行预处理步骤,不会生成可执行文件a.out,你运行的可能是旧残留文件,编译步骤实际未完成。 - 缺少必要头文件:使用
strchr函数但未包含<string.h>,编译会产生警告,虽不直接导致统计为0,但属于规范问题。 - 单词统计逻辑缺失:
words变量未在任何规则中更新,即使修复其他问题,单词数仍为0。 - 空白字符正则表达式不规范:原规则中的空白字符写法存在显示问题,应明确写为
[ \t\n](空格、制表符、换行符)。
修复方案
1. 调整文件读取逻辑
两种可选方式:
- 先调用
yylex()完成统计,再重置文件指针读取内容打印; - 在
fgets循环后,用fseek(input_file, 0, SEEK_SET)将指针重置到开头,再调用yylex()。
2. 修正编译命令
将编译命令改为:
flex prac.l gcc lex.yy.c -o a.out ./a.out
3. 添加必要头文件
在%{ ... %}块中添加#include <string.h>。
4. 实现单词统计逻辑
添加全局变量int in_word = 0;,通过状态标记区分是否处于单词中,遇到单词起始时计数:
[a-zA-Z] { characters++; if (!in_word) { words++; in_word = 1; } if (strchr("aeiouAEIOU", yytext[0])) vowels++; else consonants++; } [ \t\n] { characters++; if (yytext[0] == '\n') lines++; in_word = 0; } . { characters++; special_char++; in_word = 0; }
修复后的完整代码
%{ #include<stdio.h> #include<string.h> int lines = 0; int vowels = 0; int consonants = 0; int characters = 0; int digits = 0; int words = 0; int special_char = 0; int in_word = 0; %} %% [a-zA-Z] { characters++; if (!in_word) { words++; in_word = 1; } if (strchr("aeiouAEIOU", yytext[0])) vowels++; else consonants++; } [0-9] { characters++; digits++; } [ \t\n] { characters++; if (yytext[0] == '\n') lines++; in_word = 0; } . { characters++; special_char++; in_word = 0; } %% int yywrap(){ return 1; } int main(int argc, char* argv[]) { FILE* input_file = fopen("input.txt", "r"); if (!input_file) { printf("Error opening the file.\n"); return 1; } // 先执行统计 yyin = input_file; yylex(); // 重置指针打印文件内容 fseek(input_file, 0, SEEK_SET); char buffer[100]; printf("Input text from the file:\n"); while (fgets(buffer, sizeof(buffer), input_file) != NULL) { printf("%s", buffer); } printf("\n"); printf("Character count: %d\n", characters); printf("Word count: %d\n", words); printf("Digit count: %d\n", digits); printf("Vowel count: %d\n", vowels); printf("Consonant count: %d\n", consonants); printf("Special character count: %d\n", special_char); printf("Line count: %d\n", lines); fclose(input_file); return 0; }
内容的提问来源于stack exchange,提问作者Aaryan R Soni
相关产品推荐
相关产品推荐

