lex 有问题

发布于 2024-11-28 13:34:32 字数 508 浏览 5 评论 0原文

我的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.< /code> 等(那些缩进的内容是匹配的)

输出:

1.
1.
.1
    .1

12
12

我的目标是匹配整数和浮点数。

这里有人能告诉我上面出了什么问题吗?

My lex as follows:

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");
}

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

Output:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪推晚风 2024-12-05 13:34:32

迟到了你的问题的答案......但为了它的价值,我尝试用 * 替换原始 lex 文件中的 *DNUM 的第二个模式) code>+ (因为这确保了小数点右侧至少有一位数字,以便该数字被计为小数......)并且它似乎至少对我有用。希望这对将来的人有帮助。


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.

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 file:

%{
#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;
}

example input (on command line):

$ echo "4.12 .2 42 45. " | ./lexer
dnum: 4.12
dnum: .2
lnum: 42
lnum: 45.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文