flex and C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madmaxptz
    New Member
    • Mar 2007
    • 2

    flex and C++

    I was trying to get a simple program to work with flex and C++. I can't figure out what's wrong though. With C it works, and I can execute yyflex() with no problems and the analyzer gets executed. With C++ on the other hand...
    Take a look:

    Code
    Code:
    %option noyywrap yylineno c++
    %{
            #include <iostream>
    %}
    %%
    "<"[A-Za-z]*">" { std::cout << YYText(); }
    %%
    int main () {
        yylex();
        return 0;
    }
    Execution
    Code:
    $ flex test.l
    $ gcc -o test lex.yy.cc 
    test.l: In function 'int main()':
    test.l:10: error: 'yylex' was not declared in this scope
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    C++ needs to know the function header (name, number and type of parameters) before it can call it otherwise it gives an error message such as the one you had (in C this not an error). possibly try adding the function prototype before main()
    Code:
     void     yylex(void);

    Comment

    • madmaxptz
      New Member
      • Mar 2007
      • 2

      #3
      The problem was that C++ needed a FlexLexer object, and then execute the yylex() function through it.

      Like this:
      Code:
      int main() {
              FlexLexer* lexer = new yyFlexLexer();
              while (lexer->yylex() != 0)
                      ;
              return 0;
      }

      Solved, thanks.

      Comment

      Working...