You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何单行Standard ML代码Int.toString 5在MLton中必须加分号?

为何单行Standard ML代码在MLton中必须加分号才能编译成功?

我有一个包含单行Standard ML代码的文件foo.sml:

Int.toString 5

该代码在SML/NJ中可正常运行,但在MLton中报错:

$ cat foo.sml
Int.toString 5

$ sml < foo.sml
Standard ML of New Jersey (64-bit) v110.99.5 [built: Thu Mar 14 17:56:03 2024]
- = [autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[autoloading done]
val it = "5" : string

$ mlton foo.sml
Error: foo.sml 3.0.
  Syntax error found at EOF.
Error: foo.sml 3.0-3.0.
  Parse error.

添加分号后问题解决:

$ cat foo.sml
Int.toString 5;

$ sml < foo.sml
Standard ML of New Jersey (64-bit) v110.99.5 [built: Thu Mar 14 17:56:03 2024]
- [autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[library $SMLNJ-BASIS/(basis.cm):basis-common.cm is stable]
[autoloading done]
val it = "5" : string
- 

$ mlton foo.sml
$ ./foo
$

原因解析

核心差异在于两款工具的定位和语法解析策略:

  • SML/NJ是交互式解释器,为提升使用便利性对顶层输入做了语法宽容处理——即使表达式末尾省略分号,它也能通过上下文(比如单行结束、输入终止)识别表达式边界,自动补充分号逻辑完成解析执行。
  • MLton是批量编译器,严格遵循Standard ML官方语法规范。标准要求顶层表达式或声明必须以分号结尾,以此明确标记编译单元的结束。当MLton解析到文件末尾仍未遇到分号时,会判定语法不完整,抛出解析错误。

简言之,SML/NJ为交互式场景妥协了语法严谨性,而MLton作为编译工具严格执行标准语法要求。

内容的提问来源于stack exchange,提问作者Lone Learner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 07:17:41