書上的例子,計算行號的,但是書中對行的定義是
line *.\n
貌似不正確,flex無法解析,改成line (.)*\n就可以了。
書上的樣例也沒有yywrap,寫了個空函數。
編譯:gcc lex.yy.c
利用管道輸入剛才的程序:cat linecount.lex | ./a.out
line *.\n
貌似不正確,flex無法解析,改成line (.)*\n就可以了。
書上的樣例也沒有yywrap,寫了個空函數。
%{
/* a Lex program that adds line numbers
to lines of text, printing the new text
to the standard output
*/
#include <stdio.h>
int lineno = 1;
%}
line (.)*\n
%%
{line} { printf ("%5d %s", lineno++, yytext); }
%%
main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
生成flex程序:flex linecount.lex/* a Lex program that adds line numbers
to lines of text, printing the new text
to the standard output
*/
#include <stdio.h>
int lineno = 1;
%}
line (.)*\n
%%
{line} { printf ("%5d %s", lineno++, yytext); }
%%
main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
編譯:gcc lex.yy.c
利用管道輸入剛才的程序:cat linecount.lex | ./a.out