JavaScript 1.4 Grammar
up

Monday, May 3, 1999

This is an LR(1) grammar written by waldemar that describes the state of ECMAScript as of February 1999. The grammar is complete except for semicolon insertion (the OptionalSemicolon grammar state can sometimes reduce to «empty») and distinguishing RegularExpression from / and /=. Also, there is some controversy about elision in array literals, so this feature has been omitted for now.

Grammar syntax

Grammar productions may expand nonterminals into empty right sides. Such right sides are indicated as «empty».

A number of rules in the grammar occur in groups of analogous rules. Rather than list them individually, these groups have been summarized using the shorthand illustrated by the example below:

Statements such as

  {normalinitial}
  {allowInnoIn}

introduce grammar arguments and . If these arguments later parametrize the nonterminal on the left side of a rule, that rule is implicitly replicated into a set of rules in each of which a grammar argument is consistently substituted by one of its variants. For example,

AssignmentExpression, 
   ConditionalExpression,
|  LeftSideExpression = AssignmentExpressionnormal,
|  LeftSideExpression CompoundAssignment AssignmentExpressionnormal,

expands into the following four rules:

AssignmentExpressionnormal,allowIn 
   ConditionalExpressionnormal,allowIn
|  LeftSideExpressionnormal = AssignmentExpressionnormal,allowIn
|  LeftSideExpressionnormal CompoundAssignment AssignmentExpressionnormal,allowIn
AssignmentExpressionnormal,noIn 
   ConditionalExpressionnormal,noIn
|  LeftSideExpressionnormal = AssignmentExpressionnormal,noIn
|  LeftSideExpressionnormal CompoundAssignment AssignmentExpressionnormal,noIn
AssignmentExpressioninitial,allowIn 
   ConditionalExpressioninitial,allowIn
|  LeftSideExpressioninitial = AssignmentExpressionnormal,allowIn
|  LeftSideExpressioninitial CompoundAssignment AssignmentExpressionnormal,allowIn
AssignmentExpressioninitial,noIn 
   ConditionalExpressioninitial,noIn
|  LeftSideExpressioninitial = AssignmentExpressionnormal,noIn
|  LeftSideExpressioninitial CompoundAssignment AssignmentExpressionnormal,noIn

AssignmentExpressionnormal,allowIn is now an unparametrized nonterminal and processed normally by the grammar.

Some of the expanded rules (such as the fourth one in the example above) may be unreachable from the starting nonterminal Program; these are ignored.

Expressions

  {normalinitial}
  {allowInnoIn}

Primary Expressions

PrimaryExpressionnormal 
   SimpleExpression
|  FunctionExpression
|  ObjectLiteral
PrimaryExpressioninitial  SimpleExpression
SimpleExpression 
   this
|  null
|  true
|  false
|  Number
|  String
|  Identifier
|  RegularExpression
|  ParenthesizedExpression
|  ArrayLiteral
ParenthesizedExpression  ( Expressionnormal,allowIn )

Function Expressions

FunctionExpression 
   AnonymousFunction
|  NamedFunction

Object Literals

ObjectLiteral 
   { }
|  { FieldList }
FieldList 
   LiteralField
|  FieldList , LiteralField
LiteralField  Identifier : AssignmentExpressionnormal,allowIn

Array Literals

ArrayLiteral 
   [ ]
|  [ ElementList ]
ElementList 
   LiteralElement
|  ElementList , LiteralElement
LiteralElement  AssignmentExpressionnormal,allowIn

Left-Side Expressions

LeftSideExpression 
   CallExpression
|  ShortNewExpression
CallExpression 
   PrimaryExpression
|  FullNewExpression
|  CallExpression MemberOperator
|  CallExpression Arguments
FullNewExpression  new FullNewSubexpression Arguments
ShortNewExpression  new ShortNewSubexpression
FullNewSubexpression 
   PrimaryExpressionnormal
|  FullNewExpression
|  FullNewSubexpression MemberOperator
ShortNewSubexpression 
   FullNewSubexpression
|  ShortNewExpression
MemberOperator 
   [ Expressionnormal,allowIn ]
