未定义的引用 yyparse c++

Undefined reference yyparse c++

本文关键字:c++ yyparse 引用 未定义      更新时间:2023-10-16

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

米朗

%{
/******************** 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

%{
        /******************** 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 .. %sn", yylineno,s);
    }

米朗.cpp

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

我的制作文件如下所示。

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

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

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

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

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

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

删除 mylang 中 yyparse 声明周围的外部"C".cpp应该可以解决问题。

bison -d fic.y在野牛文件上执行此命令后:您应该使用 : flex -o nomfic.c fic.l 编译您的 flex 文件并将此文件包含在野牛文件中,然后使用:gcc -o a.out fic.tab.c