Page 2 of 2 First 12
  • Jump to page:
    #16
  1. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    please read the manual at this core utils link

    As we can see, libfl contains main and yywrap.
    Code:
    $ strings /usr/lib/libfl.a
    !<arch>
    /               1328501519  0     0     0       24        `
    (main
    yywrap
    libmain.o/      1328501519  0     0     100664  1424      `
    GCC: (Ubuntu/Linaro 4.6.2-12ubuntu1) 4.6.2
    .symtab
    .strtab
    .shstrtab
    .text
    .data
    .bss
    .rela.text.startup
    .comment
    .note.GNU-stack
    .rela.eh_frame
    main
    yylex
    libyywrap.o/    1328501519  0     0     100664  1184      `
    GCC: (Ubuntu/Linaro 4.6.2-12ubuntu1) 4.6.2
    .symtab
    .strtab
    .shstrtab
    .text
    .data
    .bss
    .comment
    .note.GNU-stack
    .rela.eh_frame
    yywrap
    Here's an example with replaced main function, as well as using the -d flex option. Please read https://www.gnu.org/software/coreuti...ode/index.html before you go further.
    Code:
    %{
      /*
    
        $ flex -d l.lex
        $ gcc lex.yy.c -lfl
        $ cat << EOF | ./a.out
        > hi
        > that's all folks
        > EOF
        custom main
        --(end of buffer or a NUL)
        --accepting rule at line 3 ("hi")
        Gaea
        --accepting default rule ("
        ")
    
        --accepting default rule ("t")
        --accepting default rule ("h")
        --accepting default rule ("a")
        --accepting default rule ("t")
        --accepting default rule ("'")
        --accepting default rule ("s")
        --accepting default rule (" ")
        --accepting default rule ("a")
        --accepting default rule ("l")
        --accepting default rule ("l")
        --accepting default rule (" ")
        --accepting default rule ("f")
        --accepting default rule ("o")
        --accepting default rule ("l")
        --accepting default rule ("k")
        --accepting default rule ("s")
        --accepting default rule ("
        ")
        that's all folks
        --(end of buffer or a NUL)
        --EOF (start condition 0)
        $ 
      */
    %}
    
    %%
    
    hi puts("Gaea");
    
    %%
    
    int main() {
      puts("custom main");
      return yylex();
    }
    Last edited by b49P23TIvg; August 5th, 2017 at 09:16 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    24
    Rep Power
    0
    Thanks. But I still don't understand how to actually debug a flex program (I don't understand what is the use of a clause "accepting default rule at line X").
    Could you explain to me, without using a computer (manually, work paper) the process of going through flex code instruction by instruction (debugging), and figure out the output
    for the given input?

    Example flex program:
    Code:
    	int counter=0;
    derp [A-Za-z]{3}[ \.]+
    %%
    {derp} if (yyleng==4) return; if (counter++) yyless(1); else { REJECT;}
    . ECHO;
    Input:
    Code:
    Magna turba in via est.
    Output:
    Code:
    Matuin
    If you can, please explain this code step by step without using a computer.
    If you can, explain what is the use of clause "accepting default rule at line X" (how does it helps in debugging?)?
  4. #18
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    and what is that default rule? what is a rule?
    wow.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    24
    Rep Power
    0
    Could you explain the above flex program (by debugging it)?
  8. #20
  9. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    We'd need to know what REJECT and yyless do. I've never used them. Well I understood REJECT, which as you know means, essentially, keep scanning to find the next matching rule.

    And yyless(n) means to push back onto the input all but n characters then continue scanning. However I was confused about whether those n characters are part of current token, but the example in the manual displays foobarbar not foobarfoobar... and I realize that the match must start afresh otherwise the same input would keep matching.

    Now, what, please, is the default rule?
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #21
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    24
    Rep Power
    0
    I don't know what is the default rule. Should it be given? Is it defined somewhere in "lex.yy.c" code? Do you know what is the default rule? Could you be more detailed in debugging this code?
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    24
    Rep Power
    0
    Is the given regular expression the default rule? It matches "est." in the given input. But I don't understand how does regular expression affects on output. Could you be more detailed in debugging this code?
  14. #23
  15. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    I absolutely do know the default rule for flex. Surprisingly, it is documented in the flex man page.
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #24
  17. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    At any rate, the answer to your question is no, I could not understand the output without additional experiments.

    I encountered two problems.
    • The return statement does not provide a value, yet yylex is to return int. And while I know that the gnu c compiler gnu standard returns the last value computed in such cases, I was unaware of the c standard for this situation. And I'm still not, although I compiled the program with several gcc -std options and they all behaved as with gnu. If we treat all warnings as errors the program does not compile. gcc -Werror lex.yy.c -lfl
    • Secondly, I did not realize that the main function in libfl.a seems to be
      Code:
      int main() {
        while (yylex())
          ;
        return 0;
      }
      whereas I had thought it was as if %option main were declared.
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #25
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    24
    Rep Power
    0
    But I should find the output for this program "on paper". Do you understand the output without using a computer?
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2017
    Posts
    24
    Rep Power
    0
    Also, could you tell what is the default rule?
  22. #27
  23. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,888
    Rep Power
    509
    You've got some homework. Read the manual.
    [code]Code tags[/code] are essential for python code and Makefiles!
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo