Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why, in general, are nested block comments NOT allowed? [closed]

In most the languages I use, you simply cannot nest block comments, because the first ocurrence of the "close" comment sintaxis closes the comment even if it was only an "inner" comment.

For example, in HTML

<!-- outer comment
<p>hello</p><!-- inner comment <p>world</p> -->
<p>this should BE commented</p>
-->

in this case, the outer comment ends on the first --> instead of the corresponding last one, causing the last <p> to print, when it shouldn't.

The same happens for languages that use /* */ for block comments, like in java, php, css, javascript, etc.

But my question is WHY is it that way? Why, by design, it is not allowed? I mention "by design" because I really doubt it is because of parsing problems, I guess the parsers are perfectly capable of keeping track of opening /*s and close the comments with their corresponding closing */s But they simply somehow decided it is not a good idea.

I already know that a workaround for this is to somehow change the inner closing comments, to avoid them to close , and only leave the last closing one. e.g. changing inner -->s and */s for - ->s and * /s . But that is obviously not convenient, and hard to do when you only want to discard blocks of code for debugging purposes. (other techniques are to nest everything in if(false){} blocks, but that is not the point here.

So, what I'd like to know is WHY nested comments are generally not allowed in several modern languages? there must be a good reason other than "others don't do it, we won't either" right?.

And as a plus, are there any other (not so obscure) languages that DO allow nested block comments?

like image 527
DiegoDD Avatar asked Jun 26 '13 18:06

DiegoDD


People also ask

Why nested comments are not allowed in C?

C and C++ do it for ease of parsing. This way, when they hit a comment start of /*, the parser can trivially scan to the end. Otherwise, it would have to set up and maintain a stack, and then report errors if the comment tokens are unmatched.

What is the advantages of allowing nested block comments?

The answer is nested comments allows commented-out code that contains comments itself example in C++ has block comments delimited by /../ that can span multiple lines and line comments delimited by //. Show activity on this post.

Can we Nest comments in Java?

Nested comments is not natively a feature of java. So what can you do? Here are some different options: Slash Slash comment nesting The best I've been able to come up with is to use an editor that has a hotkey to slashslash comment out an entire block of code as //'s can be nested.

Do nested comments exist in C++?

There are compilers that can be configured to support nested /* - */ style of comments. Code which uses nested comments either does not compile OR compiles to something different with compilers that don't support nested comments.


1 Answers

The reason is historical and has to do with the architecture of compilers.

For the sake of efficiency, most compilers traditionally parse the source code in two stages: the lexical analysis and the actual parsing of a token stream (that was produced by said lexical analysis). The lexical analysis is the part that recognises individual tokens, such as keywords, strings, number literals – and comments.

Again for reasons of efficiency, lexical analysis is traditionally implemented via a finite-state machine. These finite-state machines happen to recognise (= handle) regular languages, which fits perfectly for the above-mentioned tokens. However, it is not able to recognise nested constructs – this would require a more powerful machine (augmented by a stack).

Not allowing nested comments was thus simply a decision that traded off convenience for performance, and subsequent languages have by and large adopted the convention.

And as a plus, are there any other (not so obscure) languages that DO allow nested block comments?

There are some. The comments already mentioned Haskell and Pascal. Other languages are D and F#.

like image 157
Konrad Rudolph Avatar answered Sep 22 '22 11:09

Konrad Rudolph