lex有问题 [英] Have problem with lex

查看:65
本文介绍了lex有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的lex如下:

LNUM    [0-9]+
DNUM([0-9]*"."[0-9]+)|([0-9]+"."[0-9]*)                                                                                    
%%
{LNUM} {
printf("\t");ECHO;printf("\r\n");
}

{DNUM} {
printf("\t");ECHO;printf("\r\n");
}

但是事实证明,它只能匹配4.12 .2之类的数字,而不能匹配4245.等(缩进的数字)

But it turns out that it can only match numbers like 4.12 .2,not 42,45. etc.(those indented are matched)

输出:

1.
1.
.1
    .1

12
12

我的目标是同时匹配整数和浮点数.

My target is to match both integers and float numbers.

这里有人可以告诉我上面有什么问题吗?

Can anyone here tell me what's wrong above?

推荐答案

您的问题的最新答案...但是出于价值考虑,我尝试替换了原始lex文件中的*(用于DNUM)和+(因为这样可以确保您至少在小数点右边保留一位数字,以便将该数字计为小数...),这似乎对我有用,至少.希望这对以后的人有所帮助.

Late answer to your question... but for what it's worth, I tried replacing the * you had in the original lex file (the second pattern for DNUM) with a + (because that ensures that you at least have one digit to the right of the decimal point in order for the number to be counted as a decimal...) and it seems to work for me, at least. Hope this helps someone in the future.

lex文件:

%{
#include <iostream>
using namespace std;
%}

LNUM    [0-9]+
DNUM    ([0-9]*"."[0-9]+)|([0-9]+"."[0-9]+) 

%option noyywrap

%%
{LNUM}*  { cout << "lnum: " << yytext << endl; }
{DNUM}*  { cout << "dnum: " << yytext << endl; }
%%

int main(int argc, char ** argv)
{
    yylex();
    return 0;
}


示例输入(在命令行上):

$ echo "4.12 .2 42 45. " | ./lexer
dnum: 4.12
dnum: .2
lnum: 42
lnum: 45.

这篇关于lex有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