I would like to use “simple” precedence rules to write a parser in Menhir for a grammar where juxtaposition is right-associative function application (like OCaml syntax, so e.g. f g x
should be parsed Apply (Apply (Var "f", Var "g"), Var "x")
). One might naïvely expect the following to work:
%token <string> VAR
%token EOF
%right APP
%start <Ast.t> main
%%
main : e = expr EOF { e }
expr:
| e1=expr e2=expr %prec APP
{ Ast.Apply (e1,e2) }
| x=VAR
{ Ast.Var x }
but alas
$ menhir lib/parser.mly
File "lib/parser.mly", line 4, characters 0-6:
Warning: the precedence level assigned to APP is never useful.
File "lib/parser.mly", line 12, characters 26-29:
Warning: this %prec declaration is never useful.
Warning: one state has shift/reduce conflicts.
Warning: one shift/reduce conflict was arbitrarily resolved.
I know how to get this to work the hard way, using explicit “levels” of expressions, like this:
%token <string> VAR
%token LPAREN RPAREN
%token EOF
%start <Ast.t> main
%%
main : e = expr EOF { e }
expr:
| e1=expr e2=expr1
{ Ast.Apply (e1,e2) }
| e=expr1
{ e }
expr1:
| x=VAR
{ Ast.Var x }
| LPAREN e=expr RPAREN
{ e }
So, is there a way to get the same result but using precedence annotations?
Thanks!