AppleScript类天然语言与非英语语法设计

最先知晓是以前C#中文版的github讨论里提到了AppleScript有多语言版. 昨天想起, 以为它毕竟是为数很少(仅有的?)大公司开发的非英语语法的编程语言, 不由好奇它的前世此生. 因而做了一点调研.php

网上相关资料很少, 最先的一本AppleScript专著"AppleScript in a Nutshell"也是2001年出版, 已经到了Mac 9. AppleScript Not Necessarily in English 提到早先支持日语,法语, 还讨论过"a C dialect", 不知指哪一个语言, 但在Mac 8.5版就放弃了(1998年左右)git

接着有幸找到了2006年的论文"AppleScript", 更多介绍了多语言支持以及相关问题. 这篇论文其余内容也颇有价值, 包括这个项目的起源, 相关项目, 以及这一语言系统从需求分析到实现的各阶段细节. 本文只关注非英语语法部分, 对项目自己有兴趣的不妨自行细阅.程序员

下图是不一样"语种"(英语, 日语, 法语, "专业")的示例代码:github

文中总结的贴近天然语言语法的语言设计的问题:shell

The experiment in designing a language that resembled natural languages (English and Japanese) was not successful. It was assume that scripts should be presented in “natural language” so that average people could read and write them. This lead to the invention of multi-token keywords and the ability to disambiguate tokens without spaces for Japanese Kanji. In the end the syntactic variations and flexibility did more to confuse programmers than to help them out. The main problem is that AppleScript only appears to be a natural language. In fact is an artificial language, like any other programming language. Recording was successful, but even small changes to the script may introduce subtle syntactic errors which baffle users. It is easy to read AppleScript, but quite hard to write it.编程

When writing programs or scripts, users prefer a more conventional programming language structure. Later versions of AppleScript dropped support for dialects. In hindsight, we believe that AppleScript should have adopted the Professional Dialect that was developed but never shipped.安全

Finally, readability was no substitute for an effect security mechanism. Most people just run scripts — they don’t read or write them.markdown

大体是说, 类天然语言语法(包括英语和日语, 不知为什么没提法语)的尝试失败了. 为了实现接近天然语言的语法, 引入了多token的关键词(猜想是相似于"在...中"这样的用法), 以及日语的无空格语法. 问题是, 这样"貌似"天然语言而实质仍然语法严格的后果之一是, 用户误认为小的改动, 其实会致使词法解析的大变化. 也就是**"易读而难写"**. 另外一个结论是, "Professional Dialect", 即接近Java的语法被认为是最应该被保留的, 由于它最接近传统编程习惯.app

最后一段的意思是, 即便代码可读性好, 也不能取代一套有效的安全机制. 由于大多数人在运行脚本以前, 并不会先读一遍, 确认它是否安全. 我的认为这个结论对语言设计的借鉴价值有限, 由于安全机制和可读性应该没有直接矛盾.编程语言

关于文中提到语法近天然语言致使的问题, 我的认为与目标用户群有关. 文中提到"The language was primarily aimed at casual programmers."以及"make it accessible to non-programmers", 可见它的设计初衷和推广目标都是非专业程序员, 而他们是最容易将天然语言和程序语言混淆的. 这个问题一样能够适用于今天, 虽然技术上程序语言语法能够引入更多天然语言元素. 这也许是多数最新的通用型英语编程语言(求反例)仍然与天然语言保持至关距离的一个缘由.

话虽如此, 现今的人机交互已经愈来愈普及, 即便是通常用户, 对手机喊命令的时候也会清楚是对一个只懂得简单语法的机器说话. 随着大众适应的过程, 上面这个混淆的问题也许会不那么严重. 我的猜测, 编程语言的趋势仍是逐渐向靠近天然语言的方向发展, 尤为是适用于大众编程的语言.

再回到开头的那个讨论, 提到AppleScript时将这一功能的失败归因于关键词/语法支持多语言致使脚本内容混乱:

But it found limited adoption and where it was adopted scripts became a mess of mixed languages and Apple eventually dropped support.

回头看是须要考证的, 由于看起来最大的问题在于当时相似天然语言的语法, 即便只使用单纯的英语语法.

2018-02-04

相关文章
相关标签/搜索