Menhir separated_nonempty_list 生成类型错误的代码
Menhir separated_nonempty_list generates code with type error
我有一个简单的递归规则:
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
但也无济于事。我做错了什么?
来自 menhir 手册:
separated_nonempty_list(sep,X)
a nonempty sequence of X
’s separated with sep
’s
所以你应该写
| CHAIN LPAREN c=separated_nonempty_list(COMMA, i_stmt) RPAREN {Chain c}
我有一个简单的递归规则:
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
但也无济于事。我做错了什么?
来自 menhir 手册:
separated_nonempty_list(sep,X)
a nonempty sequence ofX
’s separated withsep
’s
所以你应该写
| CHAIN LPAREN c=separated_nonempty_list(COMMA, i_stmt) RPAREN {Chain c}