4 ms·
One of the problems with the old notation is the ambiguity. What does each of the following mean? n =- 5 n = -5 n=-5
by ghewgill 5y ago
One of the problems with the old notation is the ambiguity. What does each of the following mean?
n =- 5
n = -5
n=-5
- hnlmorg 5y agoAhh yes, that’s a very good point.
- mhh__ 5y agoIf I were designing a new language and where dead-set on the =- form I would make it so: n =- 5 // n equals n - 5 n = -5 // n equals -5 n=-5 //Illegal This can be done, it just requires getting the lexer right.
- adrian_b 5y agoDennis Ritchie, in his paper about the history of C, explains that they became aware of these ambiguities only after the first C implementation, but they considered that it was a too big mistake, so they reversed the order in the next C version.
- Taniwha 5y agomaybe not quite the next version, from memory (and I ported compilers, and the kernels, for both) it was between v6 and v7 Unix
- dn3500 5y agoThat's correct, it wasn't the next version of the Ritchie compiler, it was a whole new compiler, the Portable C Compiler. It used yacc to generate the parser, and yacc complained about the ambiguity. They left the old syntax in for quite a while.
- Taniwha 5y agoYup, almost certainly PCC - which also still has struct fields in a global namespace as a (possible wonderful, possibly horrible) hack to provide the equivalent to unions
- jandrese 5y agoAnother problem: n =* p; Is p a pointer or an integer? Maybe you want to do pointer math, it's a very C thing to do.
- jamesfinlayson 5y agoThat was my thought too - seems like it would be ambiguous even with type information anyway as the readme says this iteration at least, any type can be converted to any other type.
- moonchild 5y agoThe modern notation contains a very similar 'ambiguity'. Compare: x---y, x-- - y, and x - --y.
- mburee 5y agoAnd sadly x-----y won't parse :(
- krylon 5y agoIt's been a while I used C, but I for one am kind of glad that doesn't parse. If you need to do this, you can use parentheses: x - (--y) or (x--) - y
- tom_ 5y agoAll parse fine, and unambiguously, due to the maximal munch rule. x---y is (x--)-y, which is semantically valid. x-----y is ((x--)--)-y - but it's a semantic error, as x-- is not an lvalue.