Postgresql中yacc语法树冲突解决方法(shift/reduce conflicts)

处理方法

Postgresql中的gram.y可以独立编译,独立编译可以控制bison的参数来打印具体错误:

PG15

cd src/backend/parserbison -d -o gram.c gram.y -Wno-deprecated

正常执行后会产生gram.c文件,一旦发生冲突,bison会报错,例如:
在这里插入图片描述
但没有进一步的信息不好定位问题,这里提供两种方式打印更详细的错误帮助定位:

一、命令行直接输出冲突位置:

bison -d -o gram.c gram.y -Wno-deprecated -Wcounterexamples

(如果命令不支持counterexamples请更新bison:https://ftp.gnu.org/gnu/bison/)

结果:
在这里插入图片描述
可以看出这是一个reduce/recude冲突,位置也给出了。

二、冲突信息输出到文件:

bison --report="cex" -d -o gram.c gram.y

会在当前目录下生成gram.output文件。在文件中搜索conflict on token即可:
在这里插入图片描述

yacc的两种冲突

reduce/reduce冲突:两条规则都可以规约当前token

实例:VARCHAR改规约哪个?发生冲突。
在这里插入图片描述cex结果
在这里插入图片描述

shift/reduce冲突:两条规则既可以移进也可以规约token

实例:VARCHAR向右移进 还是 向上规约?发生冲突。
在这里插入图片描述

实例:NULL_P移进?规约?
在这里插入图片描述

bison -Wno-deprecated  -d -o gram.c gram.y -Wcounterexamplesgram.y: error: shift/reduce conflicts: 1 found, 0 expected
gram.y: warning: shift/reduce conflict on token NULL_P [-Wcounterexamples]Example: xmltable_column_option_el • NULL_PShift derivationxmltable_column_option_list↳ 1868: xmltable_column_option_el • NULL_PReduce derivationxmltable_column_option_list↳ 1867: xmltable_column_option_list         xmltable_column_option_el↳ 1866: xmltable_column_option_el • ↳ 1872: NULL_P


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部