是否可以设计一种不需要在字符串文字中转义引号的语言?
在 C++(以及翻译后的大多数语言)中,以下内容当然是语法错误:
std::string str = "Hello "Jesus""; // oopsquotes
能否创建不需要这些引号转义的类似 C++ 的语言?编译器是否可以看到上面这样的一行,并智能地确定我不希望字符串在 Hello
之后终止,在一般情况下?
语言和编译器喜欢要求我们编写精确的语法以避免歧义,但我似乎无法想出一个与上面类似的非人为的例子,其中的含义可能不是“请输入 Hello“Jesus”
在一个字符串中”。在 C++ 中,“Jesus”必须是一个扩展为某个字符串文字 "x"
的预处理器宏,因为上面的内容可能意味着其他任何内容。在不存在此类扩展的情况下,在代码中支持这种潜在情况是否非常重要?
那么,是否可以创建一种不需要在字符串文字中转义引号的语言呢?你能想出任何非人为的反例吗? 应该像这样的语言存在吗?也许有人已经这样做了……?
讨论。
In C++ (and, after translation, most languages) the following is of course a syntax error:
std::string str = "Hello "Jesus""; // oopsquotes
Could a C++-like language be created that doesn't need these quotes escaping? Could a compiler see a line like the above and intelligently determine that I didn't want the string to terminate after Hello
, in the general case?
Languages and compilers like to require us to write precise syntax to avoid ambiguities, but I can't seem to think up a non-contrived example similar to the above where the meaning could be anything but "please put Hello "Jesus"
in a string". In C++, "Jesus" would have to be a preprocessor macro that expanded to some string literal "x"
, for the above to potentially mean anything else. Is it very important to support this potential case in code where no such expansion exists?
So, could a language be created where we didn't need to escape quotes in a string literal? Can you think of any non-contrived counter-examples? Should a language like this exist? Perhaps one already does...?
Discuss.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
利用其无限前瞻功能,在基于 PEG 的解析器中实现相对容易。但是,正如其他人已经提到的那样,这样做是没有意义的,因为并不总是能够解决歧义,特别是当您想要将格式良好的代码嵌入到字符串中时。如果您不允许多行字符串,这可能会更容易一些。
It is relatively easy to implement in a PEG-based parser, utilising its infinite lookahead capability. But, as the others already mentioned, there is no point in doing it, as it won't always be possible to resolve the ambiguities, especially in cases when you want to embed a well-formed code into a string. It might be somewhat easier if you disallow multi-line strings.
在 Python 中,你可以毫无问题地执行
str = 'Hello "Jesus"'
或str = """Hello "Jesus"."""
in Python you can do
str = 'Hello "Jesus"'
without problem orstr = """Hello "Jesus"."""
某些语言使用不同类型的引号,因此可以在字符串文字中使用不同类型的引号。例如,Python 有双引号、单引号和三个双引号。
Bash 有某种形式的“用户可自定义”引用机制:
我喜欢这一点,因为在字符串文字包含 EOF 的情况下,您可以选择使用其他内容作为“引用结束”分隔符。
Some languages use different types of quotes, thus making it possible to have quotes of different types in string literals. For instance, Python has this with double quotes, single quotes and three double quotes.
Bash has some form of "user-customizable" quoting mechanism:
I like that, because in cases where your string literal contains EOF, you can just choose to use something else for the "end of quote" delimiter.