未定义对`yylex'的引用&&未定义对`yyin'的引用

Xu Wang

我正在尝试使用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

这是我的makefile:

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建议使用,将定义添加到lexer声明块中:

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章