posts - 403, comments - 310, trackbacks - 0, articles - 7
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

总算成功的运行了个词法解析程序

Posted on 2007-07-20 15:46 ZelluX 阅读(379) 评论(0)  编辑  收藏 所属分类: Courses
书上的例子,计算行号的,但是书中对行的定义是
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
编译:gcc lex.yy.c
利用管道输入刚才的程序:cat linecount.lex | ./a.out



只有注册用户登录后才能发表评论。


网站导航: