递归注释的语言支持

发布于 12-11 10:12 字数 886 浏览 0 评论 0原文

我使用过的大多数语言都不支持递归注释。

  1. 语言设计者有什么理由选择不实现这个呢?
  2. 是不是很复杂?
  3. 会不会产生不良结果?

递归注释的示例:

/*
for (int j = 0; j <= SourceTexture.Height; j += SampleSize)
{
    ...
}

// Comment within comment below:
/*for (int i = 0; i < TextureColour.Length; i++)
{
    ...
}*/


sourceTexture.SetData<Color>(TextureColour);
*/

编辑:我理解到目前为止答案的论点(当字符串中有注释标记时会出现问题)。然而,我困惑的原因是你现在遇到了这个问题。

例如,我知道下面的代码不会给出预期的结果。

/*
    char *str = "/* string";
    // Are we now 1 level inside a comment or 2 levels?

*/

printf("Hello world");

/*
    char *str2 = "string */";
*/

但在我看来,这与以下情况下的意外结果没有什么不同:

/*
CODE "*/";
*/

这也会产生意外/不期望的结果。

因此,虽然这可能是递归注释的问题,但我关于为什么这不是不这样做的理由的论点是,这已经是非递归注释的问题。作为一名程序员,我知道编译器的行为是这样的,并且我会解决它。我认为用递归注释解决同样的问题并不需要付出更多的努力。

Most languages I've worked with don't have support for recursive comments.

  1. Is there any reason why language designers would choose not to implement this?
  2. Is it deceptively complex?
  3. Would it have undesired results?

Example of a recursive comment:

/*
for (int j = 0; j <= SourceTexture.Height; j += SampleSize)
{
    ...
}

// Comment within comment below:
/*for (int i = 0; i < TextureColour.Length; i++)
{
    ...
}*/


sourceTexture.SetData<Color>(TextureColour);
*/

EDIT: I understand the argument of the answers so far (problems occur when you have comment tokens in strings). However, the reason for my confusion is that you have that problem now.

For example, i know the code below wouldn't give the expected result.

/*
    char *str = "/* string";
    // Are we now 1 level inside a comment or 2 levels?

*/

printf("Hello world");

/*
    char *str2 = "string */";
*/

But in my mind that's no different to an unexpected result in the case below:

/*
CODE "*/";
*/

Which would also yield an unexpected/undesired result.

So, while it could be a problem for recursive comments, my argument as to why that's not a reason not to do it, is that it is already a problem for non-recursive comments. As a programmer i know the compiler behaves like this and i work around it. I don't think it's much more effort to work around the same problem with recursive comments.

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

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

发布评论

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

评论(4

森末i2024-12-18 10:12:18

语言设计者有什么理由选择不这样做吗?
实现这个吗?

这使得词法分析更加难以实现。

它是不是看起来很复杂?

恕我直言,不,但这是主观的。

会产生不良结果吗?

很难说。您已经发现,即使是普通的块注释也会产生问题:

/* print ("*/");    */

我知道有 2 种具有嵌套块注释的语言: Haskell弗雷格

Is there any reason why language designers would choose not to
implement this?

It makes the lexical analysis more difficult to implement.

Is it deceptively complex?

IMHO, no, but this is subjective.

Would it have undesired results?

Hard to tell. You have already discovered that even normal block comments can make problems:

/* print ("*/");    */

I know 2 languages that have nesting block comments: Haskell and Frege.

小ぇ时光︴2024-12-18 10:12:18

我将举一个例子,也许会更清楚:

/*
    char *str = "/* string";
    // Are we now 1 level inside a comment or 2 levels?    
*/

printf("Hello world. Will this be printed? Or is it a comment?");

/*
    char *str2 = "string */";
*/

如果不解释注释中的内容,就无法解析注释中的注释。但是您无法解释评论中的内容,因为它是评论,因此根据定义是“人类文本”而不​​是“语言”。

I will make an example and perhaps it will be clearer:

/*
    char *str = "/* string";
    // Are we now 1 level inside a comment or 2 levels?    
*/

printf("Hello world. Will this be printed? Or is it a comment?");

/*
    char *str2 = "string */";
*/

You couldn't parse comments inside a comment without interpreting what is inside the comment. But you can't interpret what is inside a comment because it's a comment, so by definition "human text" and not "language".

逆光飞翔i2024-12-18 10:12:18

虽然C的多行注释不能嵌套,但是在C中使用#if 0 ... #endif或多或少可以达到递归注释的效果(我强烈建议你使用当你想要禁用一段代码时,正是出于这个原因)。

即使是设计得像帖子一样愚蠢的 C 预处理器也完全能够处理嵌套注释,就像它必须能够处理带有错误条件的嵌套 #if 指令一样。因此,这实际上与难以定义或解析的任何事情无关,因为尽管它使注释变得更加复杂,但它们仍然不会比预处理中完成的其他事情更复杂。

但是,使用 #if 0 ... #endif 当然要求您尝试排除的代码中不存在任何不匹配的 #endif

从根本上来说,注释不能同时是(a)完全非结构化和(b)递归的。无论是偶然还是有意的选择,C 都选择了 (a)——注释文本不必遵守任何语法约束,除了不包含注释终止符序列(或等价的三字符组,例如 *??/< ;换行符>/)。

Although C's multi-line comments can't be nested, the effect of recursive comments can more-or-less be achieved in C using #if 0 ... #endif (and I strongly recommend you use that when you want to disable a block of code, for exactly that reason).

Even the C preprocessor, designed to be as dumb as a post, would be perfectly capable of handling nested comments, just as it has to be capable of handling nested #if directives with false conditions. So it's not really to do with anything being difficult to define or parse, since although it makes comments more complex, they'd still be no more complex than other things done in preprocessing.

But, using #if 0 ... #endif requires of course that there not be any unmatched #endif in the code you're trying to exclude.

Fundamentally comments cannot be both (a) completely unstructured and (b) recursive. Either by happenstance or deliberate choice, C has gone with (a) -- commented text doesn't have to obey any syntax constraints other than not containing the comment-terminator sequence (or trigraph equivalent such as *??/<newline>/).

电影里的梦2024-12-18 10:12:18

我相信它只是从一开始就没有被考虑过,随着事情的发展,它变成了一个“不重要”的功能添加。此外,它还需要更多的验证。

示例场景...

MyLang 版本 1:目标

  • 提供多行注释

开发者:嗯.. 我知道,每次我找到 /* 我都会注释所有内容,直到下一个 */ - 简单!

MyLang 版本 1 发布

1 天后...

用户:呃...我无法进行递归注释,请帮助我。

支持:请稍等。

30 分钟后...

支持经理 ->开发者:用户不能进行递归注释。

开发者:(什么是递归...)等等...

30 分钟后

开发者:是的,我们不支持递归注释。

I believe it is just never considered to begin with and it becomes a "non-important" feature addition as things develop. Also, it requires a lot more validation.

Example scenario...

MyLang version 1: Objective

  • Provide Multi-line commenting

Developer: hmmm.. I know, every time I find a /* I will comment everything until the next */ - easy!

MyLang version 1 release

1 day later...

User: erm... I cant do recursive comments, help me.

Support: Please hold.

30 mins later...

Support Manager -> Developer: User cannot do recursive comments.

Developer: (What's a recursive...) hang on...

30 mins later

Developer: yeah, we dont support recursive commenting.

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