我这里有我的莱克斯档案。
%{
#include <stdio.h>
%}
%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%
我是用flex
编译的。但是当我使用gcc
编译lex.yy.c
时出现了一个错误。它说..。
: undefined reference to yywrap
我使用了gcc lex.yy.c -lfl
,但仍然有一个错误。它说..。
ld.exe: cannot find -lfl
请帮我编译我的lex.yy.c
文件。非常感谢。
发布于 2014-07-24 12:52:26
有几种方法。我推荐的是。
提供您自己的yywrap()函数。
因为我的编译器通常允许多个源文件,所以我更喜欢定义我自己的yywrap()。我是用C++编译的,但重点应该很明显。如果有人使用多个源文件调用编译器,我会将它们存储在一个列表或数组中,然后在每个文件的末尾调用yywrap(),让您有机会继续处理新文件。注意: getNextFile()或编译器->getNextFile()是我自己的函数,不是flex函数。
int yywrap() {
// open next reference or source file and start scanning
if((yyin = getNextFile()) != NULL) {
line = 0; // reset line counter for next source file
return 0;
}
return 1;
}
如果使用C++构建,请使用:
extern "C" int yywrap() {
// open next reference or source file and start scanning
if((yyin = compiler->getNextFile()) != NULL) {
line = 0; // reset line counter for next source file
return 0;
}
return 1;
}
对于较旧的lex/flex,yywrap()是一个宏,因此要重新定义它,我们必须这样做:
%{
#undef yywrap
%}
您可能会重新定义一个简单的宏,它将在单个文件之后结束扫描:
%{
#undef yywrap
#define yywrap() 1
%}
使用较新的Flex / POSIX lex,您可以在命令行中完全禁用yywrap:
flex --noyywrap
这适用于除yywrap (--noyymore等)之外的其他函数。
或者将其放入选项部分。
%option noyywrap
/* end of rules */
%%
其中lex文件的常规语法为:
Options and definitions
%%
Rules
%%
C code
https://stackoverflow.com/questions/24925247
复制相似问题