ES3 P63
ExpressionStatement :
[lookahead ? {{, function}] Expression ;
Note that an ExpressionStatement cannot start with an opening curly brace because that might make itambiguous with a Block. Also, an ExpressionStatement cannot start with the function keyword becausethat might make it ambiguous with a FunctionDeclaration.
上下文無關(guān)文法《ECMA-262, 3rd edition, December 1999》
Program :
SourceElements
SouceElements :
SouceElement
SouceElements SourceElement
SouceElement :
Statement
FunctionDeclaration
Statement :
Block
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement
LabelledStatement
SwitchStatement
ThrowStatement
TryStatement
ExpressionStatement :
[lookahead ? {{, function}] Expression ;
FunctionDeclaration :
function Identifier ( FormalParameterList[opt] ) { FunctionBody }
這幾個產(chǎn)生式表明了以Program
為根的語法樹結(jié)構(gòu)汇陆。
(1)首先,“function(){}()
”無法歸約為CallExpression
蒿偎。
CallExpression
<- LeftHandSideExpression
<- PostfixExpression
<- UnaryExpression
<- MultiplicativeExpression
<- AdditiveExpression
<- ShiftExpression
<- RelationalExpression
<- EqualityExpression
<- BitwiseANDExpression
<- BitwiseXORExpression
<- BitwiseORExpression
<- LogicalANDExpression
<- LogicalORExpression
<- ConditionalExpression
<- AssignmentExpression
<- Expression
[失敗了] <- ExpressionStatement
<- Statement
<- SourceElement
<- SourceElements
<- Program
失敗的原因是障癌,
<u></u>Expression
要想歸約為ExpressionStatement
筐付,就不能以左大括號“{
”或“function
”開頭幽钢。
(2)那么,我們只能認為“function(){}()
”是由兩部分構(gòu)成的了逊彭。
program
-> SourceElements
-> SourceElements SourceElement
-> SourceElement SourceElement
把“funtion(){}
”和“()
”都分別向SourceElement
歸約咸灿。
然而,這又不行侮叮。
(3)“funtion(){}
”無法歸約為SourceElement
析显。
因為,無論是先歸約為Statement
,還先歸約為FunctionDeclaration
都不行谷异。
歸約為Statement
要求不能以function
開頭分尸,(與上面的失敗原因相同)
歸約為FunctionDeclaration
要求必須指定函數(shù)名。
(4)然而歹嘹,我們又沒有其他歸約方法了箩绍,只能失敗了。
而加上符號以后尺上,
例如“!function(){}()
”材蛛,并不是以“function
開頭”的,
所以通過了Expression
到ExpressionStatement
的歸約怎抛。
!function(){}()
<- ! function Identifieropt ( FormalParameterListopt ) { FunctionBody } Arguments
<- ! FunctionExpression Arguments
<- ! MemberExpression Arguments
<- ! CallExpression
<- ! LeftHandSideExpression
<- ! PostfixExpression
<- ! UnaryExpression
<- UnaryExpression
<- MultiplicativeExpression
<- AdditiveExpression
<- ShiftExpression
<- RelationalExpression
<- EqualityExpression
<- BitwiseANDExpression
<- BitwiseXORExpression
<- BitwiseORExpression
<- LogicalANDExpression
<- LogicalORExpression
<- ConditionalExpression
<- AssignmentExpression
<- Expression
<- ExpressionStatement
<- Statement
<- SourceElement
<- SourceElements
<- Program
同理可以推導(dǎo)(function(){}())
卑吭,(function(){})()
值得指出的是,
“function(){}()
”就算指定了函數(shù)名马绝,也不行豆赏。
因為后面的“()
”也不能歸約為SourceElement
。
而“function f(){}(1)
”就是合法的富稻,
“function f(){}
”歸約為FunctionDeclaration
掷邦,
而“(1)
”可以歸約為Expression
,視為括號運算符椭赋,或稱為分組運算符抚岗。
(Expression)
<- PrimaryExpression
<- MemberExpression
<- NewExpression
<- LeftHandSideExpression
<- PostfixExpression
<- UnaryExpression
<- MultiplicativeExpression
<- AdditiveExpression
<- ShiftExpression
<- RelationalExpression
<- EqualityExpression
<- BitwiseANDExpression
<- BitwiseXORExpression
<- BitwiseORExpression
<- LogicalANDExpression
<- LogicalORExpression
<- ConditionalExpression
<- AssignmentExpression
<- Expression
示例1:(function(){})()
( function Identifieropt ( FormalParameterListopt ) { FunctionBody } ) Arguments
( FunctionExpression ) Arguments
(MemberExpression ) Arguments
(NewExpression ) Arguments
( LeftHandSideExpression ) Arguments
( PostfixExpression ) Arguments
( UnaryExpression ) Arguments
( MultiplicativeExpression ) Arguments
( AdditiveExpression ) Arguments
( ShiftExpression ) Arguments
( RelationalExpression ) Arguments
( EqualityExpression ) Arguments
( BitwiseANDExpression ) Arguments
( BitwiseXORExpression ) Arguments
( BitwiseORExpression ) Arguments
( LogicalANDExpression ) Arguments
( LogicalORExpression ) Arguments
( ConditionalExpression ) Arguments
( AssignmentExpression ) Arguments
( Expression ) Arguments
PrimaryExpression Arguments
MemberExpression Arguments
CallExpression
LeftHandSideExpression
PostfixExpression
UnaryExpression
MultiplicativeExpression
AdditiveExpression
ShiftExpression
RelationalExpression
EqualityExpression
BitwiseANDExpression
BitwiseXORExpression
BitwiseORExpression
LogicalANDExpression
LogicalORExpression
ConditionalExpression
AssignmentExpression
Expression
ExpressionStatement
Statement
SourceElement
SourceElements
Program
示例2:(function(){}())
( function Identifieropt ( FormalParameterListopt ) { FunctionBody }Arguments )
( FunctionExpressionArguments )
( MemberExpression Arguments )
( CallExpression )
( LeftHandSideExpression )
( PostfixExpression )
( UnaryExpression )
( MultiplicativeExpression )
( AdditiveExpression )
( ShiftExpression )
( RelationalExpression )
( EqualityExpression )
( BitwiseANDExpression )
( BitwiseXORExpression )
( BitwiseORExpression )
( LogicalANDExpression )
( LogicalORExpression )
( ConditionalExpression )
( AssignmentExpression )
( Expression )
PrimaryExpression
MemberExpression
NewExpression
LeftHandSideExpression
PostfixExpression
UnaryExpression
MultiplicativeExpression
AdditiveExpression
ShiftExpression
RelationalExpression
EqualityExpression
BitwiseANDExpression
BitwiseXORExpression
BitwiseORExpression
LogicalANDExpression
LogicalORExpression
ConditionalExpression
AssignmentExpression
Expression
ExpressionStatement
Statement
SourceElement
SourceElements
Program
示例3:!function(){}()
! function Identifieropt( FormalParameterListopt) { FunctionBody }Arguments
! FunctionExpressionArguments
! MemberExpression Arguments
! CallExpression
! LeftHandSideExpression
! PostfixExpression
! UnaryExpression
UnaryExpression
MultiplicativeExpression
AdditiveExpression
ShiftExpression
RelationalExpression
EqualityExpression
BitwiseANDExpression
BitwiseXORExpression
BitwiseORExpression
LogicalANDExpression
LogicalORExpression
ConditionalExpression
AssignmentExpression
Expression
ExpressionStatement
Statement
SourceElement
SourceElements
Program
示例4:function(){}()
functionIdentifier( FormalParameterListopt) { FunctionBody } SourceElement
FunctionDeclarationSourceElement
SourceElement SourceElement
SourceElements SourceElement
SourceElements
Program