开发者

Byacc Conflicts

I'm creating a byacc program to parse some input, and I'm having trouble resolving shift/reduce conflicts, in this case, how could I tell byac to always shift?

    53: shift/reduce conflict (shift 49, reduce 36) on '('
    53: shift/reduce conflict (shift 50, reduce 36) on '['
    53: shift/reduce conflict (shift 51, reduce 36) on '{'
    state 53
        body : start_section . section end_section  (32)
        body : start_section . section  (33)
        body : start_section .  (36)
        body : start_section . end_section  (38)
开发者_如何学Go

Thanks!


There are two types of conflicts: reduce/reduce and shift/reduce. When any conflict arises it's because the grammar you are creating is ambiguous. Your shift/reduce appears because Byacc doesn't know if it should reduce the rule 'body' and go to state 36 (like "I found this rule, give me input for the next one!") OR if it should shift (consume/accept) another token and continue looking for a rule to accept (like "I haven't found a complete rule but give me more input, maybe then I will).

Byacc always produces a unambiguous syntactic parser. How? When a conflict (a doubt) arises it just chooses one. Which one? For reduce/reduce it chooses the first to appear on the file. For a shift/reduce it chooses always the shift, by default. So... it will produce the behavior you want just by default.

Still, it's a good idea to have a unambiguous grammar. For that you'll have to resolve the conflict. That means you'll have to find a way to avoid that indecision of Byacc. I hope someone else can I help you with that, I am no expert. I know you can achieve it by reducing the number of rules and removing empty rules like:

A:            /* empty */
 | a
 ;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