JavaScript Grammar is Not Context-Free!
it's say
Context-free grammars are not sufficiently powerful to express all the rules that define whether a stream of input elements form a valid ECMAScript Script or Module that may be evaluated
example would be:
It is a Syntax Error if any element of the BoundNames of FormalParameters also occurs in the LexicallyDeclaredNames of FunctionBody.
there's some interesting complexities going on in JavaScript grammar.
here's the spec
here's some interesting things.
• they divided the grammar into
- lexical grammar
- regex grammar
- numeric string grammar
- syntactic grammar
and
- lex and regex grammar uses 3 colon
:::
to separate lhs (left-hand-side) and rhs (right-hand-side) in the grammar production notation. - numeric string grammar uses 2 colon
::
- syntactic grammar uses 1 colon
:
• Instead of the typical VERTICAL LINE |
for rhs alternatives, it uses multiple lines. That is, each line is a alternative.
• it uses subscript opt to indicate optional nonterminal, instead of the more traditional square bracket []
.
• it uses a subscript [xyz] to represent alternatives of nonterminal on lhs. This is getting complex.
• the subscript [xyz] notation can be on the rhs, and combined with the opt
• now the sub-scripted nonterminal on the rhs, can be prefixed with a question mark, depends on which of the subscribed nonterminal on lhs is used.
• the rhs may start with square bracketed notation like this [+xyz]
. It means, this alternative is valid only if the lhs has the same parameter [xyz]
.
It may start with [~xyz]
, means, this alternative is valid only if the lhs does not have the same parameter.
• now, comes “not apparently context-free” things. A lookhead notation.
• another non-standard notation, is “[no LineTerminator here]”.
• and now adding plain English word “but not” into the BNF.