该示例统计输入文件中的行数、单词数及字符数。
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(fb2-1)
find_package(Flex)
FLEX_TARGET(MyScanner fb2-1.txt ${CMAKE_CURRENT_BINARY_DIR}/lexer.c)
ADD_EXECUTABLE(fb2-1 ${FLEX_MyScanner_OUTPUTS})
fb2-1.txt
%option noyywrap
%{
int chars = 0;
int words = 0;
int lines = 0;
%}
%%
[a-zA-Z]+ { ++words; chars += yyleng; }
\n { ++lines; }
. { ++chars; }
%%
int main(int argc, char ** argv)
{
if (argc>1)
{
yyin = fopen(argv[1], "r");
if (!yyin)
{
perror(argv[1]);
return 1;
}
yylex();
printf("%8d%8d%8d\n", lines, words, chars);
}
return 0;
}