c++ - 对 `yylex' 的 undefined reference && 对 `yyin' 的 undefined reference

标签 c++ makefile bison flex-lexer

我正在尝试使用 flex 和 bison 来创建一个编译器。但是当我尝试编译时收到错误。
这是错误:

flex --header-file=lex.h --outfile=lex.cc lex.ll
g++ -w -Wall -Wextra -ansi -g -c -o lex.o lex.cc -lm -ll
bison -v --output-file=parse.cc --defines=parse.hh --warnings=all --feature=all parse.yy
g++ -w -Wall -Wextra -ansi -g   -c -o parse.o parse.cc
g++ -w -Wall -Wextra -ansi -g   -c -o main.o main.cc
g++ -w -Wall -Wextra -ansi -g -fkeep-inline-functions -o LFIO lex.o parse.o function.o main.o  -lfl -lm -ll
parse.o: In function `yyparse()':
/root/LFIO-combine/parse.cc:1199: undefined reference to `yylex'
main.o: In function `main':
/root/LFIO-combine/main.cc:21: undefined reference to `yyin'
/root/LFIO-combine/main.cc:21: undefined reference to `yyin'
collect2: error: ld returned 1 exit status
make: *** [LFIO] Error 1

这是我的生成文件:

CXXFLAGS = -w -Wall -Wextra -ansi -g
OBJECTS = lex.o parse.o function.o main.o


LFIO : $(OBJECTS)
        g++ $(CXXFLAGS) -fkeep-inline-functions -o LFIO $(OBJECTS) -lfl -lm -ll

clean :
        rm -f *.o

lex.cc : lex.ll
        flex --header-file=lex.h --outfile=lex.cc lex.ll

parse.hh : parse.cc

parse.cc : parse.yy function.h algorithm.h
        bison -v --output-file=parse.cc --defines=parse.hh --warnings=all --feature=all parse.yy

%.o: %.cc %.h
        g++ $(CXXFLAGS) -c -o $@ $< -lm -ll

main.o : main.cc lex.h parse.hh function.h algorithm.h

function.o : function.h function.cc

parse.o : parse.cc parse.hh function.h algorithm.h

lex.o :lex.h algorithm.h function.h

这是我的 lex.ll

%option c++
%option ecs
%option nodefault
%option stack
%option warn
%option yylineno

%{
#include "function.h"
#include "parse.hh"
#include <limits.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int yyFlexLexer::yywrap() { return 1; }
%}

ATOM    [A-Z][a-zA-Z0-9_']*
VARIABLE        [a-z][a-zA-Z0-9_']*

%%
[ \t\n]         ;
{VARIABLE} {  return (TOK_VARIABLE);}
{ATOM} {  return (TOK_ATOM);}
":-" {}
","  {}
"."  {}
<<EOF>>  {yyterminate();}
%%

parse.yy 是这样的:

%defines
%define api.token.prefix {TOK_}

%{
#include "function.h"
#include "lex.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#ifdef MS_WINDOWS
#include <malloc.h>
#define alloca_alloca
#endif
void yyerror(const char *s)
{
   fprintf (stderr, "%s\n", s);
}
extern "C" int yylex(void);

%}

%union{
......
}

%type ......


%%
...... 

然后main.cc是这样的:

#include "function.h"
#include "algorithm.h"
#include "parse.hh"
#include "lex.h"
extern "C" int yylex(void);
extern FILE *yyin;
extern FILE *yyout;
extern int fopen();
extern int yyparse(void *);

int main(int argc, char **argv){

if(argc > 1){
if(!(yyin = fopen(argv[1], "r"))) {
perror(argv[1]);
return (1);
}
}
//yylex();
yyparse();
......
return 0;
}

有谁知道是什么原因导致这些错误以及如何解决这些错误? 如果有人能抽出时间检查这些错误,我将不胜感激。

最佳答案

将我的评论收集到一个答案中。

有两个问题:

首先是代码被编译为 C++,这意味着 yylex 的定义将被编译为 C++。这意味着它不是 extern "C"。要解决此问题,请从所有声明中删除 extern "C"

第二个问题有两个方面:第一个是符号yyin好像没有在任何地方定义,所以你需要自己做。第二件事是它应该是指向 std::istream 对象的指针,而不是 FILE *

要解决第二个问题,使用 yyin,我建议您将定义添加到词法分析器声明 block 中:

%{
// Your includes...
int yyFlexLexer::yywrap() { return 1; }

// Add definition of yyin
std::istream* yyin;
%}

然后更新 main.cc 文件中的 extern 声明以匹配定义。

关于c++ - 对 `yylex' 的 undefined reference && 对 `yyin' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052715/

相关文章:

c++ - 读取存储在多重映射中的私有(private)枚举作为值

bison - 在给定上下文无关语法的情况下查找模棱两可的语句

c++ - Bison 在错误的行后输出字符串

c - 在 Bison 中使用字 rune 字作为终端

c++ - Clang 8 与 MinGW-w64 : How do I use address- & UB sanitizers?

c++ - 引用和数组索引之间有什么区别?

c++ - 64 位构建中 STL/OpenMP 的奇怪并发问题

linux - 使用 linux csh 脚本在许多子目录中执行 makefile

c++ - 使用 GCC 依赖文件导致 Make 每次都完全重建项目

c - Mingw 和 make 变量