当我在yacc中引入一个动作时,Shift/减少冲突
Shift/Reduce conflict when I introduce an action in yacc
我正在为我的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)); }
;
当我这样做时,我得到一个shift/reduce错误,因为{currentType = $1;}被认为是空规则。我该如何处理这个错误?是否有一种方法可以指定它只是一个动作?
下面是我的y.p 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.
我修复的问题如下:
declaration: datatype varList ';' { gTrace<<"declaration "; currentType = -1; }
varList: IDENTIFIER { builder.addSymbol($1, getType(currentType)); }
| varList',' IDENTIFIER { builder.addSymbol($3, getType(currentType)); }
;
datatype: INTEGER { gTrace<<"int "; $$ = currentType = Type::IntegerTy; }
| FLOAT { gTrace<<"float "; $$ = currentType = Type::FloatTy; }
| VOID { gTrace<<"void "; $$ = currentType = Type::VoidTy; }
;
@sarnold,希望这对你有帮助!
我觉得你只能为每条规则定义一个动作块,所以
declaration: datatype { currentType = $1; } varList ';' { gTrace<<"declaration "; currentType = -1; }
应按
处理declaration: datatype varList ';' { currentType = $1; gTrace<<"declaration "; currentType = -1; }
无论如何,您将currentType设置为数据类型的词法值,并在
相关文章:
- 写入位置0x0000000C时发生访问冲突
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- C++LinkedList问题.数据类型之间存在冲突?没有匹配的构造函数
- 热键/按钮,根据需要进行编译,但不运行(在F5和Ctrl+Shift+B之间)
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- 如何使 std::sort 在 std::swap 和我的命名空间的模板化交换之间没有名称冲突?
- C++尝试深度复制唯一指针时出现内存访问冲突
- 错误:使用通用引用的声明冲突
- 如何解决GTest和LibTorch联动冲突
- 两个运营商的一些奇怪的冲突<<
- 如何在多个线程中创建 QSql数据库连接时防止名称冲突
- C++ 中动态二维数组的访问冲突
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- C++17 十六进制浮点文字单精度后缀冲突?
- 当我在yacc中引入一个动作时,Shift/减少冲突
- 如何避免shift/reduce与多个操作数产生的冲突
- Shift减少冲突,财务计算器使用编译器