当前位置: 技术问答>linux和unix
LEX词法分析器怎么用?
来源: 互联网 发布时间:2014-10-23
本文导语: 请大虾写一个最简单的例子好吗。谢谢 下面的哪错了? int num_lines = 0, num_chars = 0; %% n ++num_lines; ++num_chars; . ++num_chars; %% main() { yylex(); printf...
请大虾写一个最简单的例子好吗。谢谢
下面的哪错了?
int num_lines = 0, num_chars = 0;
%%
n ++num_lines; ++num_chars;
. ++num_chars;
%%
main()
{
yylex();
printf( "# of lines = %d, # of chars = %dn",
num_lines, num_chars );
}
下面的哪错了?
int num_lines = 0, num_chars = 0;
%%
n ++num_lines; ++num_chars;
. ++num_chars;
%%
main()
{
yylex();
printf( "# of lines = %d, # of chars = %dn",
num_lines, num_chars );
}
|
这样改一下就好了. %{ %}中的内容会写入lex.yy.c的. n . 后面的动作规则要用{}括起来. 编译用 $ lex file.l $ cc lex.yy.c -ll ----------------------------------- %{ int num_lines = 0, num_chars = 0; %} %% n { ++num_lines; ++num_chars; } . { ++num_chars; } %% main() { yylex(); printf( "# of lines = %d, # of chars = %dn", num_lines, num_chars ); }