0

While compiling lex program an error obtained : conflicting types for ‘yylval’ extern YYSTYPE *yylval;

Any idea how to correct this one? This the the lex code

%{
  #include<stdio.h>
  #include"y.tab.h"
  extern char *yylval;
%}

%%
 "int"|"float"|"char"|"double" { yylval=strdup(yytext); return TYP;}
 [a-z A-Z][a-z A-Z 0-9]* { yylval=strdup(yytext); return ID;}
 ";" return SEMI;
 "," return COMA;
 "{" return LB;
 "}" return RB;
 "\n" return NL;
 [\t]+;
 .;
 %%
JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
deadendtux
  • 63
  • 2
  • 8
  • This is covered quite well in the documentation for `bison`. Changing the type of YYLVAL depends on which Bison version you have. See for example http://www.gnu.org/software/bison/manual/html_node/Rpcalc-Declarations.html#Rpcalc-Declarations – Gene Oct 06 '13 at 03:59

1 Answers1

2

You define yylval twice -- once as a YYSTYPE in your .y file (which is exported to y.tab.h) and a second time as char * in your lex code. Get rid of the extern char *yylval; and the multiple definitions will go away, though you'll also need to change your uses of yylval in the lex code to be compatible with whatever you've defined in your .y file.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226