Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0

    How to debug FLEX/LEX codes on Linux and Windows?


    I am having a hard time in understanding FLEX/LEX codes because I don't know how to debug them.
    Could someone show the procedure for debugging FLEX/LEX programs on Linux and Windows?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,835
    Rep Power
    509
    Code:
    %{
      /*
        $ # gnu-linux build
        $ flex l.lex
        $ gcc lex.yy.c -lfl
        $ ./a.out
        blort
        
        show some code!
        $ 
      */
    %}
    
    %%
    
    <<EOF>> puts("show some code!"), exit(0);
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    Do you mean that, in Linux terminal, I write the lines between "%{ %}" including them and the code?
    Can I copy the code in Linux notepad such that I don't need to write it in terminal?
    What is the debug process (terminal code) then?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    And also, do I need to enter these lines below "%}": "%%

    <<EOF>> puts("show some code!"), exit(0);"?
  8. #5
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,835
    Rep Power
    509
    I have presented a flex program which, when run, and presented with ^d (end of file) displays the message

    show some code!

    The message is intended to tell you, llll, to show your flex program. Or ask a more specific question.

    You might have problems with your c code which could be runtime as divide by zero or access error, link stage errors such as you forgot to link with the math library, compile time such as syntax error, and you might have problems with flex syntax itself such as invalid regular expression.

    Seems that you need to review the flex manual since you don't seem to know that
    %{
    /* c code included verbatim */
    %}

    https://ftp.gnu.org/old-gnu/Manuals/.../flex_toc.html
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,835
    Rep Power
    509
    And for goodness sakes, maintain source files with a reasonable editor. Then you can get into repositories for version control. The flex scanner has to be quite simple and not intended for reuse for me to use stdin as the flex source. Learn gawk.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    Could you please show the debugging process on an example step by step, or give a reference where debugging is explained.
    I know that the compilation of LEX/FLEX on Linux goes as following:
    Code:
    lex example.l
    gcc lex.yy.c -lfl
    ./a.out
    <Enter your input>
    Now, I am interested in debugging.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    The code that you have shown doesn't debug the program 'l.lex', it just compiles it as I have shown.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    This is the code for compilation on Linux, and it is the same as yours (although you say that your code is for debugging):
    Code:
    lex example.l
    gcc lex.yy.c -lfl
    ./a.out
    <Enter your input>
    Could you now give a code for debugging an 'example.l' or give a reference text or video on how to debug a sample program?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    Note that I must know how to debug LEX/FLEX 'on paper' (without using a computer).
  20. #11
  21. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,835
    Rep Power
    509
    Code:
    /*
      Read the error message.  This program is invalid flex 
    
      flex l.lex
      "l.lex", line 11: premature EOF
    */
    
    
    
    pattern puts("action");

    Finding the c source error can be harder. The compiler clearly states missing semi-colon, but the reported line number is 100 lines removed from the problem. Inspect c blocks of the flex source. If inspection doesn't reveal the problem, bisect your program. That is, delete (or comment away) blocks of c until you find the egregious part. The -L flex option was not helpful.
    Code:
    /*
      Read the error message.  This program contains invalid c, missing semi colon.
    
      flex l.lex && gcc -Wall lex.yy.c
      l.lex: In function ‘yylex’:
      lex.yy.c:540:18: error: expected ‘;’ before ‘break’
       #define YY_BREAK break;
                        ^
      l.lex:24:2: note: in expansion of macro ‘YY_BREAK’
      At top level:
      lex.yy.c:1033:12: warning: ‘input’ defined but not used [-Wunused-function]
       static int input()
                  ^~~~~
      lex.yy.c:987:13: warning: ‘yyunput’ defined but not used [-Wunused-function]
       static void yyunput( int c, register char *yy_bp )
                   ^~~~~~~
    */
    
    
    %%
    
    pattern puts("action")

    Here is missing library:
    Code:
    /*
      Read the error message.  Missing -lfl for linkage.
    
      flex l.lex && gcc -Wall lex.yy.c 
      lex.yy.c:1030:12: warning: ‘input’ defined but not used [-Wunused-function]
       static int input()
                  ^~~~~
      lex.yy.c:984:13: warning: ‘yyunput’ defined but not used [-Wunused-function]
       static void yyunput( int c, register char *yy_bp )
                   ^~~~~~~
      /usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
      (.text+0x20): undefined reference to `main'
      /tmp/ccFQQMN8.o: In function `yylex':
      lex.yy.c:(.text+0x3b8): undefined reference to `yywrap'
      /tmp/ccFQQMN8.o: In function `input':
      lex.yy.c:(.text+0xcb3): undefined reference to `yywrap'
      collect2: error: ld returned 1 exit status
    
    */
    
    %%
    
    . {
        puts("hi world");
        yyterminate();
      }

    Badly written patterns which run a long time. I've never done this. .*.*.*; is a bad pattern.


    Code:
    /*
      Read the error message.  Run time trouble.  Browse your source.  If you can't spot the problem, compile with -g and use gdb .
    
      $ flex l.lex && gcc -Wall lex.yy.c -lfl
      lex.yy.c:1035:12: warning: ‘input’ defined but not used [-Wunused-function]
       static int input()
                  ^~~~~
      lex.yy.c:989:13: warning: ‘yyunput’ defined but not used [-Wunused-function]
       static void yyunput( int c, register char *yy_bp )
                   ^~~~~~~
      $ ./a.out
      a really
      bad
      Segmentation fault (core dumped)
      $ 
    
    */
    
    %%
    
    bad *(char*)NULL = 'n';
    . ;
    \n ;



    Now you've got a working program. Run your unit tests. Do the patterns work? You can often tell which patterns fail, although it could be a pattern order problem.
    Does the c code do what it should? Fix it.
    [code]Code tags[/code] are essential for python code and Makefiles!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    But I only want to debug LEX/FLEX and not the C part of it. Is that possible?
    And by debugging I don't mean fixing errors, but going through LEX/FLEX code line by line and analyze it.
    Is that possible?
  24. #13
  25. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,835
    Rep Power
    509
    You can compile with -d however you could read the flex man page, and you won't get far the c code is invalid.
    Code:
           -d     makes the generated scanner run in debug mode.  Whenever a pattern is recognized and the global yy_flex_debug is non-zero (which
                  is the default), the scanner will write to stderr a line of the form:
    
                      --accepting rule at line 53 ("the matched text")
    
                  The line number refers to the location of the rule in the file defining the scanner (i.e., the file that was fed to flex).  Mes‐
                  sages are also generated when the scanner backs up, accepts the default rule, reaches the end of its input buffer (or encounters
                  a NUL; at this point, the two look the same as far as the scanner's concerned), or reaches an end-of-file.
    And, frankly, to complicate things a little more I sometimes write my flex/bison programs in m4 which adds another layer of potential errors.
    [code]Code tags[/code] are essential for python code and Makefiles!
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    Where should I then put "-d" in this code (at which line?) to be able to debug LEX/FLEX?
    Code:
    lex example.l
    gcc lex.yy.c -lfl
    ./a.out
    <Enter your input>
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    23
    Rep Power
    0
    And also, how to generate "main.c" code of given LEX/FLEX? The Linux code that I have shown above,
    l
    Code:
    ex example.l
    gcc lex.yy.c -lfl
    ./a.out
    <Enter your input>
    generates only the "lex.yy.c" code, and not the "main.c".
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo