gpt4 book ai didi

bison - 如何解决Bison警告 "... has no declared type"

转载 作者:行者123 更新时间:2023-12-02 01:33:17 29 4
gpt4 key购买 nike

在此文件上运行 Bison:

%{
#include <iostream>
int yylex();
void yyerror(const char*);
%}


%union
{
char name[100];
int val;
}

%token NUM ID
%right '='
%left '+' '-'
%left '*'

%%

exp : NUM {$$.val = $1.val;}
| ID {$$.val = vars[$1.name];}
| exp '+' exp {$$.val = $1.val + $3.val;}
| ID '=' exp {$$.val = vars[$1.name] = $3.val;}
;

%%

导致以下警告:

warning: $$ of 'exp' has no declared type.

这是什么意思以及如何解决?

最佳答案

定义的联合 (%union) 不适合直接使用。相反,您需要告诉 Bison 哪个表达式使用了联合的哪个成员。

这是通过 %type directive 完成的。

代码的固定版本是:

%{
#include <iostream>
int yylex();
void yyerror(const char*);
%}


%union
{
char name[100];
int val;
}

%token NUM ID
%right '='
%left '+' '-'
%left '*'

%type<val> exp NUM
%type<name> ID

%%

exp : NUM {$$ = $1;}
| ID {$$ = vars[$1];}
| exp '+' exp {$$ = $1 + $3;}
| ID '=' exp {$$ = vars[$1] = $3;}
;

%%

关于bison - 如何解决Bison警告 "... has no declared type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1014619/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com