Comments serve as a sort of in-code documentation. When inserted into a program, they are effectively ignored by the compiler; they are solely intended to be used as notes by the humans that read source code. All comments are removed from the program at translation phase 3 by replacing each comment with a single whitespace character. C-style comments are usually used to comment large blocks of text or small fragments of code; however, they can be used to comment single lines.
My perfect language will of course support nested comments C's if DEAD is the canonical and best-designed example. In many languages you can just wrap the dead code in the equivalent of if DEAD. Leaving a comment, docstring, whatever, whose text is a bunch of dead code, is still the worst option for readability.
Believe it or not, most languages are not this opinionated. I also disagree about not commenting code. It's very useful in development and has occasional uses in production. Mostly of the this-obvious-solution-doesn't-work as a warning not to clean up what looks like bad code. It might be a bad idea to comment out code, but that is not the reason comments don't nest in C-derived languages.
The reason is that comments are discarded in the lexing stage and lexers were traditionally implemented as a finite state machine which does not support nested structures. This was to keep the scanner and compiler simple and fast which was important on the limited hardware at the time. Probably not important today, but many languages with C-derived syntax have kept the syntax for familiarity.
OTOH several newer languages does support nested comments. Show 1 more comment. SK-logic SK-logic 8, 4 4 gold badges 23 23 silver badges 35 35 bronze badges. I would disagree on 'most' nowadays. The benefit is that it allows you to implement them separately if you need to. Clang is doing the same, too. But that's still only a tiny proportion of the existing popular languages compilers. 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.
No one is using regular expressions in their lexing for any non-trivial compiler. Nuoji that'd be a dirty hack - constructing some special kind of a state machine for only certain lexemes - all for what? To keep doing things the way Dragon Book prescribed? There is no point in it when you can just go lexerless and use exactly the same mechanism for parsing tokens and token streams.
If you're lazy you'll use flex and alike and be restricted with it, otherwise you won't have a lexer at all. Show 21 more comments. Mike Dunlavey Mike Dunlavey It would be relatively simple for tokenizer to include code to verify that every end-comment mark contains the proper token for its matching start-comment block. A lexer which keep a stack is not really a lexer anymore, it is a parser.
The counter argument easy to create unbalanced comments makes no sense. The comments are part of the language, the compiler can and should bark on unbalanced comments just like on unbalanced braces or parentheses.
Rugxulo Rugxulo 61 1 1 silver badge 1 1 bronze badge. To which I add: Haskell, Frege — Ingo. Supported by Scala too. Of course. Swift does it as well. Ingo Ingo 3, 16 16 silver badges 23 23 bronze badges.
Speed is not a thing here, compile time wouldn't be affected. That being said, I agree with simplicity in design, but personally find it useful to be able to comment out code while still coding commented out code would stay there only temporarily and be removed as soon as possible, without making it into releases. But design—wise, in this specific case, adding nested comments later as a feature would as well break compatibility.
In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program Comments are statements that are not executed by the compiler and interpreter. All those who say programming isn't for kids, just haven't met the right mentors yet. The students will get to learn more about the world of programming in these free classes which will definitely help them in making a wise career choice in the future.
Single line comment Multi-line comment. It applies comment to a single line only. It is used to denote multi-line comment. It can apply comment to more than a single line. It is referred to as C-Style comment as it was introduced in C programming.
Connect and share knowledge within a single location that is structured and easy to search. This is a code to find out if there is nesting of comment in a sample. Can anyone tell me where am I going wrong? The modified lines make the program work as intended:. The following modification does this. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
0コメント