不同的开始和结束定界符在语法上是否必要?
在对智能引号和编程语言进行类比时,我突然想到,用于开始和结束分隔符的不同字符可能不是必需的,而只是可读性的选择。
例如,Ruby 的匿名函数中的参数使用相同的管道来打开和关闭。 Haskell 对空白的使用带有极端的偏见。
我并不是在问是否需要不同类型的分隔符(索引器的括号、块的大括号),而是问是否需要不同的左大括号和右大括号(例如 (
和 )< /code>)在大多数语言中都是语法上必需的,或者只是设计者的偏好。
In making an analogy between smart quotes and programming languages, it occurred to me that distinct characters for opening and closing delimiters might not be necessary, but simply a choice in readability.
For example, arguments in Ruby’s anonymous functions use identical pipes to open and close. Haskell uses white space with extreme prejudice.
I am not asking if different types of delimiters are necessary — brackets for indexers, braces for blocks — but whether distinct open and close braces (e.g. (
and )
) are syntactically necessary in most languages, or simply a preference of the designers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在语法上不是必需的,但如果打开和关闭分隔符相同,则很难(或不可能)嵌套事物。附件 A 是 POSIX shell,
where
被替换为
正是因为具有相同开始和结束定界符的代码不会嵌套。
Not syntactically necessary, but if open and close delimiters are the same, it makes it difficult (or impossible) to nest things. Exhibit A is the POSIX shell,
where
was replace with
precisely because the code with identical opening and closing delimiters does not nest.
具有不同的分隔符允许嵌套。 Ruby 的块参数列表不支持嵌套,因此使用相同的分隔符是可以的。
Having distinct delimiters allows nesting. Ruby's block parameter list does not support nesting, so using the same delimiter is okay.
在 C 和 C++ 中,裸大括号可以嵌套,并打开嵌套的词法范围:
使用相同的定界符,这是不明确的:
鉴于 C 语法规则的流行,可能有许多其他语言存在类似问题(perl 就是其中之一) 。
In C and C++, bare braces can be nested, and open nested lexical scopes:
Use identical delimeters and this is ambiguous:
Given the prevalence of C syntax rules, there are likely to be many other languages with similar issues (perl, for one).
可能意味着:
或
两者都是有效的 Haskell 并且具有不同的含义。但您问原则上是否可以调整语言以不要求这样做?
好吧,你可以做一件不可读的事情。不要通过并置来应用函数,而是为应用程序引入一个前缀、2 个参数运算符,称之为
*
。那么:可以明确地写成:
应用程序和单个常量符号足以编码组合器演算,所以,技术上,不,你根本不需要括号(图灵机也不需要括号) )。
但你不能只是把所有的括号都去掉,然后让它明确地有意义。
Could mean either:
or
Both of which are valid Haskell and have different meanings. But you are asking whether it is in principle possible to tweak the language not to require that?
Well there is an unreadable thing you can do. Instead of applying functions with juxtaposition, introduce a prefix, 2-argument operator for application, call it
*
. Then:could be unambiguously written:
Application and a single constant symbol is enough to encode combinator calculus, so, technically, no, you don't need parentheses at all (Turing machines don't need parentheses either).
But you can't just iron out all the parentheses into bars and have it make sense unambiguously.
Unlambda 是没有方括号、大括号等的语言的示例。它们被单个运算符 (反引号)代表“应用”。类似地,在 Haskell 中,您可以编写
a $ b c
而不是a (bc)
。因此有一些方法可以避免不同的分隔符。Unlambda is an example for a language without brackets, braces etc. They are replaces by a single operator (the Backtick) which stands for "apply". Similar, in Haskell you can write
a $ b c
instead ofa (b c)
. So there are ways to avoid different delimiters.从编写编译器的开发人员的角度来看,我认为使用分隔符更容易。我不确定您所说的“不同的分隔符”到底是什么意思,但从技术上讲,您可以使用您喜欢的任何内容作为您语言中的符号。只需在这里查看这种语言即可。只要看看当 if 语句有左大括号和右大括号时,解析它是多么容易:
与:
除了制表符之外,我还如何表明我希望这两个方法作为语句体的一部分执行
如果
。大括号让解析器清楚地知道它们都是在一起的。这样做的语言的问题是“悬挂的 else 问题”:你最好让你的解析器足够聪明,知道如何匹配 else 语句。我认为明确的分隔符会让这变得更容易。
Looking at it from the point of view of a developer writing a compiler, I think it's easier to use delimiters. I'm not sure exactly what you mean by distinct delimiters, but you could technically use anything you like as symbols in your language. Just look at this language here. Just look at how much easier it is to parse an if statement when it has opening and closing braces:
as opposed to:
Aside from the tabbing, how else do I show that I want both of those methods executed as part of the body of the
if
. The braces make it clear to the parser that it all goes together. The problem with languages that do this is the "dangling else problem":You better make your parser smart enough to know how to match up that else statement. I think explicit delimiters make that easier.
正如其他人提到的,它们没有必要。还有其他方法可以指示块何时开始/结束。话虽这么说,不同的开始和结束定界符为以很少的代码阅读代码的人提供了更多信息。这些信息不仅可以帮助读者理解代码正在做什么,还可以它的意图是什么。
举个例子,许多编码标准要求 C 中的单个语句 if 块用大括号括起来。
As mentioned by others, they're not necessary. There are other ways to indicate when a block is starting/ending. That being said, distinct opening and closing delimiters provide more information to the people reading the code at very little code. That information can not only help the reader understand what the code is doing, but also what it was intended to do.
As an example of this, many coding standards require braces around single statement if blocks in C.