在Flex中包含外部头文件

时间:2021-04-22 09:41:56

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:

我正在编写一个程序,使用flex从文本文件中获取输入并将它们分成一些标记,如标识符,关键字,运算符等。我的文件名是test.l.我创建了另一个哈希表程序,其中包含一个名为SymbolTable.h的文件。有没有办法在我的test.l文件中包含这个头文件,以便我可以在读取输入时进行一些操作(例如:将标识符插入哈希表)?我已经尝试包含它但是当我尝试使用gcc lex.yy.c -lfl进行编译时,它会生成一条错误消息:

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

“致命错误:SymbolTable.h:没有这样的文件或目录。”

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 个解决方案

#1


1  

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

对于简单的应用程序,您可以使用我的bison / yacc项目的一个小模板。

gram.y

gram.y里

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

%}
// tokens here


%%
// rules here


%%

int main() {
   return yyparse();
}

scan.l:

scan.l里:

%{
#include <stdio.h>

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

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

Makefile:

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.

在此示例中,扫描程序使用从bison生成的头文件。但肯定你可以在同一个地方包含一些其他头文件。

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

要编译你的词法分析器,只需输入make scan或make make来同时编译词法分析器和语法。

Have a good luck in your lexer journey!

在你的lexer之旅中祝你好运!

#2


1  

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.

你的包含声明工作得很好。编译器找不到您的文件,因为它不在包含搜索路径中。尝试添加-I。在调用gcc时。喜欢:gcc -I。

#3


1  

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"?.

看来你的问题不在于flex,而在于C语言如何处理不同的语法。这个问题在这个问题中解释:#include 和#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


1  

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

对于简单的应用程序,您可以使用我的bison / yacc项目的一个小模板。

gram.y

gram.y里

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

%}
// tokens here


%%
// rules here


%%

int main() {
   return yyparse();
}

scan.l:

scan.l里:

%{
#include <stdio.h>

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

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

Makefile:

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.

在此示例中,扫描程序使用从bison生成的头文件。但肯定你可以在同一个地方包含一些其他头文件。

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

要编译你的词法分析器,只需输入make scan或make make来同时编译词法分析器和语法。

Have a good luck in your lexer journey!

在你的lexer之旅中祝你好运!

#2


1  

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.

你的包含声明工作得很好。编译器找不到您的文件,因为它不在包含搜索路径中。尝试添加-I。在调用gcc时。喜欢:gcc -I。

#3


1  

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"?.

看来你的问题不在于flex,而在于C语言如何处理不同的语法。这个问题在这个问题中解释:#include 和#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.

由于您没有显示您使用的实际代码,因此很难正确回答。