History of trailing comma in programming language grammars

后端 未结 4 1523
执念已碎
执念已碎 2020-11-30 10:16

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

4条回答
  •  无人及你
    2020-11-30 10:32

    [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.

提交回复
热议问题