user1184034
user1184034

Reputation: 81

Linker error Yacc on Mac

I am getting this error when i try to compile. I have compiled lex files before with no problem i cant seem to get yacc to compile without error.

:~ yacc project-5.y 
:~ lex  project.l 
:~g++ -o $dragon project-5.tab.c lex.yy.c 
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated
Undefined symbols for architecture x86_64:
"_main", referenced from:
 implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When compiled with the -c flag. I dont understand why it seems to compile but with errors.

test-5.tab.c:1246:16: warning: implicit declaration of function 'yylex' is invalid in C99 [-Wimplicit-function-declaration]
  yychar = YYLEX;
           ^
  test-5.tab.c:601:15: note: expanded from macro 'YYLEX'
  #define YYLEX yylex ()
          ^
  test-5.tab.c:1374:7: warning: implicit declaration of function 'yyerror' is invalid in C99 [-     Wimplicit-function-declaration]
  yyerror (YY_("syntax error"));
  ^
  test-5.y:35:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
  main(int argc, char **argv)
  ^~~~
  test-5.y:40:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
  yyerror(char *s)
  ^~~~~~~
  test-5.y:42:1: warning: control reaches end of non-void function [-Wreturn-type]
  }

LEX Code

%option noyywrap
%{
# include "project.tab.h"
%}

Delimiter               [ \t\n]
WhiteSpace              {Delimiter}+
Letter                  [A-za-z]
Digit                   [0-9]
E_Notation              E("+"|"-")?({Digit})+
Var                     ("_"|{Letter})(({Letter}|{Digit})+("_")?)*
UnsignedInteger         {Digit}*
UnsignedFloatingPoint   (({Digit})+(".")({Digit})*({E_Notation})?|({Digit}*(".")?({Digit})+            {E_Notation}?))
factor                  {UnsignedInteger}|{UnsignedFloatingPoint}|{Var}
term                    {factor}(("*"|"/"|"%"){factor})*
expression              {term}(("+"|"-"){term})*
Op                      ("+"|"-")
Op2                     ("*"|"/"|"%")
RelOp                   ("<"|"<="|"=<"|"=="|"!="|">"|">="|"=>"|".LT."|".LE."|".EL."|".EQ."|".NE."|".GT."|".GE."|".EG.")
LogOp                   ("!"|"||"|"&&"|".NOT."|".OR."|".AND.")
LogCons                 (".TRUE."|".FALSE.")
function                {Var}+"("{factor}*")"
Punct                   (";"|"("|")")
UnaryOp                 ("+"|"-")|[^{factor}]
%%
{Var}                   {return Var;}
{UnsignedInteger}       {return UnsignedInteger;}
{UnsignedFloatingPoint} {return UnsignedFloatingPoint;}
{Op}                    {return Op;}
{RelOp}                 {return RelOp;}
{LogOp}                 {return LogOp;}
{LogCons}               {return LogCons;}
{Punct}                 {return Punct;}
{function}              {return Function;}
\n                      {return EOL;}



%%

YACC Code

 /* simplest version of calculator */
 %{
 #include <stdlib.h>
 #include <stdio.h>

 %}
 /* declare tokens */
 %token Var
 %token UnsignedInteger
 %token UnsignedFloatingPoint
 %token Op
 %token Op2
 %token RelOp
 %token LogOp
 %token LogCons
 %token Punct
 %token Function
 %token EOL
 %%

stmt:   /* empty */
|stmt exp EOL {printf("Ok\n");}
|stmt error EOL {printf("Error\n");}
exp: term
| term Op exp
;
factor: Var
| UnsignedInteger
| UnsignedFloatingPoint
;
term: factor
| factor Op2 term
 ;
%%

int main(int argc, char **argv)
{
yyparse();
return 0;
}
int yyerror(char *s)
{
return 0;
 }

Upvotes: 0

Views: 3019

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126438

The default version of bison on OSX is a rather ancient v2.3, which generates C89 code (not compatible with either C99 or C++, as the errors/warnings you get show). You can compile with -std=c89 or you can make it more compatible by adding forward declarations to the first section of the .y file:

%{
int yylex();
int yyerror(char *);
%}

That will fix the C99 warnings/C++ errors about undefined functions, but will leave the warnings about misuse of string literals. If you also fix the file name mismatch identified by Brian Tompsett and the (possible cut-paste induced) typo spaces in the definition of UnsignedFloatingPoint in the .l file, you should get a runnable executable.

Upvotes: 3

You appear to be including different versions of the output of yacc which perhaps is giving you the compile errors?

You put the output of yacc project-5.y in the compile line:

g++ -o $dragon project-5.tab.c lex.yy.c 

but then in the project.l you have the following:

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

which perhaps is the output of yacc on an earlier project.y file which may have changed and thus causing some incompatibility between the versions of the code....

Upvotes: 2

Related Questions