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

I have a problem compiling 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 have placed here only the most important part of the code, because the rest of them are not needed here. In any case, if someone wants to see the whole code, I inserted it here: http://pastebin.com/1rS2FBJj . But it is a little bigger and takes up more space.

When I tried to compile all the * .c files and then gcc, everything was fine. But when I switched the compiler to g ++ and the 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

Can someone give me advice on how to fix it?

+4
source share
1 answer

In your Flex file, you declare this:

 extern int yylex(); 

Compiled as C ++, which declares a function with C ++ binding . In your Bison file, you declare the following:

 extern "C" { int yylex(); } 

This gives the link C. These are 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 that the compiler thinks you are trying to call (in the code created by Bison). Linker sees that version C is being used, but she cannot find the definition.

Select one link and use it sequentially. (I would choose C ++, since it allows you to completely exclude "extern" from the code.)

+11
source

All Articles