未定义对`yylex'的引用 [英] Undefined reference to `yylex'

查看:564
本文介绍了未定义对`yylex'的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用flex和bison解析一个输入文件,但是在编译程序时遇到了一个困难.我正在附上我得到的flex和bison代码以及错误.

I am trying to parse one input file using flex and bison but I am facing one difficulty while compiling my program. I am attaching my flex and bison code and error which I am getting.

请帮助我解决这些错误

lex.l

%{
 #include <iostream>  
 #include <stdio.h>
#include "yacc.tab.h"
 #define YY_DECL extern "C" int yylex()

using namespace std;  


%}

DOT             "."  
COLON           ":"  
SEMICOLON       ";"  
COMMA           ","  
ANGLE_LEFT      "<"  
ANGLE_RIGHT     ">"  
AT              "@"  
EQUAL           "="  
SQUARE_OPEN     "["  
SQUARE_CLOSE    [^\\]"]"  
OPENBRACE       "\("  
CLOSEBRACE      "\)"  
QUOTE           "\""  
QUOTE_OPEN      "\""  
QUOTE_CLOSE     [^\\]"\""  
SPACE           " "  
TAB             "\t"  
CRLF            "\r\n"  
QUOTED_PAIR     "\\"[^\r\n]  
DIGIT           [0-9]  
ALPHA           [a-zA-Z]  
QTEXT           [0-9a-zA-Z!#$%&'()*+,\-.\/:;<=>?@\[\]^_`{|}~]  

%%

[a-zA-Z0-9]+            { yylval.sval = strdup(yytext); return TOK_STRING; }

{SPACE}*                    {return TOK_SPACE; }

{SPACE}*Name.*              {return TOK_NAME; }  
{SPACE}*SIZE.*              {return TOK_SIZE; }  
{SPACE}*ITERATE.*           {return TOK_ITERATE; }  
{SPACE}*DIRECTION.*     {return TOK_DIRECTION; }  

^{CRLF}                         { return TOK_EMPTY_LINE; }  
{CRLF}                          {}  
.                               {}/* ignore unknown chars */  

yacc.y

 %{
 #include <cstdio> 
 #include <cstring>
 #include <iostream>
 #include <stdio.h>

using namespace std;

extern "C" int yylex();  
extern "C" FILE *yyin;

void yyerror(const char* s);  


%}

%union  
{  
    char* sval;  
};  

%token <sval> TOK_NAME  
%token <sval> TOK_SIZE  
%token <sval> TOK_STRING  
%token <sval> TOK_ITERATE  
%token <sval> TOK_DIRECTION  


%token TOK_SPACE  


%%

str:  
    TOK_SPACE TOK_NAME TOK_SPACE TOK_STRING   
    {  
        cout << "Value:" << $2 << "->" << $4;  
    }  
    ;  
%%  

int main(void) {  
    FILE * pt = fopen("new file ", "r" );  
    if(!pt)  
    {  
    cout << "Bad Input.Noexistant file" << endl;  
    return -1;  
    }  
    yyin = pt;  
    do  
    {  
        yyparse();  
     }while (!feof(yyin));        
}  
void yyerror(const char *s)  
{  
   cout << "Error. " << s << endl;   
   exit(-1);     
}  

我使用以下命令构建这些文件:

I build these using:

flex bas.l 
bison -d yacc.y 
g++ lex.yy.c yacc.tab.c -lfl -o scanner.exe 

在编译程序时,我发现以下错误:

While compiling program I found following errors:

/tmp/cceIyDkD.o:在函数"main"中:
yacc.tab.c :(.text + 0x708):`main'的多个定义
/tmp/ccatq95p.o:lex.yy.c:(.text+0x1228):首先在此处定义
/usr/bin/ld:警告:符号"main"的大小从/tmp/ccatq95p.o中的86更改为/tmp/cceIyDkD.o中的120 /tmp/cceIyDkD.o:在函数yyparse()中:
yacc.tab.c :(.text + 0x2d6):未定义对yylex的引用
collect2:ld返回1退出状态

/tmp/cceIyDkD.o: In function `main':
yacc.tab.c:(.text+0x708): multiple definition of `main'
/tmp/ccatq95p.o:lex.yy.c:(.text+0x1228): first defined here
/usr/bin/ld: Warning: size of symbol `main' changed from 86 in /tmp/ccatq95p.o to 120 in /tmp/cceIyDkD.o
/tmp/cceIyDkD.o: In function `yyparse()':
yacc.tab.c:(.text+0x2d6): undefined reference to `yylex'
collect2: ld returned 1 exit status

请帮帮我

推荐答案

  1. 在parser.y中,更确切地说,在包含内容的代码的C部分中,您需要编写:extern int yylex(void);.不需要Extern关键字,因为默认情况下全局函数具有动态连接性,但是最好写一个关键字来指出该函数不是您正在使用的文件中的函数.但是,您的代码在这里没有错误.

  1. In parser.y, more precisely C part of the code where you include stuff, you need to write: extern int yylex(void);. Extern keyword is not needed, because global functions by default have dynamic connectivity, but it is good practice to write keyword to point that function is not from that file you're working. Your code has no mistake here however.

将lex.yy.c编译为目标文件,将y.tab.c编译为目标文件,然后在最终程序中将它们链接在一起. 首先执行yacc命令非常重要,因为Lex使用的是由Yacc生成的yacc.tab.h.因为弯曲它会出现错误,但您不知道是什么yacc .tab.h是.

Compile lex.yy.c to the object file, compile y.tab.c to object file and then link them together in final program. It is very important to first do yacc command, because Lex is using yacc.tab.h generated with Yacc. You're getting error because you flex it, but you don't know what yacc.tab.h is.

yacc -d yacc.y创建y.tab.c

yacc -d yacc.y Make y.tab.c

g++ -c y.tab.c -o y.tab.o将y.tab.c编译为目标文件y.tab.o

g++ -c y.tab.c -o y.tab.o Compile y.tab.c to object file y.tab.o

flex lex.l创建lex.yy.c

flex lex.l Make lex.yy.c

g++ -c lex.yy.c -o lex.yy.o将lex.yy.c编译为目标文件lex.yy.o

g++ -c lex.yy.c -o lex.yy.o Compile lex.yy.c to object file lex.yy.o

g++ lex.yy.o y.tab.o -o program将它们链接在一起.

注意:请确保在lex文件中,其他包含之后包含"y.tab.h",因为其他包含可能使用y.tab.h中声明的某些函数,所以请记住这一点.当您编写更复杂的解析器时,可能会发生这种情况.

Note: Make sure that in your lex file you include "y.tab.h" after other includes, because it is possible that other includes use some function declared in y.tab.h, so have that in mind. Situations like this can occur when you're writing more complex parsers.

这篇关于未定义对`yylex'的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