为什么语言的语法不更严格?

发布于 2024-11-07 15:02:25 字数 91 浏览 0 评论 0原文

我浪费时间担心风格,例如间距、行缩进等。

我浪费时间适应项目之间的不同风格。

为什么语言在这方面变得灵活?为什么不要求非常严格的格式呢?

i lose time worrying about style, e.g. spacing, line indentation, etc.

i lose time adapting to different styles between projects.

why are languages made flexible in this regard? why not just demand very strict formatting?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-11-14 15:02:25

好吧,一个很好的指导性例子是看看 Pascal 和 ;命令分隔符。

Nicklaus Wirth(Pascal 的设计者)提出的规则是 ; 用于分隔块内的语句。从编译器的角度来看,这是明智的,因为这是严格需要语法元素来解决歧义的唯一情况。另外,通常已经用逗号完成了,所以处理它的代码就在那里。

问题在于,这是一条奇怪的规则,而且对于人类编码人员来说是一个很难理解的规则。面对变化也不稳定;如果您在块末尾添加或删除一行,则必须更改哪一行没有分号。大多数 Pascal 用户最终都会在每个编码/调试周期中经历一个完整的阶段,归结为编译器向他们吐出错误,直到他们正确使用分号为止。他们会讨厌它,进而讨厌帕斯卡。

将其与 Pascal 及其后代引入的相对较新的强类型概念相结合,就会有一些非常暴躁的开发人员。他们开始称呼 Pascal 及其同类束缚和纪律语言,并被吸引到像 C 这样更宽松的语言。这阻碍了许多更好的语言的发展,这一切都是因为具有良好理论概念的人使他的语言成为 PITA 使用。

对于后来设计的语言,Nicklaus Wirth 吸取了教训,并将分号定义为命令终止符,而不是分隔符。当然,C 设计者更进一步,允许额外的分号成为合法(但被忽略)的语法元素。

Well, a good instructive example would be to look at what happened with Pascal and the ; command separator.

The rule Nicklaus Wirth (the designer of Pascal) came up with was that the ; was to be used to separate statements within a block. This is sensible from the compiler's perspective, because that's the only situation where a syntactic element is strictly required to resolve ambiguity. Plus, it is commonly done with the comma already, so the code to handle it is there.

The problem is that it is a weird rule, and a tough one for human coders to wrap their heads around. It is also not stable in the face of changes; if you add or delete a line at the end of the block, you have to change which line gets no semicolon. Most Pascal users ended up with a whole phase in every coding/debug cycle that boiled down to their compiler spitting errors at them till they got their semicolons right. They'd get to hate it, and by extension they hated Pascal.

Combine this with the relatively new concept of strong typing that Pascal and its descendents introduced, and you had some very cranky developers. They started calling Pascal and its ilk bondage and discipline languages, and gravitated to more permissive languages like C. This had the effect of retarding the growth of a lot of better languages, all because someone with a good theoretical concept made his language a PITA to use.

For later designed languages, Nicklaus Wirth learned his lesson and defined semicolons to be command terminators, not separators. The C designers of course took it a step further and allowed extra semicolons to be legal (but ignored) syntax elements.

沒落の蓅哖 2024-11-14 15:02:25

据推测,用户定义自己的样式的选项(无论这是由某些编辑器规则强制执行的)比强制某种样式作为其语法的一部分的语言更可取。然而,有些语言在特定规则方面稍微严格一些。例如,Haskell 中有一个“功能”,给定的行必须比某些相关的前一行缩进得更远才能正常工作(我忘记了这个特定语言功能的名称,但我确信有人知道)。

Presumably the option for a user to define their own style ( whether this is enforced by some editor rules ) is preferable to a language forcing a certain style as part of its syntax. However some languages are slightly stricter when it comes to particular rules. For example, there is a 'feature' in Haskell whereby a given line must be indented further than some related preceding line to work properly ( I've forgotten the name of this particular language feature but I'm sure someone knows ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文