1

I have a pretty standard .l file for flex to to process.

scanner.l

%{
#include "sFile.h"
%}

%%
"BEGIN"         return BG;
"END"           return END;
"And so on...."
%%

int yywrap()
{
     return 1
}

So I hit this with a flex -+ scanner.l and like magic a lex.yy.cc file shows up.

I turn to my trusty c++ compiler and g++ lex.yy.cc just to give it a go and I get

lex.yy.cc: In member function 'virtual int yyFlexLexer::yylex()':
lex.yy.cc:868: error: 'yywrap' was not declared in this scope
lex.yy.cc: In member function 'int yyFlexLexer::yyinput()':
lex.yy.cc:1271: error: 'yywrap' was not declared in this scope

I've also tried modifying the scanner.l to use %option c++ and a few other things but I can't for the life of me figure out how to get this thing to compile.

The end result is is using this with an external .cpp file to do some other work for the assignment.

edit: Versions g++ 4.1.1 flex 2.5.33

edit for dupe clarification: I'm not using bison, we have to write the parse step on our own without a generator(Like bison). Each of the other related questions have this problem entangled with another tool. I'm terribly weak in this area as a student so I am having trouble translating what bison needs and what my cpp file needs.

However %option noyywrap did clarify/fix that problem but really just opened up a whole different can of worms.

Perhaps it would be best if I just restated the intent.

I need to run a flex scanner on an input file. I need to parse that output in my own .cpp file. My c is super weak and there isn't enough time in the project for our group to do it in c.

It's important that I have access to

extern int yylex();
extern int yylineno;
extern char* yytext;
extern FILE* yyin;

In my .cpp parser. All of which seem to error when compiled. -lfl is giving me an error

g++ lex.yy.cc program.cpp -lfl
ld: fatal: library -lfl: not found
ld: fatal: File processing errors. No output written to a.out

ninja edit: I found the flex libs

$ g++ lex.yy.cc -L /usr/local/lib -lfl using this question but I am still getting the same error. error: 'yywrap' was not declared in this scope

I've also tried to compile lex.yy.cc on its own with the same error.

Trying to get to a simpler state I also removed the yywrap definintion from scanner.l

and replaced with

int main()
{
yylex();
}

Where I received the following error.

Undefined                       first referenced
 symbol                             in file
yylex()                             /var/tmp//ccjy7kng.o

So then I took all of that noise out and simply put a int main(){return 1;} and it compiled. But when I tried to compile with my .cpp parser

ld: fatal: symbol `main' is multiply-defined:
Community
  • 1
  • 1
Bmo
  • 1,212
  • 11
  • 34
  • Flex, by default, generates C code, not C++. Use the --c++ option to generate C++ code from flex. – Sam Varshavchik Oct 04 '15 at 03:15
  • I've tried both of the options and got a .cc file out. When I try to compile against that I'm getting those errors. – Bmo Oct 04 '15 at 03:17
  • Examining the output of flex 2.5.37: it generates yyFlexLexer::yylex() which call yyFlexLexer::yywrap(), which is defined in FlexLexer.h, which #include-d from lex.yy.cc I'm guessing that the older flex does not declare a yywrap() method in the yyFlexLexer class, so it becomes a global function call. You need to declare int yywrap(); in the initialization section, within a %{ ... %} block, probably, so that this function gets declared before its used in the code. – Sam Varshavchik Oct 04 '15 at 05:24
  • 1
    Possible duplicate of [Undefined Reference To yywrap](http://stackoverflow.com/questions/1811125/undefined-reference-to-yywrap) – wilx Oct 04 '15 at 08:05
  • @wilx I updated the question. I checked the dupe, I think it moved me forward but I am still stuck. Thank you. – Bmo Oct 04 '15 at 15:05

0 Answers0