3 ms·
Every time I have to deal with the declarator syntax in C or C++, I can't help but ponder what K&R were thinking when they designed this. It's not like there we
by int_19h 10y ago
Every time I have to deal with the declarator syntax in C or C++, I can't help but ponder what K&R were thinking when they designed this. It's not like there weren't other languages back then with a saner approach.
It looks like what they did was take the syntax from B:
auto x[10];
and generalize it such that the type name ended up before the variable name, as in Algol. But in B this worked much better, because it didn't have array types (or pointer types, or function types) - everything was a machine word. So [] in a variable declaration was just to allocate memory to which the variable would refer; the variable itself would still be a word. When they made [] part of the type, and added pointers and function types, the result was a mess.
- clishem 10y agoWhat if I told you that even declarations like char const *(* const (*(*ip)())[])[] are trivial to read? [Read this](http://www.icce.rug.nl/documents/cplusplus/cplusplus03.html#an116 http://www.icce.rug.nl/documents/cplusplus/cplusplus03.html#...) and you'll never struggle with any declaration ever again.
- Rangi42 10y ago"Unambiguous" is not the same as "trivial". C code generally reads left to right, so a trivial syntax for declaring, say, an array of const pointers to functions that take a const pointer to a char and return a const pointer to a char, would have tokens for those things in that order.
- userbinator 10y agoC code generally reads left to right No it doesn't: p += k[foo(bar, baz, 3*(quux+1))] << 32-m[++i];
- int_19h 10y agoI know the rules, and how to apply them. But they are not trivial. The fact that such documents need to be written in the first place, and the fact that utilities like cdecl exist, is a strong testimony to that.
- Animats 10y agoWhat they were thinking can be see in K&R C - there is no "typedef" in early C. Without typedef, the syntax of C is context-independent and LALR-1. You don't have to know if a name is a type to parse the syntax. Then came "typedef", which broke parsing. C parsing became context-dependent. To parse C with "typedef", and especially C++, you must read all the header files first. With name-first declaration syntax (Pascal, Modula, Go, Rust), parsing is context-independent again. Readability improves. Error messages improve. Syntax-coloring editors with a single-file view don't get lost.
- int_19h 10y agoThat explains how they ended up with context-dependent syntax - you're right, in the absence of typedefs, it's context-free to parse because of tags. But it can still be a pain to read, if you run into things like arrays of pointers to functions.
- swhipple 10y agoThat's interesting -- I was wondering in which cases typedef changes the parse tree, and came across a few [1]: a (b); /* function call or declaration */ a * b; /* multiplication or declaration */ f((a) * b); /* multiplication or deref and cast */ > With one further change, namely deleting the production typedef-name: identifier and making typedef-name a terminal symbol, this grammar is acceptable to the YACC parser-generator. [1] http://eli.thegreenplace.net/2007/11/24/the-context-sensitivity-of-cs-grammar http://eli.thegreenplace.net/2007/11/24/the-context-sensitiv...
- int_19h 10y agoС++ takes it all the way to 11 with templates. Here's a program that is parsed differently depending on whether pointers are 32-bit or 64-bit: template<size_t N = sizeof(void*)> struct a; template<> struct a<4> { enum { b }; }; template<> struct a<8> { template<int> struct b {}; }; enum { c, d }; int main() { a<>::b<c>d; d; } Depending on which instantiation is used, the first line of main is either a variable declaration, or two operators < and > applied in sequence. This is especially fun to deal with for C++ IDEs that support semantic highlighting (i.e. typenames are in a different color etc). If I remember correctly, the first one that could handle this right was VS 2012 - it only took 14 years after ISO C++ standard was released...
- gsg 10y agoThe idea was that the syntax for declaring a data type mirrors the syntax for using that datatype. See https://www.bell-labs.com/usr/dmr/www/chist.pdf https://www.bell-labs.com/usr/dmr/www/chist.pdf for Ritchie's own comments. It was not a successful idea, but there was a method to the madness.