4
votes

I am writing a program using flex that takes input from a text file and splits them into some tokens like identifier, keywords, operators etc. My file name is test.l. I have made another hash table program which includes a file named SymbolTable.h . Is there any way to include this header file in my test.l file so that I can make some operations (for example: inserting the identifiers into the hash table)while reading the input? I have already tried to include it but when I try to compile using gcc lex.yy.c -lfl , it generates an error message saying:

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

Please help me on how to include the header file or in any other way I can do the desired operation I stated above.

3
You say that your header file is SymbolTable.h and later that the error says SymbolInfo.h. That sounds like you've put the wrong file name. It would be useful if you include in your answer so more details, including an excerpt from the test.l file where the header file is included.rici
sorry, edited. that was another file name I had mistakenly typed. Now check the post please @riciSKB
I include the headers file in my lexer all the time and it was never a problem to use declared structures from them... Are you sure, like rici suggested, its not a path issue?W.F.
yap. I have edited the post @ Wojciech FrohmbergSKB

3 Answers

2
votes

It seems your issue is not with flex, but with how the C language handles the 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 you should have

#include "SymbolTable.h"

As you did not show the actual code you used it was hard to properly answer.

1
votes

For simple application you can use a little template of bison/yacc project of mine.

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

Scanner uses the header file generated from bison in this example. But surely you can include in the same place some other header file.

To compile your lexer just type make scan or just make to compile lexer and grammar at the same time.

Have a good luck in your lexer journey!

1
votes

Your include statement worked just fine. The compiler couldn't find your file because it is not in the include search path. Try adding -I. when invoking gcc. Like : gcc -I.