Resolving reduce/reduce conflict in yacc/ocamlyacc

ぐ巨炮叔叔 提交于 2019-12-05 01:24:43

Unfortunately, the only answer I can come up with means increasing the complexity of the grammar.

  1. split expr into simple_expr and expr_with_prefix
  2. allow only simple_expr or (expr_with_prefix) in an APPLY

The first step turns your reduce/reduce conflict into a shift/reduce conflict, but the parentheses resolve that.

You're going to have the same problem with 'a b c': is it a(b(c)) or (a(b))(c)? You'll need to also break off applied_expression and required (applied_expression) in the grammar.

I think this will do it, but I'm not sure:

expr := INT
      | parenthesized_expr
      | expr MINUS expr

parenthesized_expr := ( expr )
                    | ( applied_expr )
                    | ( expr_with_prefix )

applied_expr := expr expr

expr_with_prefix := MINUS expr

Well, this simplest answer is to just ignore it and let the default reduce/reduce resolution handle it -- reduce the rule that appears first in the grammar. In this case, that means reducing expr MINUS expr in preference to MINUS expr, which is exactly what you want. After seeing a-b, you want to parse it as a binary minus, rather than a unary minus and then an apply.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!