gpt4 book ai didi

c++ - 当我在 yacc 中引入一个 Action 时 Shift/Reduce 冲突

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:33:41 25 4
gpt4 key购买 nike

我正在为我的 C 编译器编写一个前端,目前我正在其中添加类型系统。以前我假设一切都是一个 int,因此下面的规则工作正常。

declaration: datatype varList ';' { gTrace<<"declaration ";}

varList: IDENTIFIER { builder.addSymbol($1); }
| varList',' IDENTIFIER { builder.addSymbol($3); }
;

但现在我还为符号添加了类型,因此修改了我的规则,如下所示:

 declaration: datatype { currentType = $1; } varList ';' { gTrace<<"declaration ";   currentType = -1; }

varList: IDENTIFIER { builder.addSymbol($1, getType(currentType)); }
| varList',' IDENTIFIER { builder.addSymbol($3, getType(currentType)); }
;

当我这样做时,我得到了一个移位/减少错误,因为 { currentType = $1; 被视为空规则。我该如何解决这个错误?有没有办法指定它只是一个 Action ?

下面附上了我的 y.output 的一个片段

32 $@6: /* empty */

33 declaration: datatype $@6 varList ';'

34 varList: IDENTIFIER
35 | varList ',' IDENTIFIER

最佳答案

我没有收到任何错误或警告:

%token   INT
%token FLOAT
%token CHAR
%token IDENTIFIER

%%

declaration: datatype { currentType = $1; } varList ';' { gTrace<<"declaration "; currentType = -1; }

varList : IDENTIFIER { builder.addSymbol($1, getType(currentType)); }
| varList ',' IDENTIFIER { builder.addSymbol($3, getType(currentType)); }
;

datatype: INT
| FLOAT
| CHAR
;

%%

命令

% bison p.yacc
%

我认为您需要提供更多信息。

完整的 yacc 文件和传递给 yacc/bison 的参数

编辑

我试过你的文件(根据评论)我仍然没有收到错误或警告:

> yacc --version
bison (GNU Bison) 2.3
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

关于c++ - 当我在 yacc 中引入一个 Action 时 Shift/Reduce 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8599338/

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