lex有问题

编程入门 行业动态 更新时间:2024-10-11 21:25:48
本文介绍了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.等(缩进的数字)

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有问题

本文发布于:2023-11-28 05:57:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641203.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!