menhir separated_nonempty_list生成类型错误的代码

时间:2017-05-19 01:29:17

标签: ocaml ocamlyacc menhir

我有一个简单的递归规则:

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 }
  ;

Menhir编译好了,但ocamlc抱怨生成的代码:

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 istmt =
  | Chain of (istmt list)
  | Assign of ivar*iexpr
  | Return of iexpr

我试图添加:

%type <Ast.istmt> i_stmt

但它也没有帮助。我做错了什么?

1 个答案:

答案 0 :(得分:4)

来自menhir手册:

  

separated_nonempty_list(sep,X)X分隔的sep非空序列

所以你应该写

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