next up previous contents
Next: Scanner - the input Up: Bison and Flex Previous: Bison   Contents

Flex and Bison working together

One of the main uses of the Flex is as a companion to the Bison parser-generator. Bison 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 the Flex with the Bison, one specifies the -d option to the Bison to instruct it to generate the file *.tab.h containing definitions of all the tokens appearing in the Bison input. This file is then included in the flex scanner. For example, if one of the tokens is ``INTEGER'', part of the scanner might look like:

     %{
     #include "ogener.tab.h"
     %}
     DIGIT        [0-9]
     INTEGER      {DIGIT}+
     %%
     {INTEGER}    {yylval=atoi(yytext) ; return INTEGER;}

Please note that the scanner have to return a token code, in this case it is the INTEGER, defined in ogener.tab.h (This example is a part of the scanner used by the Controller).



Igor Wojnicki 2001-02-21