Search code examples
macosflex-lexer

Using Flex (the lexical analizer) on OS X


I've got a file, test.lex, which I run through as

$ flex test.lex

That gives me lex.yy.c, which I try to compile with:

$ gcc lex.yy.c -lfl

This gives me the error ld: library not found for -lfl. I know the Flex specification is correct and lex.yy.c compiles fine on a Linux machine. Any suggestions?

Edit: I'm using the flex supplied by Apple.


Solution

  • Some systems make libfl a separate package from flex, as it is rarely needed. The libfl library just contains two functions:

    int main() {
        while (yylex());
        return 0;
    }
    
    int yywrap() {
        return 1;
    }
    

    Normally you'll want your own main function rather than the one from libfl, and defining yywrap yourself is trivial. Alternately, you can use %option noyywrap and not need it at all.

    In your case, try just getting rid of the -lfl option. If you get an error about yywrap, add
    %option noyywrap to the first section of your test.lex file.