1 Syntactic and Lexical Grammars
1.1 Context-Free Grammars
一个context-free grammar包含许多productions。每个production都有一个称为非终止符的抽象符号as its left-hand side,以及一串非终止符和终止符的组合as its right-hand side。For each grammar, 终止符are drawn from a specified alphabet.
A chain production is a production that has exactly one nonterminal symbol on its right-hand side along with zero or more terminal symbols.
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.
1.2 The Lexical and RegExp Grammars
A lexical grammar for ECMAScript 在第12节给出。 This grammar has as its terminal symbols Unicode code points that conform to the rules for SourceCharacter defined in 11.1. It defines a set of productions, starting from the goal symbol InputElementDiv, InputElementTemplateTail, or InputElementRegExp, or InputElementRegExpOrTemplateTail, that describe how sequences of such code points are translated into a sequence of input elements.
除空白和注释外的输入元素构成ECMAScript语法的终止符,称为ECMAScript tokens。这些tokens是 reserved words, identifiers, literals, and punctuators of the ECMAScript language. Moreover, line terminators, although not considered to be tokens, also become part of the stream of input elements and guide the process of automatic semicolon insertion (12.9). Simple white space and single-line comments are discarded and do not appear in the stream of input elements for the syntactic grammar. A MultiLineComment (that is, a comment of the form /…/ regardless of whether it spans more than one line) is likewise simply discarded if it contains no line terminator; but if a MultiLineComment contains one or more line terminators, then it is replaced by a single line terminator, which becomes part of the stream of input elements for the syntactic grammar.
A RegExp grammar for ECMAScript is given in 22.2.1. This grammar also has as its terminal symbols the code points as defined by SourceCharacter. It defines a set of productions, starting from the goal symbol Pattern, that describe how sequences of code points are translated into regular expression patterns.
Productions of the lexical and RegExp grammars are distinguished by having two colons “::” 作为分隔符. The lexical and RegExp grammars share some productions.
1 终止符
终止符符号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 script exactly as written. All terminal symbol code points specified in this way are to be understood as the appropriate Unicode code points from the Basic Latin range, as opposed to any similar-looking code points from other Unicode ranges. A code point in a terminal symbol cannot be expressed by a \ UnicodeEscapeSequence.
2 非终止符
非终止符用斜体字表示,它的定义(这个定义本身被称为“production”)包括非终止符名字加上一个或多个冒号(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。
如下的句法定义中:
非终止符 WhileStatement 代表 token while 后跟着一个左括号,然后跟着一个 Expression,然后跟着一个右括号,接下来跟着一个Statement。Expression 和 Statement 本身也是非终止符。
另外一个例子:
表明 ArgumentList 可能表示一个 AssignmentExpression,也可能表示一个由逗号隔开的 ArgumentList 和 AssignmentExpression。这种 ArgumentList 的定义是递归的,结果是 ArgumentList 可能包含任意数量的 ArgumentExpression ,由逗号隔开。
3 opt 下缀
出现在一个终止符或非终止符后的opt下缀表示是一个可选symbol,下面是一个例子:
是如下的一个简单缩写:
在上面的例子中,非终止符ForStatement实际上有四个alternative right-hand sides
4 下标表示
一个非终止符可以通过“[parameters]”这样的下标注释来参数化, 也就是作为production中非终止符的后缀。”parameters“可能是单个name,也可能是由逗号隔开的一串name。一个参数化的production是非终止符名字加下划线加parameter的速记,如下是一个例子:
是如下的缩写:
一个production中的Right-hand side非终止符同样可以被parameterized,例如:
等同于:
而:
等同于:
一个非终止符可能可能同时有parameter list以及一个“opt”下标,例如:
是如下的一种简略表示:
在一个right-hand side非终止符的parameter name前使用?
使得这个parameter value dependent upon the occurrence of the parameter name on the reference to the current production’s left-hand side symbol. 例如:
是如下的一种简略表示:
如果一个right-hand side alternative使用“[+parameter]”前缀,那么这个alternative is only available if the named parameter was used in referencing the production’s nonterminal symbol.
是如下的一种简略缩写:
如果一个right-hand side可选项使用“[~parameter]”前缀,那么这个可选项is only available if the named parameter was not used in referencing the production’s nonterminal symbol
是如下的简略缩写:
When the words “one of” 出现在语法定义中的冒号之后时,这表示下面一行或几行的的每个终止符都是 an alternative definition. 例如,ECAMScript的lexical grammar包含这个production:
这仅是如下的一个简单缩写: