Flex(Lex,不是动作脚本或其他)错误
我对 Flex 完全陌生。
我在使用 Flex 时遇到构建错误。也就是说,我使用 flex 生成了一个 .c 文件,并且在运行它时收到此错误:
1>lextest.obj : error LNK2001: unresolved external symbol "int __cdecl isatty(int)" (?isatty@@YAHH@Z)
1>C:\...\lextest.exe : fatal error LNK1120: 1 unresolved externals
这是我正在使用的 lex 文件(从 此处):
/*** Definition section ***/
%{
/* C code to be copied verbatim */
#include <stdio.h>
%}
/* This tells flex to read only one input file */
%option noyywrap
%%
/*** Rules section ***/
/* [0-9]+ matches a string of one or more digits */
[0-9]+ {
/* yytext is a string containing the matched text. */
printf("Saw an integer: %s\n", yytext);
}
. { /* Ignore all other characters. */ }
%%
/*** C Code section ***/
int main(void)
{
/* Call the lexer, then quit. */
yylex();
return 0;
}
同样,为什么我必须在 lex 语法代码中放置一个“main”函数?我想要的是能够调用 yylex();来自另一个c文件。
I'm totally new to flex.
I'm getting a build error when using flex. That is, I've generated a .c file using flex, and, when running it, am getting this error:
1>lextest.obj : error LNK2001: unresolved external symbol "int __cdecl isatty(int)" (?isatty@@YAHH@Z)
1>C:\...\lextest.exe : fatal error LNK1120: 1 unresolved externals
here is the lex file I'm using (grabbed from here):
/*** Definition section ***/
%{
/* C code to be copied verbatim */
#include <stdio.h>
%}
/* This tells flex to read only one input file */
%option noyywrap
%%
/*** Rules section ***/
/* [0-9]+ matches a string of one or more digits */
[0-9]+ {
/* yytext is a string containing the matched text. */
printf("Saw an integer: %s\n", yytext);
}
. { /* Ignore all other characters. */ }
%%
/*** C Code section ***/
int main(void)
{
/* Call the lexer, then quit. */
yylex();
return 0;
}
As well, why do I have to put a 'main' function in the lex syntax code? What I'd like is to be able to call yylex(); from another c file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Q1 链接错误
看起来好像 isatty() 函数有些混乱。它不会显示在您显示的代码中 - 但它可能会在 flex 生成的代码中引用。如果是这样,则表明您正在使用 C++ 编译器进行编译,并且 isatty() 函数被视为具有 C++ 链接的函数并且未找到 - 它通常是具有 C 链接的函数,需要使用C++ 代码中的“
extern "C" int isatty(int);
”。要解决此问题,请追踪< /代码>)。
isatty()
是否出现在生成的 C 中。如果是,还要追踪它的声明位置(它的 POSIX 标准标头为Q2 Main
您不必将主程序放入带有词法分析器的文件中。事实上,您通常不会这样做,或者其中的主程序只是一个用于单独测试词法分析器的虚拟程序(并且仅有条件地编译到代码中 - 在
#ifdef TEST / #endif
或同等内容)。是什么让你认为你必须这样做?
Q1 Link Error
It looks a bit as if something is confused about the isatty() function. It doesn't show in the code you show - but it might be referenced in the code generated by flex. If so, it appears that you are compiling with a C++ compiler, and the isatty() function is being treated as a function with C++ linkage and is not being found - it is normally a function with C linkage and would need to be declared with '
extern "C" int isatty(int);
' in C++ code.To resolve, track down whether
isatty()
appears in the generated C. If so, also track down where it is declared (the POSIX standard header for it is<unistd.h>
).Q2 Main
You do not have to put the main program in the file with the lexer. Indeed, you often won't do that, or the main program in there will simply be a dummy used to test the lexer in isolation (and only compiled into the code conditionally - inside
#ifdef TEST / #endif
or equivalent).What makes you think that you have to do it?
链接错误看起来像是您尝试在 Windows 上使用非 Windows 原生版本的 Flex,但它无法正常工作。如果使用cygwin自带的flex版本,则需要使用cygwin的编译器和链接器来编译和链接程序。
The link error looks like you're trying to use a non-windows-native version of flex on windows, and its not working properly. If you use the version of flex that comes with cygwin, you need to compile and link the program with cygwin's compiler and linker.