cflex-lexer

Generated header file does not include user-defined files


Currently trying to create a lexer with Flex. When running the command flex pl0_lexer.l, the include statements in my pl0_lexer.h do not match my pl0_lexer.c.

pl0_lexer.l Snippet

%option header-file = "pl0_lexer.h"
%option outfile = "pl0_lexer.c"
%option yylineno
%option bison-bridge

%{
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include "ast.h"
#include "parser_types.h"
#include "utilities.h"
#include "lexer.h"

 /* Tokens generated by Bison */
#include "pl0.tab.h"
%}

My problem is that all of the user-defined headers like "ast.h" appear in the .c code, but pl0_lexer.h only includes standard C headers.


Solution

  • As I noted in a comment:

    That's because the contents of pl0_lexer.h don't need any other headers to compile. The header is minimal — your code (outside the lexer) has to include the headers necessary. You can't rely on pl0_lexer.h to include those headers.

    The generated header provides the definitions necessary to be able to use the lexer — primarily an appropriate declaration for yylex(), but also for the supporting functions. It doesn't need to know anything about the information in the user-defined headers — those do not form a part of the interface to the lexical analyzer.

    See also Should I use #include in headers? for a discussion of self-contained, idempotent, minimal headers, and also How to link multiple implementation files in C?.

    (As an aside, the generated header doesn't meet my definition of 'minimal'. For example, it ensures that type names like flex_int8_t and flex_uint64_t are defined, but (with Flex 2.6.4) the structure defined in the header does not use these types, nor does anything else. All that code is not directly needed in the header. I'm not sure where it is documented that consumers of the lexical analyzer must (or should) use those types.)