gpt4 book ai didi

c++ - 一个相当不寻常的 Bison 错误

转载 作者:太空狗 更新时间:2023-10-29 21:28:23 29 4
gpt4 key购买 nike

我正在自学 Bison,然后前往维基百科查看相同内容,并复制粘贴了示例中的整个代码 [ http://en.wikipedia.org/wiki/GNU_Bison ].它编译并完美运行。然后,我通过添加一些 C++ 对其进行 OOP。这是我的新 Parser.y 文件:

%{

#include "TypeParser.h"
#include "ParserParam.h"
#include "addition.h"

%}

%define api.pure

%left '+' TOKEN_PLUS
%left '*' TOKEN_MULTIPLY
%left '-' TOKEN_SUBTRACT
%left '/' TOKEN_DIVIDE
%left '^' TOKEN_EXP

%token TOKEN_LPAREN
%token TOKEN_RPAREN
%token TOKEN_PLUS
%token TOKEN_MULTIPLY

%token <value> TOKEN_NUMBER

%type <expression> expr

%%

input:
expr { ((SParserParam*)data)->expression = $1; }
;

expr:
expr TOKEN_PLUS expr { $$ = new Addition($1, $2); }
| expr TOKEN_MULTIPLY expr { $$ = new Multiplication($1, $2); }
| expr TOKEN_SUBTRACT expr { $$ = new Addition($1, $2); }
| expr TOKEN_DIVIDE expr { $$ = new Multiplication($1, $2); }
| expr TOKEN_EXP expr { $$ = new Addition($1, $2); }
| TOKEN_LPAREN expr TOKEN_RPAREN { $$ = $2; }
| TOKEN_NUMBER { $$ = new Value($1); }
;

%%

但是我不断收到以下错误:

Parser.y:33.52-53: $2 of `expr' has no declared type
Parser.y:34.62-63: $2 of `expr' has no declared type
Parser.y:35.56-57: $2 of `expr' has no declared type
Parser.y:36.60-61: $2 of `expr' has no declared type
Parser.y:37.52-53: $2 of `expr' has no declared type

如何解决?我的意思是,我改变了什么导致了这个?我没有更改维基百科代码中的任何内容,%type% 声明仍然存在 [ union 具有相同的成员,类型从 SExpression 更改为 表达式.]。所有类,即 AdditionExpressionMultiplication 均已定义和声明。我不认为这是导致这里问题的原因,而只是说。

为什么只有 $2 有问题。即使 $1expr 类型,那为什么我没有得到 $1 的任何错误?

感谢任何帮助...

最佳答案

规则中expr TOKEN_PLUS expr $1是第一个表达式,$2TOKEN_PLUS$3 是第二个表达式。见 Bison manual .

所以语义 Action 需要从你的 { $$ = new Addition($1, $2); }{ $$ = new Addition($1, $3);

关于c++ - 一个相当不寻常的 Bison 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7055875/

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