OCaml类型不兼容 - OCamlyacc

时间:2015-02-28 18:18:08

标签: compiler-construction functional-programming ocaml yacc ocamlyacc

  funcexpr:  /* This is a function: arguments -> string list */
   LPAREN HEAD arguments RPAREN                     { let head a = [List.hd (List.hd a)] in head << $3 }
 | LPAREN REAR arguments RPAREN                     { let rear b = List.tl (List.hd b) in rear << $3 }
 | LPAREN ERECT arguments RPAREN                    { let erect c = List.append (List.hd c) (List.hd (List.tl c)) in erect << $3 }
   ;
 arguments:  /* This is a list of functions */
   PARAM                                            { let func p = p in func }
 | funcexpr                                         { [$1] }
 | arguments arguments                              { List.append $1 $2 }

返回错误:错误:此表达式具有类型字符串列表 - &gt;字符串列表但是表达式需要类型字符串列表 - &gt;字符串列表列表类型字符串与类型字符串列表不兼容

我认为我们需要以某种方式将func放入列表中,但我尝试的每一种方式似乎都没有效果!任何帮助表示赞赏..

2 个答案:

答案 0 :(得分:1)

我的建议是你改变了

let func p = p in func

[ let func p = p in func ]

或者你可以使用更紧凑的:

[ fun p -> p ]

这是基于观察到arguments的其他替代方案返回列表但第一个替代方案没有。

答案 1 :(得分:0)

我假设你正试图用&lt;&lt;组成函数。您在别处定义的运算符。

不幸的是,$ 3并不代表一个函数,而是一个函数列表,因此在编写函数之前你需要做3美元的事情。

相关问题