Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

A few do, but not any of the popular ones as far as I know. Is there something bad about nesting comments?

I plan to have block comments nest in the (small) language I'm working on, but I would like to know if this is a bad idea.

share|improve this question
    
re a few answers: ohh, that makes sense =) I'm totally doing nested block comments then; although I do have a separate lexing stage, it's not the limiting sort SK-logic described. –  Vuntic Jun 2 '11 at 11:05
    
@Vuntic: If you have a separate lexing stage that uses stuff more complicated than regular expressions, you may have performance issues. REs are fast and easy to use by implementing DFAs. –  David Thornley Jun 2 '11 at 16:18
    
It catches more errors earlier to not allow nesting –  user1249 Jun 2 '11 at 16:21
2  
@David: ...not at all. It's actually really fast. –  sparkleshy Jun 2 '11 at 20:28
add comment

migrated from stackoverflow.com Jun 2 '11 at 15:52

This question came from our site for professional and enthusiast programmers.

8 Answers

up vote 8 down vote accepted

Because most of the implementations are using separate lexing and parsing stages, and for lexing they're using plain old regular expressions. Comments are treated as whitespaces - i.e., ignored tokens, and thus should be resolved entirely in a lexing pass. The only advantage of this approach is parsing speed. Numerous disadvantages include severe limitations on syntax (e.g., a need to maintain a fixed, context-independent set of keywords).

share|improve this answer
2  
I would disagree on 'most' nowadays. Certainly that's the traditional way, but I know that for C, EDG combines the preprocessor, lexing and parsing, and I suspect that both GCC and Microsoft do too. The benefit is that it allows you to implement them separately if you need to. –  Andrew Aylett Jun 2 '11 at 9:56
    
Clang is doing the same, too. But that's still only a tiny proportion of the existing popular languages compilers. –  SK-logic Jun 2 '11 at 10:00
    
@Neil Butterworth, take a look at mcs, javac, gcc (yep, it back-patches a lexer, but still it is a dedicated lexing pass), clang (same as gcc), dmd, fpc, and many, many more. –  SK-logic Jun 4 '11 at 23:09
add comment

It's perfectly possible to make a lexer that can handle nested comments. When it's eating whitespace, when it sees /* it can increment a depth counter, and decrement it when it sees */, and stop when the depth is zero. That said, I've done many parsers, and never found a good reason for comments to nest.

If comments can nest, then a downside is it's easy to get their ends unbalanced, and unless you have a fancy editor, it can invisibly hide code you assume is there.

An upside of comments that don't nest is something like this:

/*
some code
more code
blah blah blah
/**/

where you can easily comment the code in or out by removing or adding the first line - a 1-line edit. Of course, if that code itself contains a comment, this would break, unless you also allow C++-style // comments in there. So that's what I tend to do.

share|improve this answer
1  
// comments are also C99-style. –  JAB Jun 2 '11 at 20:58
add comment

Supporting nested block comments complicates the parser, which is both more work and it could increase the compile time. I guess it is not a very needed feature for a language, so it is better to use the time and effort on other improvements and optimizations.

In my opinion simplicity is always a good thing in designing anything. Keep in mind that it is easier to add a feature than to remove it. Once you allow nested comments and there are programs out there using it, you won't be able to take them out without breaking compatibility.

share|improve this answer
1  
+1 for "easier to add a feature than to remove it". –  R.. Jun 8 '11 at 0:49
add comment

One probable reason is that nested comments must be handled by the parser, since the flavor of regular expressions commonly used in lexers don't support recursion. The simple ones can be eliminated as whitespace by the lexer, so they're simpler to implement in that way.

share|improve this answer
2  
It's not the "flavor". The word "regular" in regular expression inherently excludes recursion. –  R.. Jun 8 '11 at 0:48
3  
@R: In mathematics, sure. But in programming, we have things that we call regexes that do support recursion. –  sparkleshy Jun 8 '11 at 19:33
add comment

Since nobody else mentioned it, I'll list a few languages that do support nested comments: Rexx, Modula-2, Modula-3, Oberon. Despite all the complaints here about difficulty and speed issues, none of those seem to have any huge problems.

share|improve this answer
1  
To which I add: Haskell, Frege –  Ingo Aug 24 '11 at 9:31
    
Supported by Scala too. –  Matt R Aug 13 '13 at 15:23
add comment

A good point of nesting block comments is that you can comment out large portions of code easily (well, almost, unless you have the block comment end sequence in a string constant).

An alternative method is to prepend a bunch of line with the line comment start sequence if you have an editor that supports it.

Haskell has nested block comments, but most people dont seem to notice or to complain about it. I guess this is because people that do not expect nested comments tend to avoid them as this would be a lexical error in other languages.

share|improve this answer
add comment

Who knows? I would guess because supporting nested comments is more work - you would have to maintain a stack of some sort, and because it complicates the language grammar.

share|improve this answer
add comment

Nested comments are mean extra work for the parser. Usually when you see the start of a comment you ignore everything until the end comment marker. I order to support nested comments you have to parse the text in the comments as well. The biggest issue though is that a programmer has to be careful to close all nested comments correctly or it will lead to compilation errors. Correctly implementing a compiler is something that can be done but keeping track of nested comments as a programmer is quite error prone and irritating.

share|improve this answer
1  
-1: not true. Sane parsers don't work like that. –  sparkleshy Jun 8 '11 at 19:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.