Including an external header file in Flex

I am writing a program using flex that takes input from a text file and breaks it down into some markers like id, keywords, statements, etc. My filename is test.l. I made another hash table program that includes a file called SymbolTable.h. Is there a way to include this header file in the test.l file so that I can perform some operations (ex: inserting ids into a hash table) while reading the input? I've already tried to enable it, but when I try to compile with gcc lex.yy.c -lfl

it generates an error:

"fatal error: SymbolTable.h: no such file or directory."

Please help me on how to include the header file or in some other way I can perform the desired operation mentioned above.

+3


source to share


3 answers


For a simple application, you can use the small bison / yacc project template.

gram.y

%{
#include <stdio.h>
#include <stdlib.h>

%}
// tokens here


%%
// rules here


%%

int main() {
   return yyparse();
}

      

scan.l:

%{
#include <stdio.h>

//include a YACC grammar header
//#include "gram.h"
%}
%%
. /*LEX RULES*/;

%%
// C code
int yywrap() {
   return 1;
}

      



Makefile:

all: gram

gram: gram.c gram.h scan.c
    gcc gram.c scan.c -lfl -ly -o gram
gram.c: gram.y
    yacc -d gram.y -o gram.c
scan.c: scan.l
    flex -o scan.c scan.l

scan: scan.c
    gcc scan.c -lfl -o scan

      

In this example, the scanner uses a header file generated from bison. But of course you can include another header file in the same place.

To compile your lexer just type make scan

or just make

to compile lexer and grammar at the same time.

Good luck with the Lexer's journey!

+1


source


Your include statement works pretty well. The compiler could not find your file because it is not in the include search path. Try adding -I. when calling gcc. For example: gcc -I.



+1


source


Your problem seems to be not with flex, but with the way the C language handles different syntax. This is explained in this question: What is the difference between #include <filename> and #include "filename"? ...

You probably have:

#include <SymbolTable.h>

      

when should you have

#include "SymbolTable.h"

      

Since you weren't showing the actual code you were using, it was difficult to answer correctly.

+1


source







All Articles