如何找到有关给定语言(例如 bash)的空白要求的精确且清晰的信息?

发布于 2024-12-12 06:03:35 字数 303 浏览 0 评论 0原文

语言有不同的空白要求,例如,python 非常严格地指定所有内容的间隔方式。如果您可以提供从手册或其他文件中识别这些要求的一般程序,那将会很有帮助。一个具体的例子 - bash - 会非常有帮助。这是 bash man 的链接: https://www.gnu.org/software/bash/manual 。您可以引用那里的内容来回答,但也请说明您如何查找该信息。这就是重点 - 找到一个有效的过程来了解 lang 的空白要求,而无需事先了解它。

Langs have different whitespace requirements, e.g., python specifies very strictly how everything has to be spaced. If you could provide a general procedure for identifying these requirements from the manual or otherwise, that would be helpful. A specific example - bash - would be very helpful. This is the link to bash man: https://www.gnu.org/software/bash/manual. You may quote from there to answer, but please also indicate how you looked for the info. This is the point - to find an efficient procedure to learn about a lang's whitespace requirements without knowing it beforehand.

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

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

发布评论

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

评论(2

黑白记忆 2024-12-19 06:03:35

我认为 bash 没有特殊的空间要求。然而,有些地方必须有空格 - 例如:

if [ "a$x" != "a" ]; then
      ...
fi 

在 '[' 之后写没有空格的 ["a$x" 是行不通的,因为 '[' 通常是指向 test 实用程序的链接,所以如果后面没有空格,bash 将搜索 [a$x 可执行文件来执行。但这是特殊情况。我认为 bash 对空格没有更多要求。

很少有编程/脚本语言使用空格/制表符作为语言的组成部分。
例如,makefile 使用制表符来执行规则命令:

target: prerequisite1 prerequisite2
     command1  # the line starts with tab
     command2  # the line starts with tab

另一种依赖空格来理解代码的语言,正如您提到的,是 python。

其他语言没有指定任何空间要求,但为了便于阅读,您需要使用缩进。所有现代 IDE 都支持帮助您自动缩进代码。恕我直言,这组语言相当大 - C/C++/JAVA/bash/....

例如 C 代码:

  int foo()
     {
          int i = 0;  // compiler do not need the spaces (indentation) in the beginning of the line,
                      // It is required for programmer to easy understanding the code.
          ...
          return i;
     } 

您将代码写为:

  int foo(){int i=0;return i;}

但是还有其他语言具有不同的语法 - 例如 html/sed/.. . 其中空格具有不同的含义,具体取决于它们在表达式的哪个部分使用。

I don't think there's special space requirements for bash. However there're places that space is obligated - e.g:

if [ "a$x" != "a" ]; then
      ...
fi 

Writing ["a$x" without space after '[' won't work, since '[' is generally a link to test utility, so if no space after it bash will search for [a$x executable to execute. But this is special case. I don't think that bash has any more requirements regarding spaces.

There're few programming/scripting languages that use space/tab as an integral part of language.
For example makefile use tab for commands of rules:

target: prerequisite1 prerequisite2
     command1  # the line starts with tab
     command2  # the line starts with tab

Another language, which relies on spaces to understand the code, is as you mentioned is python.

Other langueages do not specify any space requirements, but for readibility you need to use indentation. All modern IDEs support help you automatically to indent the code. This group of languages is IMHO pretty big - C/C++/JAVA/bash/....

For example C code:

  int foo()
     {
          int i = 0;  // compiler do not need the spaces (indentation) in the beginning of the line,
                      // It is required for programmer to easy understanding the code.
          ...
          return i;
     } 

You code write it as:

  int foo(){int i=0;return i;}

But there're other languages that have different syntax - e.g. html/sed/... where spaces have different meaning depending in which part of expression they are used.

临风闻羌笛 2024-12-19 06:03:35

在大多数语言中,空格用作单词分隔符。所以在bash手册中,我会首先搜索“word”。第一次出现将引导我进入“分词”部分,在那里我将遇到 $IFS 并且我会进一步挖掘以了解其含义。

Python 示例并不是您可以选择的最佳示例,因为在 Python 中,空格用于定义代码块,因此除了作为单词分隔符之外,空格在语言语法中也发挥着重要作用。

如果您想深入了解像这样的主题(作为旁注,我真的不明白为什么空格比括号或任何其他符号更有趣),只需查找该语言的语法定义您感兴趣的符号,并查看您正在跟踪的符号实际上扮演什么角色。

你很少会发现某些语言有明确的“空格要求”,但你会发现很多语法要求(可能涉及很多空格约束——比如在 Python 中——或者很多括号约束——比如在 Lisp 中) 。

In most languages, the whitespace is used as word separator. So in the bash manual, I would first search for "word". The first occurrence would lead me to the "Word splitting" section and there I'll encounter the $IFS and I'd dig further to see what that's about.

The Python example is not really the best you could choose, as in Python white spaces are used to define blocks of code, so white space plays an important role in the language syntax, besides being a word separator.

If you want to get to the roots of a topic like this one (as a side note, I don't really see why white space would be more interesting than parentheses or any other symbols), just look for the grammar definition of the language that you're interested in and see what role the symbols you're tracking are actually playing.

You will rarely find explicit "whitespace requirements" about some language, but you'll find a lot of syntax requirements (that may involve lots of whitespace contraints -- like in Python -- or a lot of parentheses constraints -- like in Lisp).

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