5 ms·
The huge advantage of C (before the advent of LLVM) was that it was relatively straightforward to write a C compiler for any exotic or not-so-exotic system, sin
by bluetomcat 10y ago
The huge advantage of C (before the advent of LLVM) was that it was relatively straightforward to write a C compiler for any exotic or not-so-exotic system, since the language has very simple semantics and many operations would correspond to single machine instructions. There are literally hundreds of production C compilers written to this day.
The syntax of the language is also designed to help the compiler writer and not the user – think of "declarations mirror use", the irrelevance of the order of type specifiers, etc.
- kevinnk 10y ago> The syntax of the language is also designed to help the compiler writer and not the user – think of "declarations mirror use", the irrelevance of the order of type specifiers, etc. Err, I would agree with the statement that the syntax of C (mostly) favors the compiler writer, but "declarations mirror use" is decidedly not a good example for a couple of reasons: 1. The grammar for type declarations is actually quite complicated[1] 2. Agol, which inspired C's type system, actually had a much more "compiler friendly" type syntax[2]. C's syntax is designed to be more programmer friendly. 3. Dennis Ritchie makes it pretty clear that "declarations mirror use" was intended to make use and declarations visually similar, which implies that it was done for the benefit for the programmer[3]. [1] Compare https://www.lysator.liu.se/c/ANSI-C-grammar-y.html https://www.lysator.liu.se/c/ANSI-C-grammar-y.html to [2]. [2] http://www.csci.csusb.edu/dick/samples/algol60.syntax.html#5.%20Declarations http://www.csci.csusb.edu/dick/samples/algol60.syntax.html#5... [3] "Analogical reasoning led to a declaration syntax for names mirroring that of the expression syntax in which the names typically appear...In all these cases the declaration of a variable resembles its usage in an expression whose type is the one named at the head of the declaration." from http://www.jslint.com/chistory.html http://www.jslint.com/chistory.html
- bluetomcat 10y agoThe declarator looks complex because of differences in the precedence and associativity between the asterisk, `[]` and `()` operators. The point is that the language uses a set of the same operators with the same precedence and associativity both in declarations and in normal expressions. In my view, a "user-oriented" declaration of an array of pointers would look like `pointer[10] of int`, and not `int *arr[10]`.
- barrkel 10y agoI'm guessing you mean `array[10] of pointer to int`. This is pretty close to Pascal syntax already: `array[0..9] of ^Integer`, where ^ is pronounced 'pointer to'. Pascal is a language that is genuinely easy to parse; it's designed to be LL(1). C is not; in particular, typedefs effectively introduce keywords, and the easiest way to parse C is to inform the tokenizer about typedefs as they're found so the parser can disambiguate things like `a (b);`; this looks simultaneously like declaring a variable and calling a function, depending on the type of 'a'.
- Roboprog 10y agoMaybe I'm missing something, but I think Pascal "TYPE" blocks and C "typedef" statements do pretty much the same thing. (they don't look the same, but you can use either to define record/structure types or enums, for example)
- barrkel 10y agoThe distinction is in the syntax. int main() { foo(bar); foo * bar; } If foo is a typedef, these are variable declarations, otherwise the first is a function call and the second multiplication (syntactically). These are different kinds of things that would normally be up to the parser to figure out (rather than semantic analysis later). These are well-known ambiguities relating to typedefs in C (well-known to compiler authors, at any rate). The situation doesn't come up in Pascal because variables go in the var block (starting with the 'var' keyword) and functions and procedures both start with keywords ('function' and 'procedure').
- Roboprog 10y agoOK, thanks. I was focusing on the "definition" part, rather than the "usage" part of the types. It never occurred to me to put variables names inside parens in a declaration when I did C, though. But OF COURSE it's legal obfuscation. As stated elsewhere, I don't know why C had to screw up Algol's syntax so badly. I guess K&R liked curly braces much more than BEGIN/END, and why stop there, once you get going. Maybe somebody had a love affair with APL???