2
votes

I am developing a simple interpreter using bison and flex. When I compile my code I am getting the error of 'undefined reference to 'yyparse'.

mylang.ll

%{

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

mylang.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;
}

My makefile running like below.

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

When I run this code, I am getting the below compile time error.

mylang.cpp:31: undefined reference to `yyparse'

collect2: error: ld returned 1 exit status

make: *** [mylang] Error 1

Where do I define yyparse function here? What mistake I have done in my code?

1

1 Answers

3
votes

You are compiling your parser with a C++ compiler

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

but you declare the parser function exported from that file as extern "C", thus causing the linker to look for the symbol yyparse with C naming conventions.

The linker searches for a symbol _yyparse, while the C++ compiler has exported the symbol with C++ name mangling (adding type information to the name so all possible overloads of the function can be exported) and turned it into something like _Z3yyparsev.

Removing the extern "C" around the yyparse declaration in mylang.cpp should solve the problem.