我的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有问题
发布评论