|  . Identifier
Arguments 
   ( )
|  ( ArgumentList )
ArgumentList 
   AssignmentExpressionnormal,allowIn
|  ArgumentList , AssignmentExpressionnormal,allowIn

Postfix Operators

PostfixExpression 
   LeftSideExpression
|  LeftSideExpression ++
|  LeftSideExpression --

Unary Operators

UnaryExpression 
   PostfixExpression
|  delete LeftSideExpressionnormal
|  void UnaryExpressionnormal
|  typeof UnaryExpressionnormal
|  ++ LeftSideExpressionnormal
|  -- LeftSideExpressionnormal
|  + UnaryExpressionnormal
|  - UnaryExpressionnormal
|  ~ UnaryExpressionnormal
|  ! UnaryExpressionnormal

Multiplicative Operators

MultiplicativeExpression 
   UnaryExpression
|  MultiplicativeExpression * UnaryExpressionnormal
|  MultiplicativeExpression / UnaryExpressionnormal
|  MultiplicativeExpression % UnaryExpressionnormal

Additive Operators

AdditiveExpression 
   MultiplicativeExpression
|  AdditiveExpression + MultiplicativeExpressionnormal
|  AdditiveExpression - MultiplicativeExpressionnormal

Bitwise Shift Operators

ShiftExpression 
   AdditiveExpression
|  ShiftExpression << AdditiveExpressionnormal
|  ShiftExpression >> AdditiveExpressionnormal
|  ShiftExpression >>> AdditiveExpressionnormal

Relational Operators

RelationalExpression,allowIn 
   ShiftExpression
|  RelationalExpression,allowIn < ShiftExpressionnormal
|  RelationalExpression,allowIn > ShiftExpressionnormal
|  RelationalExpression,allowIn <= ShiftExpressionnormal
|  RelationalExpression,allowIn >= ShiftExpressionnormal
|  RelationalExpression,allowIn instanceof ShiftExpressionnormal
|  RelationalExpression,allowIn in ShiftExpressionnormal
RelationalExpression,noIn 
   ShiftExpression
|  RelationalExpression,noIn < ShiftExpressionnormal
|  RelationalExpression,noIn > ShiftExpressionnormal
|  RelationalExpression,noIn <= ShiftExpressionnormal
|  RelationalExpression,noIn >= ShiftExpressionnormal
|  RelationalExpression,noIn instanceof ShiftExpressionnormal

Equality Operators

EqualityExpression, 
   RelationalExpression,
|  EqualityExpression, == RelationalExpressionnormal,
|  EqualityExpression, != RelationalExpressionnormal,
|  EqualityExpression, === RelationalExpressionnormal,
|  EqualityExpression, !== RelationalExpressionnormal,

Binary Bitwise Operators

BitwiseAndExpression, 
   EqualityExpression,
|  BitwiseAndExpression, & EqualityExpressionnormal,
BitwiseXorExpression, 
   BitwiseAndExpression,
|  BitwiseXorExpression, ^ BitwiseAndExpressionnormal,
BitwiseOrExpression, 
   BitwiseXorExpression,
|  BitwiseOrExpression, | BitwiseXorExpressionnormal,

Binary Logical Operators

LogicalAndExpression, 
   BitwiseOrExpression,
|  LogicalAndExpression, && BitwiseOrExpressionnormal,
LogicalOrExpression, 
   LogicalAndExpression,
|  LogicalOrExpression, || LogicalAndExpressionnormal,

Conditional Operator

ConditionalExpression, 
   LogicalOrExpression,
|  LogicalOrExpression, ? AssignmentExpressionnormal, : AssignmentExpressionnormal,

Assignment Operators

AssignmentExpression, 
   ConditionalExpression,
|  LeftSideExpression = AssignmentExpressionnormal,
|  LeftSideExpression CompoundAssignment AssignmentExpressionnormal,
CompoundAssignment 
   *=
|  /=
|  %=
|  +=
|  -=
|  <<=
|  >>=
|  >>>=
|  &=
|  ^=
|  |=

Expressions

Expression, 
   AssignmentExpression,
