5 ms·
> Every C programmers (sic) knows you can't embed a multi-line comment in a multi-line comment. And every Standard ML programmer might find this to be a surpri
by susam 2y ago
> Every C programmers (sic) knows you can't embed a multi-line comment in a multi-line comment.
And every Standard ML programmer might find this to be a surprising limitation. The following is a valid Standard ML program:
(* (* Nested (**) *) comment *)
val _ = print "hello, world\n"
Here is the output:
$ sml < hello.sml
Standard ML of New Jersey (64-bit) v110.99.5 [built: Thu Mar 14 17:56:03 2024]
- = hello, world
$ mlton hello.sml && ./hello
hello, world
Given how C was considered one of the "expressive" languages when it arrived, it's curious that nested comments were never part of the language.
- dahart 2y agoThere are 3 things I find funny about that comment: ML didn’t have single-line comments, so same level of surprising limitation. I’ve never heard someone refer to C as “expressive”, but maybe it was in 1972 when compared to assembly. And what bearing does the comment syntax have on the expressiveness of a language? I would argue absolutely none at all, by definition. :P
- susam 2y ago> ML didn’t have single-line comments, so same level of surprising limitation. It is not quite clear to me why the lack of single-line comments is such a surprising limitation. After all, a single-line block comment can easily serve as a substitute. However, there is no straightforward workaround for the lack of nested block comments. > I’ve never heard someone refer to C as “expressive”, but maybe it was in 1972 when compared to assembly. I was thinking of Fortran in this context. For instance, Fortran 77 lacked function pointers and offered a limited set of control flow structures, along with cumbersome support for recursion. I know Fortran, with its native support for multidimensional arrays, excelled in numerical and scientific computing but C quickly became the preferred language for general purpose computing. While very few today would consider C a pinnacle of expressiveness, when I was learning C, the landscape of mainstream programming languages was much more restricted. In fact, the preface to the first edition of K&R notes the following: "In our experience, C has proven to be a pleasant, expressive and versatile language for a wide variety of programs." C, Pascal, etc. stood out as some of the few mainstream programming languages that offered a reasonable level of expressiveness. Of course, Lisp was exceptionally expressive in its own right, but it wasn't always the best fit for certain applications or environments. > And what bearing does the comment syntax have on the expressiveness of a language? Nothing at all. I agree. The expressiveness of C comes from its grammar, which the language parser handles. Support for nested comments, in the context of C, is a concern for the lexer, so indeed one does not directly influence the other. However, it is still curious that a language with such a sophisticated grammar and parser could not allocate a bit of its complexity budget to support nested comments in its lexer. This is a trivial matter, I know, but I still couldn't help but wonder about it.
- dahart 2y agoFair enough. From my perspective, lack of single line comments is a little surprising because most other languages had it at the time (1973, when ML was introduced). Lack of nested comments doesn’t seem surprising, because it isn’t an important feature for a language, and because most other languages did not have it at the time (1972, when C was introduced). I can imagine both pro and con arguments for supporting nested comments, but regardless of what I think, C certainly could have added support for nested comments at any time, and hasn’t, which suggests that there isn’t sufficient need for it. That might be the entire explanation: not even worth a little complexity.
- masfuerte 2y agoAFAIK, C didn't get single line comments until C99. They were a C++ feature originally.
- dahart 2y agoOh wow, I didn’t remember that, and I did start writing C before 99. I stand corrected. I guess that is a little surprising. ;) Is true that many languages had single line comments? Maybe I’m forgetting more, but I remember everything else having single line comments… asm, basic, shell. I used Pascal in the 80s and apparently forgot it didn’t have line comments either?
- masfuerte 2y agoThat's my recollection, that most languages had single line comments. Some had multi-line comments but C++ is the first I remember having syntaxes for both. That said, I'm not terribly familiar with pre-80s stuff.
- quietbritishjim 2y agoSome C compilers supported it as an unofficial extension well before C99, so that could be why you didn't realise or don't remember. I think that included both Visual Studio (which was really a C++ compiler that could turn off the C++ bits) and GCC with GNU extensions enabled.
- michaelcampbell 2y agoI was barely too young for this to make much of an impact at the time, (but older than many, perhaps most, here), I understand why C was considered a "high level language", but it still hits me weird, given today's context.
- gsliepen 2y agoWell there is one way to nest comments in C, and that's by using #if 0: #if 0 This is a #if 0 nested comment! #endif #endif
- fanf2 2y agoExcept that text inside #if 0 still has to lex correctly. (unifdef has some evil code to support using C-style preprocessor directives with non-C source, which mostly boils down to ignoring comments. I don’t recommend it!)
- dahart 2y ago> Except that text inside #if 0 still has to lex correctly. Are you sure? I just tried on godbolt and that’s not true with gcc 14.2. I’ve definitely put syntax errors intentionally into #if 0 blocks and had it compile. Are you thinking of some older version or something? I thought the pre-processor ran before the lexer since always…
- fanf2 2y agoThere are three (relevant) phases (see “translation phases” in section 5 of the standard): • program is lexed into preprocessing tokens; comments turn into whitespace • preprocessor does its thing • preprocessor tokens are turned into proper tokens; different kinds of number are disambiguated; keywords and identifiers are disambiguated If you put an unclosed comment inside #if 0 then it won’t work as you might expect.
- dahart 2y agoAh, I see. You’re right!
- kragen 2y agoThis is not just true of Standard ML; it's also true of regular ML.
- layer8 2y agoLexing nested comments requires maintaining a stack (or at least a nesting-level counter). That wasn’t traditionally seen as being within the realm of lexical analysis, which would only use a finite-state automaton, like regular expressions.
- akira2501 2y agoPascal always supported the same nested comment syntax as your example.