piotrdab piotrdab - 8 days ago 7
C++ Question

Difficulties during the compilation (g++, bison, flex) with yyparse();

I have a problem with compilation of my code:

Flex:

%{
#include "lista4.tab.hpp"
#include <stdlib.h>
extern int yylex();
%}
%%
"=" {return EQ;}
"!=" {return NE;}
"<" {return LT;}
">" {return GT;}
":=" {return ASSIGN;}
";" {return SEMICOLON;}
"IF" {return IF;}
"THEN"{return THEN;}

"END" {return END;}
[_a-z]+ {yylval.text = strdup(yytext); return IDENTIFIER;}
[ \t]+
[0-9]+ {
yylval.var = atoi (yytext);
return NUMBER;
}
[-+/^*'%'()] {
return *yytext;
}
\n return RESULT;
%%


Bison:

%{
extern "C"
{
int yyparse();
int yylex(void);
void yyerror(char *s){}
int yywrap(void){return 1;}
}

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <map>

using namespace std;

vector <string> instructions;
map <> vars;
%}

%union{
char* text;
int var;
}


%token EQ
%token NE
%token ASSIGN
%token SEMICOLON
%token IF
%token THEN
%token <text> IDENTIFIER
%token <var> NUMBER
%token <var> RESULT

%left '+' '-'
%left '*' '/' '%'
%right '^'

%%

exp: NUMBER
| IDENTIFIER
| IDENTIFIER "+" IDENTIFIER
| IDENTIFIER "-" IDENTIFIER
;
%%

int main(void){
yyparse();
}


And bash script:

#!/bin/bash
clear
rm launcher lex.yy.cpp *.tab.cpp *.tab.hpp
bison -d -o lista4.tab.cpp *.y
flex -o lex.yy.cpp *.l
g++ -o launcher *.cpp -lfl


I posted here only the most important part of code, because rest of them is not necessary here. Anyway, if someone would like to see whole code, I pasted it here: http://pastebin.com/1rS2FBJj. But it's a little bigger and takes more place.

When I tried to compile all to *.c files and then by gcc it was all fine. But when I switched compiler to g++ and files to cpp I get this error:


lista4.tab.cpp: In function ‘int yyparse()’:
lista4.tab.cpp:1397: warning: deprecated conversion from string constant to ‘char*’
lista4.tab.cpp:1540: warning: deprecated conversion from string constant to ‘char*’
/tmp/ccdqpQVx.o: In function `yyparse':
lista4.tab.cpp:(.text+0x252): undefined reference to `yylex'
collect2: ld returned 1 exit status


Could anyone give me a tip how to fix it?

Answer

In your Flex file, you declare this:

extern int yylex();

When compiled as C++, that declares a function with C++ linkage. In your Bison file, you declare this:

extern "C" {
  int yylex();
}

That gives it C linkage. They're two different functions. You define the C++ version (or, rather, Flex defines it for you), but you declare the C version, and the C version is the one the compiler thinks you try to call (in the Bison-generated code). The linker sees that the C version is used, but it cannot find the definition.

Pick one linkage and use it consistently. (I'd choose C++, since it allows omission of the "extern" stuff from the code entirely.)