最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Why does Bison prioritize Exp → Exp MINUS Exp over Exp → MINUS Exp? - Stack Overflow

programmeradmin3浏览0评论

I'm working on a Bison grammar for arithmetic expressions, and I encountered an interesting behavior when parsing expressions with the MINUS operator.

Here’s a simplified version of my grammar:

%left MINUS
%%
Exp : Exp MINUS Exp
    | MINUS Exp
    | ID
    ;
%%

When parsing a - b, how does Bison decide between Exp -> MINUS Exp and Exp -> Exp MINUS Exp?Is it a reduce/reduce conflict?And why he choose the latter one?

I have set MINUS as left-associative using %left MINUS, but I thought that only affects multiple Exp MINUS Exp cases like a - b - c.

The Bison's manual says Bison resolves a reduce/reduce conflict by choosing to use the rule that appears first in the grammar.But I have exchanged the sequence of these two rules and the results have no difference.

Questions: Why does Bison prioritize Exp → Exp MINUS Exp over Exp → MINUS Exp when parsing a - b? Even if I switch the order of the two rules, Bison still chooses Exp MINUS Exp. Why? Does %left MINUS play a role in this decision, and if so, how? I suspect this has to do with shift/reduce behavior, but I want to clarify why Exp MINUS Exp wins over MINUS Exp. Any detailed explanation, preferably from a state-machine perspective, would be greatly appreciated!

Thanks in advance!

发布评论

评论列表(0)

  1. 暂无评论