# Resolving shift/reduce conflict in Menhir grammar

**URL:** https://discuss.ocaml.org/t/resolving-shift-reduce-conflict-in-menhir-grammar/4958
**Category:** Learning
**Tags:** menhir
**Created:** [January 4, 2020, 10:58am UTC](https://discuss.ocaml.org/t/resolving-shift-reduce-conflict-in-menhir-grammar/4958 "2020-01-04T10:58:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![AjayMT](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ajaymt/32/1732_2.png) [@AjayMT](https://discuss.ocaml.org/u/AjayMT)
#### Post date: [January 4, 2020, 10:58am UTC](https://discuss.ocaml.org/t/resolving-shift-reduce-conflict-in-menhir-grammar/4958/1 "2020-01-04T10:58:06Z")

</div>

Hey everyone,  
I’m trying to write a Menhir grammar to parse simple expressions in a C-like language, but I’m having some trouble resolving one particular shift/reduce conflict.

I want to be able to parse ‘function calls’:

```auto
expression ( expression )

```

as well as unary operators:

```auto
*expression

```

However, I want the unary operator rule to have a higher precedence than the ‘function call’ rule, so  
`*expression(expression)` is equivalent to `(*expression)(expression)`.

Here’s a simplified version of my grammar:

```auto
%token OPERATOR IDENTIFIER EOF LPAREN RPAREN

%start <unit> term

%%

term: expr EOF { () }
;

expr:
  IDENTIFIER { () }
| LPAREN expr RPAREN { () }
| OPERATOR expr { () }
| expr LPAREN expr RPAREN { () }
;

```

This produces one shift/reduce conflict, and since the default action is to shift, `*expression(expression)` is parsed as `*(expression(expression))`.

I’ve tried adding `%prec` declarations:

```auto
%nonassoc low
%nonassoc high

...

| OPERATOR expr %prec high { () }
| expr LPAREN expr RPAREN %prec low { () }

```

But Menhir says that the `%prec` declarations are never useful.

How would I force Menhir to reduce `OPERATOR expr` instead of shifting?

---

<div class="post-metadata">

### Author: ![AjayMT](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/ajaymt/32/1732_2.png) [@AjayMT](https://discuss.ocaml.org/u/AjayMT)
#### Post date: [January 5, 2020, 1:00pm UTC](https://discuss.ocaml.org/t/resolving-shift-reduce-conflict-in-menhir-grammar/4958/2 "2020-01-05T13:00:00Z")

</div>

I resolved this by splitting up the `expr` rule into two:

```auto
op_expr:
  IDENTIFIER { () }
| LPAREN expr RPAREN { () }
| OPERATOR op_expr { () }
;

expr:
  op_expr { () }
| expr LPAREN expr RPAREN { () }
;

```

---

<div class="post-metadata">

### Author: ![threepwood](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@threepwood](https://discuss.ocaml.org/u/threepwood)
#### Post date: [January 6, 2020, 12:57pm UTC](https://discuss.ocaml.org/t/resolving-shift-reduce-conflict-in-menhir-grammar/4958/3 "2020-01-06T12:57:10Z")

</div>

I might be wrong, but I think what menhir does for shift/reduce conflicts is compare the precedence of a production to that of a token, as opposed to comparing two productions which is what it does for reduce/reduce conflicts. So you should have something like this where an actual token is referred to:

```auto
%nonassoc LPAREN
%nonassoc high

...

| OPERATOR expr %prec high { () }

```

I didn’t test that, though.

Of course if you managed to fix this in the grammar it’s probably more future-proof, precedence levels are such a pain to get right.
