Can't compile flex & bison (Symbols not found x86_64)

流过昼夜 提交于 2019-12-11 00:12:30

问题


I am trying to compile a simple program on Flex & Bison on my Mac running Yosemite but get the following error:

Undefined symbols for architecture x86_64: "_yyerror", referenced from: _yyparse in pr1-19c182.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

My two files look like this, they compile on my teacher's Ubuntu installation but I can't get them to work on my Mac:

pr1.y

%{
    #include <stdio.h>
%}

%%

expr : expr '+' term {printf(" + ");}
    | term 
    ;

term : term '*' fact {printf(" * ");}
    | fact 
    ;

fact : "(" expr ")"
    | '0' {printf("0");}
    | '1' {printf("1");}
    | '2' {printf("2");}
    | '3' {printf("3");}
    | '4' {printf("4");}
    | '5' {printf("5");}
    | '6' {printf("6");}
    | '7' {printf("7");}
    | '8' {printf("8");}
    | '9' {printf("9");}

%%

pr1.l

%{
    #include "pr1.tab.h"
%}

%%

[0-9]   {return (yytext[0]);}
[+*()]  {return (yytext[0]);}
\n      {return (0);}
.       {}

%%

I compile everything using the following commands:

bison -d pr1.y
flex pr1.l
gcc -o result lex.yy.c pr1.tab.c -lfl -std=gnu89

The reason I use the -std flag is because the default is c99 and the code generated by flex and bison gets warnings and errors. Any ideas???


回答1:


You need to define yyerror in your bison input file (pr1.y). And you need to declare yylex or you will get another warning.

The following would be fine:

%{
   #include <stdio.h>
   void yyerror(const char* msg) {
      fprintf(stderr, "%s\n", msg);
   }
   int yylex();
%}

I have no idea how it works without that on your teacher's machine.

Also, Mac OS X has very old versions of bison and flex. You might want to upgrade.




回答2:


Check out the 2nd comment of the answer on this post Unable to compile output of lex

On Mac OS X, if you're trying to compile from the command line, you need to link against the libl.a library, by using -ll instead of -lfl. Apparently OS X has no libfl.a library. So try this...

gcc -o result lex.yy.c pr1.tab.c -ll

Don't know about your need for the -std=gnu89 option.



来源:https://stackoverflow.com/questions/28207339/cant-compile-flex-bison-symbols-not-found-x86-64

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!