我是 OCaml 新手,我正在尝试编写一个简单的类似 OCaml 的语法,但我无法弄清楚。我的语法允许这样的事情:
let sub = fun x -> fun y -> x - y;;
但是,如果我想使用这样定义的函数,我可以写:
(sub 7) 3
但我不会写 sub 7 3
,这真的让我很烦恼。出于某种原因,它被解释为好像我写了 sub (7 3)
(这会将 7
视为带有参数 3
的函数)。相关部分是:/* other operators, then at the very end: */
%left APPLY
/* ... */
expr:
/* ... */
| expr expr %prec APPLY { Apply($1, $2) }
谢谢!
最佳答案
ocaml 编译器执行如下函数应用:(来自 ocaml/parsing/parser.mly
)
expr:
...
| simple_expr simple_labeled_expr_list
{ mkexp(Pexp_apply($1, List.rev $2)) }
哪里
simple_expr
是可以计算为函数而无需括号的可能 expr 值的子集。这排除了与函数调用内联使用的所有非自括号构造。它还阐明了子表达式的结合性,因为第二个子表达式明确是一个列表。至于您为何尝试使用
%left APPLY
从 ocaml 的 parser.mly 中的注释中获得正确的关联性不起作用:We will only use associativities with operators of the kind x * x -> x
for example, in the rules of the form expr: expr BINOP expr
in all other cases, we define two precedences if needed to resolve
conflicts.
我会说这意味着你不能在没有运算符的情况下使用 %prec 进行关联。尝试通过定义更多规则来创建您想要的关联性,然后看看会导致什么结果。
关于ocaml - 关于 ocamlyacc,函数应用语法和优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2847399/