Java – why are nested blocking annotations generally not allowed?

In most languages I use, you can't nest annotations at all, because "closing" comments. The first occurrence of sintaxis will close comments even if it's just an "internal" 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 external annotation should not be printed in the first > instead of the corresponding last, resulting in the last < p >

For languages that use / * * / block annotations, just like in Java, PHP, CSS, JavaScript, etc

But my question is why? Why is the design not allowed? I mentioned "design" because I really doubt it is because of the parsing problem. I guess the parser has the ability to track the open / *, and close the comments with their corresponding end * / s, but they just decide in some way whether it is a good idea

I already know that one solution to this is to change the internal closing comments in some way to avoid them closing and only leave the last closing For example, change the internal > s and * / s of – > s and * / s. But this is obviously inconvenient. When you just want to discard code blocks for debugging, it is difficult to do so Other techniques are to nest everything in if (false) {} blocks, but this is not the point here

So, what I want to know is why several modern languages generally do not allow nested comments? There must be a good reason, except "if others don't do it, we won't do it", right?

As a plus sign, is there any other (less ambiguous) language that allows nested block comments?

Solution

The reason is historical and related to the architecture of the compiler

To improve efficiency, most compilers usually parse the source code in two stages: lexical analysis and the actual parsing of the token stream (generated by the lexical analysis) Lexical analysis is the part of identifying personal tokens, such as keywords, strings, numbers, literals and comments

Thirdly, for efficiency reasons, lexical analysis is traditionally implemented through fine state machine These finite state machines just recognize (= handle) regular languages, which fully conforms to the above token However, it does not recognize nested constructs - this will require a more powerful machine (augmented by a stack)

Therefore, not allowing nested comments is only a decision of convenient performance, and later languages generally adopt conventions

somewhat. Haskell and Pascal have been mentioned in the comments Other languages are D and f #

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>