11

In the ECMAScript grammar specification for Javascript, there are blocks defined with a double colon like this:

Literal ::
    NullLiteral
    BooleanLiteral
    NumericLiteral
    StringLiteral
    RegularExpressionLiteral

And blocks defined with a single colon like this:

PrimaryExpression :
    this
    Identifier
    Literal
    ArrayLiteral
    ObjectLiteral
    ( Expression )

And, even blocks with a triple colon:

uriCharacter :::
    uriReserved
    uriUnescaped
    uriEscaped

What is the difference between the single and double and triple colons?

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    It's explained in [Section 5.1: Syntactic and Lexical Grammars](http://www.ecma-international.org/ecma-262/5.1/#sec-5.1). – Leonid Beschastny Mar 28 '15 at 01:52
  • See sub-sections 5.1.1 to 5.1.5. – Leonid Beschastny Mar 28 '15 at 01:59
  • @LeonidBeschastny - I've seen and read that section, but don't know what it means. If you can explain what that means, please put it in an answer. – jfriend00 Mar 28 '15 at 02:04
  • @jfriend00 Application ? – guest271314 Mar 28 '15 at 02:48
  • 1
    @guest271314 - I'm attempting to understand some fine details of the JS syntax and figured the colon differences must mean something. – jfriend00 Mar 28 '15 at 03:06
  • @jfriend00 http://stackoverflow.com/questions/1823612/lexer-written-in-javascript , http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form , http://disnetdev.com/papers/sweetjs.pdf ? – guest271314 Mar 28 '15 at 03:32
  • @guest271314 - maybe I'm missing something, but I don't see how any of those documents answer the question about the difference between single and double colons. – jfriend00 Mar 28 '15 at 03:58
  • 1
    FYI, one of the reasons I was researching the JS Grammar is this question and answer: http://stackoverflow.com/questions/29250950/why-is-00-syntactically-valid/29251025#29251025 – jfriend00 Mar 28 '15 at 04:10
  • 1
    Just realized there are triple colons too (added that to the question). – jfriend00 Mar 28 '15 at 05:34
  • See also http://www.ecma-international.org/ecma-262/5.1/#sec-9.3.1 – guest271314 Mar 28 '15 at 05:40
  • @guest271314 - I've read the whole document. It isn't helping me to just point to portions of that document. I'm looking for someone to actually explain what the single, double and triple colons mean in words I would understand. – jfriend00 Mar 28 '15 at 06:30
  • @jfriend00 `:` and `::` appear to define how language read, or how to write custom `js` , or modify "native" `js` . page 5 at http://disnetdev.com/papers/sweetjs.pdf appear to , at least , address `::` and provide definition of process at section 2.3 Proving Read . At page 7 `:` appear to be addressed at `transformer : TokenTree∗ → TokenTree∗` defining macro ` = > x` . Plain language, no. Considered asking https://twitter.com/BrendanEich ? – guest271314 Mar 28 '15 at 18:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74014/discussion-between-guest271314-and-jfriend00). – guest271314 Mar 28 '15 at 22:08

2 Answers2

3

The JSON lexical grammar is used to translate character sequences into tokens and is similar to parts of the ECMAScript lexical grammar. The JSON syntactic grammar describes how sequences of tokens from the JSON lexical grammar can form syntactically correct JSON object descriptions.

Lexical rules ("::") for tokens mean "what the parts of the language look like". It defines rules like "5.5 is a number".

Syntactic rules (":") for expressions mean "how the parts interact with eachother". It defines rules like "5.5 abc doesn't make sense".

The triple-colon (":::") seems to be reserved specifically to define rules for converting strings to numbers. The string " 0x1235 " (with whitespace) is a valid number. The triple-colon rules define that.

The triple-colon (":::") also seems to be used for uri string grammar. Most commonly used like this: "f%20o%20o" decodes to "f o o". These rules define the structure of the "numerical" part of the strings.

