c - yyerror 的 Bison 冲突类型

标签 c bison lex

我试图用 flex 和 bison 制作一个计算器,但我在编译过程中发现了一个错误。

这里是错误:

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

这是我的 .l 代码:

%{
#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);
}

这是我的 .y 代码:

%{
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
;

为什么我会收到这个错误?任何解决这个问题的建议。
提前致谢

最佳答案

yyerror 应该有这个签名:

int yyerror(char *);

因为它应该接受一个在错误消息中使用的字符串(使用 const char * 可能会更好,但是你可能会得到额外的(可忽略的)警告......

关于c - yyerror 的 Bison 冲突类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15641256/

相关文章:

c - 结构体列表、成员删除

c - 如何将复杂的C结构从Go传输到C

c++ - 如何在单独的文件中使用 `yy_scan_string(const char *str)`(由 lex yacc 生成)

c - 随机访问一个大的二进制文件

c - 适用于 Linux 的 Sun Studio 12 C 编译器太慢了

c++ - Xcode 中的 Flex、Bison、C++

warnings - Bison 建立警告: "-s option given but default rule can be matched"

char总是使用flex改变

calculator - 使用两个以上参数计算最小值/最大值

c - 搜索并替换一个字符串,如下所示