未定义的引用yyparse c ++

时间:2016-01-28 05:57:29

标签: c++ g++ bison flex-lexer

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

mylang.ll

 Intent i = new Intent(D.this, B.class);
// set the new task and clear flags
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

mylang.yy

%{

/******************** 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.cpp

%{

        /******************** 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);
    }

我的makefile运行如下。

#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;
}

当我运行此代码时,我收到以下编译时错误。

  

mylang.cpp:31:未定义对`yyparse&#39;

的引用      

collect2:错误:ld返回1退出状态

     

make:*** [mylang]错误1

我在哪里定义yyparse函数?我在代码中犯了什么错误?

1 个答案:

答案 0 :(得分:2)

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

g++ -Os -g -std=c++0x -c mylang.yy.tab.c

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

链接器搜索符号_yyparse,而C ++编译器导出了带有C ++名称修改的符号(向名称添加类型信息,以便可以导出函数的所有可能重载)并将其转换为类似_Z3yyparsev的内容。 / p>

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