首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未定义引用Y解析c++

未定义引用Y解析c++
EN

Stack Overflow用户
提问于 2016-01-28 05:57:28
回答 1查看 2.7K关注 0票数 2

我正在用bison和flex开发一个简单的解释器。当我编译我的代码时,我得到的错误是“未定义的对'yyparse‘的引用”。

mylang.ll

代码语言:javascript
复制
%{

/******************** C-libraries and Token definitions *****************/

#include <string.h>                 /* for strdup   */
#include "mylang.yy.tab.h"          /* for token definitions and yylval */
extern int yyparse(void);

%}

%option nounput yylineno


/******************** MY TOKEN Definitions *****************/


%%

int yywrap(void){}

mylang.yy

代码语言:javascript
复制
%{

        /******************** C Libraries, Symbol Table, Code Generator & other C code *****************/
    #include <stdio.h>                              /* For I/O */
    #include <stdlib.h>                             /* For malloc here and in symbol table */
    #include <string.h>                             /* For strcmp in symbol table */

    #include "StackMachine.h"                       /* Stack Machine*/

    #include "CodeGenerator.h"                      /* Code Generator*/

    #include "SymbolTable.h"                        /* Symbol Table*/

    extern "C" int yylex(void);

    extern "C" void yyerror(const char *);
    extern "C" int yylineno;

    #include "IM.h"                                 /* Identifier Making */

    #define YYDEBUG 1                               /* For Debugging*/

%}

    /******************** SEMANTIC RECORDS *****************/

    %union semrec               /* The Semantic Records*/
    {
        double floatnum;        /* Double values */
        char *string;           /* Identifiers*/
    }

        /******************** TOKENS ***************************/

%%
        /******************** GRAMMAR RULES for the Simple language *******/

%%

        /******************** YYERROR ********************************************************** */

    void yyerror (const char *s ) /* Called by yyparse on error */
    {
        errors++;
        printf ("Line Number : %d .. %s\n", yylineno,s);
    }

mylang.cpp

代码语言:javascript
复制
#include "Headers.h"
extern "C" {
  int yyparse(void);
}
/* ********************************** MAIN *****************************/
int main( int argc, char *argv[] )
{   
    extern FILE *yyin;
    ++argv; --argc;
    yyin = fopen( argv[0], "r" );

    errors = 0;
    yyparse ();

    /* Execute Code */

    return 0;
}

我的makefile跑得像下面一样。

代码语言:javascript
复制
g++ -Os -g -std=c++0x -c SM.cpp
g++ -Os -g -std=c++0x -c mylang.cpp
bison -d mylang.yy
flex mylang.ll
g++ -Os -g -std=c++0x -c mylang.yy.tab.c
gcc  -c lex.yy.c
g++ -Os  -std=c++0x  -o mylang mylang.yy.tab.o lex.yy.o SM.o mylang.o -lm

当我运行这段代码时,我会得到下面的编译时错误。

mylang.cpp:31:对“`yyparse”的未定义引用 collect2:错误: ld返回1退出状态 制造:* mylang错误1

在这里我应该在哪里定义yy解析式函数?我在代码中犯了什么错误?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-28 06:50:16

您正在使用C++编译器编译解析器。

代码语言:javascript
复制
g++ -Os -g -std=c++0x -c mylang.yy.tab.c

但是,您将从该文件导出的解析器函数声明为extern "C",从而导致链接器查找具有C命名约定的符号yyparse。

链接器搜索符号_yyparse,而C++编译器则导出带有C++名称损坏的符号(将类型信息添加到名称中,以便可以导出函数的所有可能重载),并将其转换为类似于_Z3yyparsev的内容。

在mylang.cpp中删除yyparse声明周围的extern "C“应该可以解决这个问题。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35053838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档