By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,116 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,116 IT Pros & Developers. It's quick & easy.

flex and C++

P: 2
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
Expand|Select|Wrap|Line Numbers
  1. %option noyywrap yylineno c++
  2. %{
  3.         #include <iostream>
  4. %}
  5. %%
  6. "<"[A-Za-z]*">" { std::cout << YYText(); }
  7. %%
  8. int main () {
  9.     yylex();
  10.     return 0;
  11. }
Execution
Expand|Select|Wrap|Line Numbers
  1. $ flex test.l
  2. $ gcc -o test lex.yy.cc 
  3. test.l: In function 'int main()':
  4. test.l:10: error: 'yylex' was not declared in this scope
Mar 24 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
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()
Expand|Select|Wrap|Line Numbers
  1.  void     yylex(void);
  2.  
Mar 24 '07 #2

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

Like this:
Expand|Select|Wrap|Line Numbers
  1. int main() {
  2.         FlexLexer* lexer = new yyFlexLexer();
  3.         while (lexer->yylex() != 0)
  4.                 ;
  5.         return 0;
  6. }

Solved, thanks.
Mar 24 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.