flex--扫描器生成工具
flex是一种功能强大的扫描器生成工具。不仅能用在编译器工具中,而且可以生成其他辅助工具。
下面的代码是个统计文件的行数和单词数目的小例子:
test.lex:
%{
int num_lines = 0, num_chars = 0;
%}
%%
\n ++num_lines; ++num_chars;
. ++num_chars;
%%
main()
{
yylex();
printf("#of lines = %d, # of chars = %d\n",
num_lines,num_chars);
}
生成c文件:
flex -otest.c test.lex
编译c文件:
gcc -o test test.c -lfl
运行:
./test < test.c
#of lines = 1755, # of chars = 44207
转载请注明出处