3
votes

I'm trying to make a calculator from flex and bison, but I found an error during the compile.

Here is the error:

C:\GnuWin32\src>gcc lex.yy.c y.tab.c -o tugas
tugas.y:51: error: conflicting types for 'yyerror'
y.tab.c:1433: error: previous implicit declaration of 'yyerror' was here

Here is my .l code :

%{
#include <stdio.h>
#include "y.tab.h"
YYSTYPE yylval;
%}
plus    [+]
semi    [;]
minus   [-]
var [a-z]
digit   [0-1]+
equal   [:=]
%%
{var}   {yylval = *yytext - 'a'; return VAR;}
{digit} {yylval = atoi(yytext); return DIGIT;}
{plus}  {return PLUS;}
{minus} {return MINUS;}
{equal} {return EQUAL;}
{semi}  {return SEMI;}
 .  { return *yytext; }
%%
int main(void)
{
 yyparse();
 return 0;
}

int yywrap(void)
{
 return 0;
}

int yyerror(void) 
{
  printf("Error\n");
  exit(1);
}

And here is my .y code :

%{
int sym[26];
%}

%token DIGIT VAR
%token MINUS PLUS EQUAL
%token SEMI 

%%

program: dlist SEMI slist
;

dlist:  /* nothing */
| decl SEMI dlist
;

decl:   'VAR' VAR   {printf("deklarasi variable accepted");}
;

slist:  stmt
| slist SEMI stmt
;

stmt:   VAR EQUAL expr   {sym[$1] = $3;}
| 'PRINT' VAR   {printf("%d",sym[$2]);}
;

expr:   term    {$$ = $1;}
| expr PLUS term    { $$ = $1 + $3;}
| expr MINUS term   { $$ = $1 - $3; }
;

term:   int {$$ = $1;}
| VAR   {$$ = sym[$1]; }
;

int:    DIGIT   {$$ = $1;}
| int DIGIT
;

Why I am getting this error? any suggestion to overcome this issue.
Thanks in advance

2

2 Answers

12
votes

yyerror should have this signature:

int yyerror(char *);

Since it is expected to accept a string to be used in the error message (would probably be better with a const char *, but you might get additional (ignorable) warnings with that...

2
votes

You need to change

int yyerror(void)

to

int yyerror(char*)

In other words, yyerror() must take a single c-string argument which describes the error which occured.