作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在自学 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
更改为 表达式
.]。所有类,即 Addition
、Expression
、Multiplication
均已定义和声明。我不认为这是导致这里问题的原因,而只是说。
为什么只有 $2
有问题。即使 $1
是 expr
类型,那为什么我没有得到 $1
的任何错误?
感谢任何帮助...
最佳答案
规则中expr TOKEN_PLUS expr
$1
是第一个表达式,$2
是TOKEN_PLUS
,$3
是第二个表达式。见 Bison manual .
所以语义 Action 需要从你的 { $$ = new Addition($1, $2); }
到 { $$ = new Addition($1, $3);
。
关于c++ - 一个相当不寻常的 Bison 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7055875/
我是一名优秀的程序员,十分优秀!