LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 715|回复: 2

lex 求助

[复制链接]
发表于 2002-12-20 17:31:22 | 显示全部楼层 |阅读模式
我试了一下redhat8的flex,把我搞糊涂了,那位高手帮我看看
我是想把整数都改为hex, 本来第1 个%%后只有一项{number} ,因为调试才加了
{digit}+        ,9[0-9]+        。但它们只对9开头的数有效。规则好像不能传递
我的样本文件 "test.txt" 如下
==========>8==========
1234
45
6
7
34
987
99
34
==========>8===========

lex文件"mylex.l"如下:
========>8=================================
%{
/* a Lex program that changes all numbers
from decimal to hexadecimal notation,
printing a summary statistic to stderr
*/
#include <stdlib.h>
#include <stdio.h>
int count = 0;
%}
digit [0-9]
number {digit}+
%%
{number}         {
                                int n = atoi (yytext);
                                printf ("0x%xh", n);
                                if (n > 9) count++;
                        }
{digit}+       
                        {
                                int n = atoi (yytext);
                                printf ("0x%xh", n);
                                if (n > 9) count++;
                        }
9[0-9]+                       
                        {
                                int n = atoi (yytext);
                                printf ("0x%xh", n);
                                if (n > 9) count++;
                        }               

%%
main( )
{
        yylex ( );
        fprintf ( stderr, "number of replacements = %d",count ) ;
        return 0 ;
}
int yywrap()
{
        return 1;
}
========================>8==============================

命令行下执行:
flex mylex.l           (生成 lex.yy.c)

gcc lex.yy.c -o mylex

./mylex < test.txt

结果如下:
1234
45
6
7
34
0x3dbh
0x63h
34


怎么回事啊?
谢谢各位拉!
发表于 2003-1-1 13:34:52 | 显示全部楼层

re:lex

thinkers兄:
想与你认识,我对lex技术也有兴趣

          我的email:sqts@msn.com
             fml
 楼主| 发表于 2003-1-3 08:20:52 | 显示全部楼层
OK,
thinkers@21cn.com
ICQ:48941168
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表