ECMAScript 1 Grammar

ECMAScript grammar as specified in ECMA-262 1st edition. This grammar is left recursive and is not friendly for LL parsers.


Program :
SourceElements

SourceElements :
SourceElement
SourceElements SourceElement

SourceElement :
Statement
FunctionDeclaration

Statement :
Block
VariableStatement
EmptyStatement
ExpressionStatement
IfStatement
IterationStatement
ContinueStatement
BreakStatement
ReturnStatement
WithStatement

Block :
{ StatementListopt }

StatementList :
Statement
StatementList Statement

VariableStatement :
var VariableDeclarationList ;

VariableDeclarationList :
VariableDeclaration
VariableDeclarationList , VariableDeclaration

VariableDeclaration :
Identifier Initializeropt
*Identifier as defined in Chapter 7 of the spec

Initializer :
= AssignmentExpression

AssignmentExpression :
ConditionalExpression
LeftHandSideExpression AssignmentOperator AssignmentExpression
*AssignmentOperator as defined in Chapter 7 of the spec

ConditionalExpression :
LogicalORExpression
LogicalORExpression ? AssignmentExpression : AssignmentExpression

LogicalORExpression :
LogicalANDExpression
LogicalORExpression || LogicalANDExpression

LogicalANDExpression :
BitwiseORExpression
LogicalANDExpression && BitwiseORExpression

BitwiseORExpression :
BitwiseXORExpression
BitwiseORExpression | BitwiseXORExpression

BitwiseXORExpression :
BitwiseANDExpression
BitwiseXORExpression ^ BitwiseANDExpression

BitwiseANDExpression :
EqualityExpression
BitwiseANDExpression & EqualityExpression

EqualityExpression :
RelationalExpression
EqualityExpression == RelationalExpression
EqualityExpression != RelationalExpression

RelationalExpression :
ShiftExpression
RelationalExpression < ShiftExpression
RelationalExpression > ShiftExpression
RelationalExpression <= ShiftExpression
RelationalExpression => ShiftExpression

ShiftExpression :
AdditiveExpression
ShiftExpression << AdditiveExpression
ShiftExpression >> AdditiveExpression
ShiftExpression >>> AdditiveExpression

AdditiveExpression :
MultiplicativeExpression
AdditiveExpression + MultiplicativeExpression
AdditiveExpression - MultiplicativeExpression

MultiplicativeExpression :
UnaryExpression
MultiplicativeExpression * UnaryExpression
MultiplicativeExpression / UnaryExpression
MultiplicativeExpression % UnaryExpression

UnaryExpression :
PostfixExpression
delete UnaryExpression
void UnaryExpression
typeof UnaryExpression
++ UnaryExpression
-- UnaryExpression
+ UnaryExpression
- UnaryExpression
~ UnaryExpression
! UnaryExpression

PostfixExpression :
LeftHandSideExpression
LeftHandSideExpression [no LineTerminator here] ++
LeftHandSideExpression [no LineTerminator here] --

LeftHandSideExpression :
NewExpression
CallExpression

NewExpression :
MemberExpression
new NewExpression

MemberExpression :
PrimaryExpression
MemberExpression [ Expression ]
MemberExpression . Identifier
new MemberExpression Arguments

PrimaryExpression :
this
Identifier
Literal
( Expression )
*Literal as defined in Chapter 7 of the spec

Expression :
AssignmentExpression
Expression , AssignmentExpression

Arguments :
( )
( ArgumentList )

ArgumentList :
AssignmentExpression
ArgumentList , AssignmentExpression

CallExpression :
MemberExpression Arguments
CallExpression Arguments
CallExpression [ Expression ]
CallExpression . Identifier

EmptyStatement :
;

ExpressionStatement :
Expression ;

IfStatement :
if ( Expression ) Statement else Statement
if ( Expression ) Statement

IterationStatement :
while ( Expression ) Statement
for ( Expression(opt) ; Expression(opt) ; Expression(opt) ) Statement
for ( var VariableDeclarationList ; Expression(opt) ; Expression(opt) ) Statement
for ( LeftHandSideExpression in Expression ) Statement
for ( var Identifier Initializer(opt) in Expression ) Statement

ContinueStatement :
continue ;

BreakStatement :
break ;

ReturnStatement :
return [no LineTerminator here] Expression(opt) ;

WithStatement :
with ( Expression ) Statement

FunctionDeclaration :
function Identifier ( FormalParameterList(opt) ) Block

FormalParameterList :
Identifier
FormalParameterList , Identifier