Flex(Lex,不是actionscript或其他)错误

Cam*_*Cam 0 windows lex flex-lexer

我刚开始弯曲.

使用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
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的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;
}
Run Code Online (Sandbox Code Playgroud)

同样,为什么我必须在lex语法代码中加入'main'函数?我想要的是能够打电话给yylex(); 来自另一个c文件.

Jon*_*ler 5

Q1链路错误

它看起来有点像是对isatty()函数感到困惑.它没有显示在您显示的代码中 - 但它可能会在flex生成的代码中引用.如果是这样,似乎您正在使用C++编译器进行编译,并且isatty()函数被视为具有C++链接的函数并且未被发现 - 它通常是具有C链接的函数,并且需要使用' extern "C" int isatty(int);'在C++代码中.

要解决,请跟踪是否isatty()出现在生成的C中.如果是,还要跟踪声明它的位置(POSIX标准标题为<unistd.h>).

Q2主要

您不必将主程序放在带有词法分析器的文件中.实际上,你经常不会这样做,或者那里的主程序只是一个用于独立测试词法分析器的虚拟程序(并且只在条件上编译成代码 - 内部#ifdef TEST / #endif或等效).

是什么让你认为你必须这样做?