Common tasks with Flex

This page describes how to do some common tasks with Flex, a tool for generating programs that perform pattern-matching on text.

Suppress "yyunput defined but not used"

To suppress the warning message "yyunput defined but not used" when compiling the flex output with gcc -Wall, use a line

%option nounput
in the options section (the part after %}).

Make flex generate line numbers

To make flex generate line numbers, use a line

%option yylineno
in the options section (the part after %}). This makes a global variable, yylineno, available. You need to set yylineno to the first line of the file being read.

See also the Flex manual on the prefix option.

Make flex create a header file declaring yylex

To make flex generate a header file with a declaration of a prototype for yylex, use the --header-file option,

flex --header-file=my.lex.h my.l
where my.l is the name of your input file, and my.lex.h is the name of the header file you want to create.

Change the yy prefix to something else

To change the prefix used on all of Flex's functions, use a line

%option prefix="myprefix_"
in the options section (the part after %}). This changes everything; yylex becomes myprefix_lex, yylineno becomes myprefix_lineno, etc.

See also the Flex manual on the prefix option.

Stop the "undefined reference to `yywrap'" error

To stop the error "undefined reference to `yywrap'" from Flex, use

%option noyywrap
in the options section (the part after %}), or link with the Flex library, -lfl, to use the default yywrap function which comes with Flex.

Stop the "warning: 'yy_top_state' defined but not used" error

To stop the error message "warning: 'yy_top_state' defined but not used", use

%option noyy_top_state
in the options section.


Copyright © Ben Bullock 2009-2022. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. / Privacy / Disclaimer