|  Expression, , AssignmentExpressionnormal,
OptionalExpression 
   Expressionnormal,allowIn
|  «empty»

Statements

  {noShortIffull}
Statement 
   EmptyStatement
|  ExpressionStatement OptionalSemicolon
|  VariableDefinition OptionalSemicolon
|  Block
|  LabeledStatement
|  IfStatement
|  SwitchStatement
|  DoStatement OptionalSemicolon
|  WhileStatement
|  ForStatement
|  WithStatement
|  ContinueStatement OptionalSemicolon
|  BreakStatement OptionalSemicolon
|  ReturnStatement OptionalSemicolon
|  ThrowStatement OptionalSemicolon
|  TryStatement
OptionalSemicolon  ;

Empty Statement

EmptyStatement  ;

Expression Statement

ExpressionStatement  Expressioninitial,allowIn

Variable Definition

VariableDefinition  var VariableDeclarationListallowIn
VariableDeclarationList 
   VariableDeclaration
|  VariableDeclarationList , VariableDeclaration
VariableDeclaration  Identifier VariableInitializer
VariableInitializer 
   «empty»
|  = AssignmentExpressionnormal,

Block

Block  { BlockStatements }
BlockStatements 
   «empty»
|  BlockStatementsPrefix
BlockStatementsPrefix 
   Statementfull
|  BlockStatementsPrefix Statementfull

Labeled Statements

LabeledStatement  Identifier : Statement

If Statement

IfStatementfull 
   if ParenthesizedExpression Statementfull
|  if ParenthesizedExpression StatementnoShortIf else Statementfull
IfStatementnoShortIf  if ParenthesizedExpression StatementnoShortIf else StatementnoShortIf

Switch Statement

SwitchStatement 
   switch ParenthesizedExpression { }
|  switch ParenthesizedExpression { CaseGroups LastCaseGroup }
CaseGroups 
   «empty»
|  CaseGroups CaseGroup
CaseGroup  CaseGuards BlockStatementsPrefix
LastCaseGroup  CaseGuards BlockStatements
CaseGuards 
   CaseGuard
|  CaseGuards CaseGuard
CaseGuard 
   case Expressionnormal,allowIn :
|  default :

Do-While Statement

DoStatement  do Statementfull while ParenthesizedExpression

While Statement

WhileStatement  while ParenthesizedExpression Statement

For Statements

ForStatement 
   for ( ForInitializer ; OptionalExpression ; OptionalExpression ) Statement
|  for ( ForInBinding in Expressionnormal,allowIn ) Statement
ForInitializer 
   «empty»
|  Expressionnormal,noIn
|  var VariableDeclarationListnoIn
ForInBinding 
   LeftSideExpressionnormal
|  var VariableDeclarationnoIn

With Statement

WithStatement  with ParenthesizedExpression Statement

Continue and Break Statements

ContinueStatement  continue OptionalLabel
BreakStatement  break OptionalLabel
OptionalLabel 
   «empty»
|  Identifier

Return Statement

ReturnStatement  return OptionalExpression

Throw Statement

ThrowStatement  throw Expressionnormal,allowIn

Try Statement

TryStatement 
   try Block CatchClauses
|  try Block FinallyClause
|  try Block CatchClauses FinallyClause
CatchClauses 
   CatchClause
|  CatchClauses CatchClause
CatchClause  catch ( Identifier ) Block
FinallyClause  finally Block

Function Definition

FunctionDefinition  NamedFunction
AnonymousFunction  function FormalParametersAndBody
NamedFunction  function Identifier FormalParametersAndBody
FormalParametersAndBody  ( FormalParameters ) { TopStatements }
FormalParameters 
   «empty»
|  FormalParametersPrefix
FormalParametersPrefix 
   FormalParameter
|  FormalParametersPrefix , FormalParameter
FormalParameter  Identifier

Programs

Program  TopStatements
TopStatements 
   «empty»
|  TopStatementsPrefix
TopStatementsPrefix 
   TopStatement
|  TopStatementsPrefix TopStatement
TopStatement 
   Statementfull
|  FunctionDefinition

Waldemar Horwat
Last modified Monday, May 3, 1999
up