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
这样的数字,而不是42
,45.< /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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
迟到了你的问题的答案......但为了它的价值,我尝试用
*
替换原始 lex 文件中的*
(DNUM
的第二个模式) code>+ (因为这确保了小数点右侧至少有一位数字,以便该数字被计为小数......)并且它似乎至少对我有用。希望这对将来的人有帮助。lex 文件:
示例输入(在命令行上):
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 forDNUM
) 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:
example input (on command line):