000
  • 26,951
  • 10
  • 71
  • 101
  • 4
    While I'm sure this is correct, it's not clear how this distinction applies to the examples given. Can you clarify what the difference between the examples is? – Vitruvie Mar 28 '15 at 02:16
  • Can you explain how what you've written applies to the three examples I cited in my question because I'm not following your explanation as is. For example, why would Literal use `::`? The items listed after the `::` aren't about interacting with parts - they're a list of items that can all be a Literal by themselves. – jfriend00 Mar 28 '15 at 06:33
  • @jfriend00, did you figure it out? I believe that `:` denotes grammar used during lexical analysis to produce a stream of input tokens, while the `::` grammar is used during syntax analysis to produce a parse tree. – Max Koretskyi Aug 29 '17 at 08:54
1

See Standard ECMA-262 5.1 Edition / June 2011/

5.1.1 Context-Free Grammars

A context-free grammar consists of a number of productions. Each production has an abstract symbol called a nonterminal as its left-hand side, and a sequence of zero or more nonterminal and terminal symbols as its right-hand side. For each grammar, the terminal symbols are drawn from a specified alphabet.

Starting from a sentence consisting of a single distinguished nonterminal, called the goal symbol, a given context-free grammar specifies a language, namely, the (perhaps infinite) set of possible sequences of terminal symbols that can result from repeatedly replacing any nonterminal in the sequence with a right-hand side of a production for which the nonterminal is the left-hand side.

5.1.6 Grammar Notation

Terminal symbols of the lexical and string grammars, and some of the terminal symbols of the syntactic grammar, are shown in fixed width font, both in the productions of the grammars and throughout this specification whenever the text directly refers to such a terminal symbol. These are to appear in a program exactly as written. All terminal symbol characters specified in this way are to be understood as the appropriate Unicode character from the ASCII range, as opposed to any similar-looking characters from other Unicode ranges.

Nonterminal symbols are shown in italic type. The definition of a nonterminal is introduced by the name of the nonterminal being defined followed by one or more colons. (The number of colons indicates to which grammar the production belongs.) One or more alternative right-hand sides for the nonterminal then follow on succeeding lines. For example, the syntactic definition:

   WhileStatement :
     while(Expression) Statement

states that the nonterminal WhileStatement represents the token while, followed by a left parenthesis token, followed by an Expression, followed by a right parenthesis token, followed by a Statement. The occurrences of Expression and Statement are themselves nonterminals. As another example, the syntactic definition:

   ArgumentList :
     AssignmentExpression
     ArgumentList , AssignmentExpression

:::

Productions of the numeric string grammar are distinguished by having three colons ":::" as punctuation.

::

Productions of the lexical and RegExp grammars are distinguished by having two colons "::" as separating punctuation. The lexical and RegExp grammars share some productions.

:

Productions of the syntactic grammar are distinguished by having just one colon ":" as punctuation.

Note,

5.1.5 The JSON Grammar

Productions of the JSON lexical grammar are distinguished by having two colons "::" as separating punctuation. The JSON lexical grammar uses some productions from the ECMAScript lexical grammar. The JSON syntactic grammar is similar to parts of the ECMAScript syntactic grammar. Productions of the JSON syntactic grammar are distinguished by using one colon ":" as separating punctuation.

guest271314
  • 1
  • 15
  • 104
  • 177
  • 2
    And what exactly are "production of the lexical and RegExp grammars", "productions of the syntactic grammar" and "productions of the numeric string grammar". I've read it in the spec and don't know what those are. I'm looking for someone to explain those in English. – jfriend00 Mar 28 '15 at 20:34
  • @jfriend00 If interpreting specification correctly, basically, appear to provide guidance for writing `js` ; when the actual language, or language implementation, is defined by the author of the particular implementation. See Conformance _"A conforming implementation of ECMAScript must provide and support all the types, values, objects, properties, functions, and program syntax and semantics described in this specification."_ At answer, `Production` would be `WhileStatement` `:` `// define stuff here utilizing Grammar` – guest271314 Mar 28 '15 at 20:56
  • @jfriend00 Have not actually written `js` from scratch at the core level , as described within document; though have composed `dtd`'s http://www.w3.org/TR/html4/sgml/dtd.html , and composed custom tags for use at `HTML` , `XML` . That is closest could compare, here, from actual composition. Though, could "see" how `js` could be written - even in `js` - to create custom implementation of `Production` -> `Grammar` utilizing patterns at document. Link at last comment, below original Question , appear to describe this process to a fair degree , using term "macros" . – guest271314 Mar 28 '15 at 21:03