【发布时间】:2014-10-03 23:54:10
【问题描述】:
考虑以下 Bison 语法(这是从我正在研究的一个更大的语法中剥离出来的):
%token ident
%left '+'
%left CALLPREC
%%
start: add ';' ;
expr: ident | call | add ;
call: expr '(' ')' %prec CALLPREC ;
add: expr '+' expr ;
在解析像foo + bar() 这样的表达式时,显然没有优先级存在s/r 冲突。我试图理解为什么 %prec 声明不能解决该冲突。我用的是 Bison 3.0.2,好像觉得指令没用:
$ bison -r state,solved -Wall ambigram.y
ambigram.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
ambigram.y:5.1-5: warning: useless precedence and associativity for CALLPREC [-Wprecedence]
奇怪的是,消除 %prec CALLPREC 并声明 %left '(' 可以解决冲突,但声明 %left ')' 不能。这与我对 Bison 文档的期望相反,即 [by] default, the precedence of a rule is that of its last token。
【问题讨论】:
标签: parsing compiler-construction bison