Search code examples
ocamlocamlyaccmenhir

Menhir separated_nonempty_list generates code with type error


I have a simple recursive rule:

i_stmt:
  | CHAIN LPAREN c=separated_nonempty_list(i_stmt, COMMA) RPAREN {Chain c}
  | ASSIGN LPAREN n=i_var COMMA e=i_expr RPAREN {Assign (n,e)}
  | CRETURN LPAREN i=i_expr RPAREN { Return i }
  ;

It is compiled OK by Menhir, but ocamlc complains about generated code:

File "parser.mly", line 38, characters 72-73:
Error: This expression has type unit list
       but an expression was expected of type Ast.istmt list
       Type unit is not compatible with type Ast.istmt 

Type definition:

type istmt =
  | Chain of (istmt list)
  | Assign of ivar*iexpr
  | Return of iexpr

I've attempted to add:

%type <Ast.istmt> i_stmt

But it did not help either. What am I doing wrong?


Solution

  • From menhir manual :

    separated_nonempty_list(sep,X) a nonempty sequence of X’s separated with sep’s

    So you should write

    | CHAIN LPAREN c=separated_nonempty_list(COMMA, i_stmt) RPAREN {Chain c}