Have problem with lex

前端 未结 1 963
梦如初夏
梦如初夏 2021-01-25 17:56

My lex as follows:

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


        
相关标签:
1条回答
  • 2021-01-25 18:41

    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.
    
    0 讨论(0)
提交回复
热议问题