views:

359

answers:

4

Many programming languages allow trailing commas in their grammar following the last item in a list. Supposedly this was done to simplify automatic code generation, which is understandable.

As an example, the following is a perfectly legal array initialization in Java (JLS 10.6 Array Initializers):

int[] a = { 1, 2, 3, };

I'm curious if anyone knows which language was first to allow trailing commas such as these. Apparently C had it as far back as 1985.

Also, if anybody knows other grammar "peculiarities" of modern programming languages, I'd be very interested in hearing about those also. I read that Perl and Python for example are even more liberal in allowing trailing commas in other parts of their grammar.

+1  A: 

I just found out that a g77 Fortran compiler has the -fugly-comma Ugly Null Arguments flag, though it's a bit different (and as the name implies, rather ugly).

The -fugly-comma option enables use of a single trailing comma to mean “pass an extra trailing null argument” in a list of actual arguments to an external procedure, and use of an empty list of arguments to such a procedure to mean “pass a single null argument”.

For example, CALL FOO(,) means “pass two null arguments”, rather than “pass one null argument”. Also, CALL BAR() means “pass one null argument”.

I'm not sure which version of the language this first appeared in, though.

polygenelubricants
EEEwwwwww. Nasty.
Aiden Bell
Is that option-type-f: ugly-comma, or is that just "fugly comma". I really want to believe it's the latter.
Conspicuous Compiler
+2  A: 

I'm not an expert on the commas, but I know that standard Pascal was very persnickity about semi-colons being statement separators, not terminators. That meant you had to be very very careful about where you put one if you didn't want to get yelled at by the compiler.

Later Pascal-esque languages (C, Modula-2, Ada, etc.) had their standards written to accept the odd extra semicolon without behaving like you'd just peed in the cake mix.

T.E.D.
Speaking of semicolons, I'd be interested to see a chart on the usage frequency of semicolons in human history. According to Wikipedia, the earliest general use is in 1591. I suspect that there's a jump in its usage with every new curly-bracket style programming language invented.There's probably also a bump when people figure out that you can use it to wink at people `;)`
polygenelubricants
Seriously; I don't think it has altered the frequency I use them;
Aiden Bell
+1  A: 

[Does anybody know] other grammar "peculiarities" of modern programming languages?

One of my favorites, Modula-3, was designed in 1990 with Niklaus Wirth's blessing as the then-latest language in the "Pascal family". Does anyone else remember those awful fights about where semicolon should be a separator or a terminator? In Modula-3, the choice is yours! The EBNF for a sequence of statements is

stmt ::= BEGIN [stmt {; stmt} [;]] END

Similarly, when writing alternatives in a CASE statement, Modula-3 let you use the vertical bar | as either a separator or a prefix. So you could write

CASE c OF
| 'a', 'e', 'i', 'o', 'u' => RETURN Char.Vowel
| 'y' => RETURN Char.Semivowel
ELSE RETURN Char.Consonant
END

or you could leave off the initial bar, perhaps because you prefer to write OF in that position.

I think what I liked as much as the design itself was the designers' awareness that there was a religious war going on and their persistence in finding a way to support both sides. Let the programmer choose!


P.S. Objective Caml allows permissive use of | in case expressions whereas the earlier and closely related dialect Standard ML does not. As a result, case expressions are often uglier in Standard ML code.


EDIT: After seeing T.E.D.'s answer I checked the Modula-2 grammar and he's correct, Modula-2 also supported semicolon as terminator, but through the device of the empty statement, which makes stuff like

x := x + 1;;;;;; RETURN x

legal. I suppose that's not a bad thing. Modula-2 didn't allow flexible use of the case separator |, however; that seems to have originated with Modula-3.

Norman Ramsey
That's right. That's also how C got around the issue, I believe. Ada's solution is closer to Modula-3's, so that ugly-looking multi semicolon thing would not be legal. If you want an "empty statement" for some weird reason in Ada, you have to say `null;`
T.E.D.
+1  A: 

Something which has always galled me about C is that although it allows an extra trailing comma in an intializer list, it does not allow an extra trailing comma in an enumerator list (for defining the literals of an enumeration type). This little inconsistency has bitten me in the ass more times than I care to admit. And for no reason!

Norman Ramsey