Go to the first, previous, next, last section, table of contents.

Interfacing with Parser Generators

One of the main uses of flex is as a companion to parser generators like yacc. yacc parsers expect to call a routine named yylex to find the next input token. The routine is supposed to return the type of the next token as well as putting any associated value in the global yylval. To use flex with yacc, specify the `-d' option to yacc to instruct it to generate the file y.tab.h containing definitions of all the %tokens appearing in the yacc input. Then include this file in the flex scanner. For example, if one of the tokens is `TOK_NUMBER', part of the scanner might look like:

%{
#include "y.tab.h"
%}

%%

[0-9]+        yylval = atoi( yytext ); return TOK_NUMBER;


Go to the first, previous, next, last section, table of contents.