JavaScript 2.0
Formal Description
Parser Grammar
previousupnext

Tuesday, February 15, 2000

This LALR(1) grammar describes the syntax of the JavaScript 2.0 proposal. The starting nonterminal is Program. See also the description of the grammar notation.

This document is also available as a Word 98 rtf file.

Terminals

General tokens: Identifier   Number   RegularExpression   String   VirtualSemicolon

Punctuation tokens: !   !=   !==   %   %=   &   &&   &&=   &=   (   )   *   *=   +   ++   +=   ,   -   --   -=   .   ...   /   /=   :   ::   ;   <   <<   <<=   <=   =   ==   ===   >   >=   >>   >>=   >>>   >>>=   ?   @   [   ]   ^   ^=   ^^   ^^=   {   |   |=   ||   ||=   }   ~

Future punctuation tokens: #   ->

Reserved words: break   case   catch   class   const   continue   default   delete   do   else   eval   extends   false   final   finally   for   function   if   in   instanceof   new   null   package   private   public   return   super   switch   this   throw   true   try   typeof   var   while   with

Future reserved words: abstract   debugger   enum   export   goto   implements   import   interface   native   protected   synchronized   throws   transient   volatile

Non-reserved words: get   language   set

Expressions

b  {allowInnoIn}

Identifiers

Identifier 
   Identifier
|  get
|  set
|  language
QualifiedIdentifier 
   Identifier
|  QualifiedIdentifier :: Identifier
|  ParenthesizedExpression :: Identifier

Primary Expressions

PrimaryExpression 
   null
|  true
|  false
|  Number
|  Number [no line break] String
|  String
|  this
|  super
|  QualifiedIdentifier
|  RegularExpression
|  ParenthesizedExpression
|  ParenthesizedExpression [no line break] String
|  ArrayLiteral
|  ObjectLiteral
|  FunctionExpression
ParenthesizedExpression  ( ExpressionallowIn )

Function Expressions

FunctionExpression 
   function FunctionSignature Block
|  function Identifier FunctionSignature Block

Object Literals

ObjectLiteral 
   { }
|  { FieldList }
FieldList 
   LiteralField
|  FieldList , LiteralField
LiteralField  FieldName : AssignmentExpressionallowIn
FieldName 
   QualifiedIdentifier
|  String
|  Number

Array Literals

ArrayLiteral  [ ElementList ]
ElementList 
   LiteralElement
|  ElementList , LiteralElement
LiteralElement 
   «empty»
|  AssignmentExpressionallowIn

Postfix Unary Operators

PostfixExpression 
   FullPostfixExpression
|  ShortNewExpression
FullPostfixExpression 
   PrimaryExpression
|  FullNewExpression
|  FullPostfixExpression MemberOperator
|  FullPostfixExpression Arguments
|  PostfixExpression [no line break] ++
|  PostfixExpression [no line break] --
FullNewExpression  new FullNewSubexpression Arguments
ShortNewExpression  new ShortNewSubexpression
FullNewSubexpression 
   PrimaryExpression
|  FullNewSubexpression MemberOperator
|  FullNewExpression
ShortNewSubexpression 
   FullNewSubexpression
|  ShortNewExpression
MemberOperator 
   [ ArgumentList ]
|  . QualifiedIdentifier
|  . ParenthesizedExpression
|  @ QualifiedIdentifier
|  @ ParenthesizedExpression
Arguments  ( ArgumentList )
ArgumentList 
   «empty»
|  ArgumentListPrefix
|  NamedArgumentListPrefix
ArgumentListPrefix 
   AssignmentExpressionallowIn
|  ArgumentListPrefix , AssignmentExpressionallowIn
NamedArgumentListPrefix 
   LiteralField
|  ArgumentListPrefix , LiteralField
|  NamedArgumentListPrefix , LiteralField

Prefix Unary Operators

UnaryExpression 
   PostfixExpression
|  delete PostfixExpression
|  typeof UnaryExpression
|  eval UnaryExpression
|  ++ PostfixExpression
|  -- PostfixExpression
|  + UnaryExpression
|  - UnaryExpression
|  ~ UnaryExpression
|  ! UnaryExpression

Multiplicative Operators

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

Additive Operators

AdditiveExpression 
   MultiplicativeExpression
|  AdditiveExpression + MultiplicativeExpression
|  AdditiveExpression - MultiplicativeExpression

Bitwise Shift Operators

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

Relational Operators

RelationalExpressionallowIn 
   ShiftExpression
|  RelationalExpressionallowIn < ShiftExpression
|  RelationalExpressionallowIn > ShiftExpression
|  RelationalExpressionallowIn <= ShiftExpression
|  RelationalExpressionallowIn >= ShiftExpression
|  RelationalExpressionallowIn instanceof ShiftExpression
|  RelationalExpressionallowIn in ShiftExpression
RelationalExpressionnoIn 
   ShiftExpression
|  RelationalExpressionnoIn < ShiftExpression
|  RelationalExpressionnoIn > ShiftExpression
|  RelationalExpressionnoIn <= ShiftExpression
|  RelationalExpressionnoIn >= ShiftExpression
|  RelationalExpressionnoIn instanceof ShiftExpression

Equality Operators

EqualityExpressionb 
   RelationalExpressionb
|  EqualityExpressionb == RelationalExpressionb
|  EqualityExpressionb != RelationalExpressionb
|  EqualityExpressionb === RelationalExpressionb
|  EqualityExpressionb !== RelationalExpressionb

Binary Bitwise Operators

BitwiseAndExpressionb 
   EqualityExpressionb
|  BitwiseAndExpressionb & EqualityExpressionb
BitwiseXorExpressionb 
   BitwiseAndExpressionb
|  BitwiseXorExpressionb ^ BitwiseAndExpressionb
BitwiseOrExpressionb 
   BitwiseXorExpressionb
|  BitwiseOrExpressionb | BitwiseXorExpressionb

Binary Logical Operators

LogicalAndExpressionb 
   BitwiseOrExpressionb
|  LogicalAndExpressionb && BitwiseOrExpressionb
LogicalXorExpressionb 
   LogicalAndExpressionb
|  LogicalXorExpressionb ^^ LogicalAndExpressionb
LogicalOrExpressionb 
   LogicalXorExpressionb
|  LogicalOrExpressionb || LogicalXorExpressionb

Conditional Operator

ConditionalExpressionb 
   LogicalOrExpressionb
|  LogicalOrExpressionb ? AssignmentExpressionb : AssignmentExpressionb
NonAssignmentExpressionb 
   LogicalOrExpressionb
|  LogicalOrExpressionb ? NonAssignmentExpressionb : NonAssignmentExpressionb

Assignment Operators

AssignmentExpressionb 
   ConditionalExpressionb
|  PostfixExpression = AssignmentExpressionb
|  PostfixExpression CompoundAssignment AssignmentExpressionb
CompoundAssignment 
   *=
|  /=
|  %=
|  +=
|  -=
|  <<=
|  >>=
|  >>>=
|  &=
|  ^=
|  |=
|  &&=
|  ^^=
|  ||=

Expressions

Expressionb 
   AssignmentExpressionb
|  Expressionb , AssignmentExpressionb
OptionalExpression 
   ExpressionallowIn
|  «empty»

Type Expressions

TypeExpressionb  NonAssignmentExpressionb

Statements

w  {abbrevabbrevNonEmptyabbrevNoShortIffull}
TopStatementw 
   Statementw
|  LanguageDeclarationw
|  PackageDefinition
Statementw 
   AnnotatedDefinitionw
|  EmptyStatementw
|  ExpressionStatement Semicolonw
|  AnnotatedBlock
|  LabeledStatementw
|  IfStatementw
|  SwitchStatement
|  DoStatement Semicolonw
|  WhileStatementw
|  ForStatementw
|  WithStatementw
|  ContinueStatement Semicolonw
|  BreakStatement Semicolonw
|  ReturnStatement Semicolonw
|  ThrowStatement Semicolonw
|  TryStatement
Semicolonabbrev 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonabbrevNonEmpty 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonabbrevNoShortIf 
   ;
|  VirtualSemicolon
|  «empty»
Semicolonfull 
   ;
|  VirtualSemicolon

Empty Statement

EmptyStatementabbrev 
   ;
|  «empty»
EmptyStatementabbrevNonEmpty  ;
EmptyStatementabbrevNoShortIf  ;
EmptyStatementfull  ;

Expression Statement

ExpressionStatement  [lookahead{function{}] ExpressionallowIn

Block

AnnotatedBlock  Attributes Block
Block  { TopStatements }
TopStatements 
   TopStatementabbrev
|  TopStatementsPrefix TopStatementabbrevNonEmpty
TopStatementsPrefix 
   TopStatementfull
|  TopStatementsPrefix TopStatementfull

Labeled Statements

LabeledStatementw  Identifier : Statementw

If Statement

IfStatementabbrev 
   if ParenthesizedExpression Statementabbrev
|  if ParenthesizedExpression StatementabbrevNoShortIf else Statementabbrev
IfStatementabbrevNonEmpty 
   if ParenthesizedExpression StatementabbrevNonEmpty
|  if ParenthesizedExpression StatementabbrevNoShortIf else StatementabbrevNonEmpty
IfStatementfull 
   if ParenthesizedExpression Statementfull
|  if ParenthesizedExpression StatementabbrevNoShortIf else Statementfull
IfStatementabbrevNoShortIf  if ParenthesizedExpression StatementabbrevNoShortIf else StatementabbrevNoShortIf

Switch Statement

SwitchStatement 
   switch ParenthesizedExpression { }
|  switch ParenthesizedExpression { CaseGroups LastCaseGroup }
CaseGroups 
   «empty»
|  CaseGroups CaseGroup
CaseGroup  CaseGuards CaseStatementsPrefix
LastCaseGroup  CaseGuards CaseStatements
CaseGuards 
   CaseGuard
|  CaseGuards CaseGuard
CaseGuard 
   case ExpressionallowIn :
|  default :
CaseStatements 
   Statementabbrev
|  CaseStatementsPrefix StatementabbrevNonEmpty
CaseStatementsPrefix 
   Statementfull
|  CaseStatementsPrefix Statementfull

Do-While Statement

DoStatement  do StatementabbrevNonEmpty while ParenthesizedExpression

While Statement

WhileStatementw  while ParenthesizedExpression Statementw

For Statements

ForStatementw 
   for ( ForInitializer ; OptionalExpression ; OptionalExpression ) Statementw
|  for ( ForInBinding in ExpressionallowIn ) Statementw
ForInitializer 
   «empty»
|  ExpressionnoIn
|  VariableDefinitionKind VariableBindingListnoIn
ForInBinding 
   PostfixExpression
|  VariableDefinitionKind VariableBindingnoIn

With Statement

WithStatementw  with ParenthesizedExpression Statementw

Continue and Break Statements

ContinueStatement  continue [no line break] OptionalLabel
BreakStatement  break [no line break] OptionalLabel
OptionalLabel 
   «empty»
|  Identifier

Return Statement

ReturnStatement  return [no line break] OptionalExpression

Throw Statement

ThrowStatement  throw [no line break] ExpressionallowIn

Try Statement

TryStatement 
   try AnnotatedBlock CatchClauses
|  try AnnotatedBlock FinallyClause
|  try AnnotatedBlock CatchClauses FinallyClause
CatchClauses 
   CatchClause
|  CatchClauses CatchClause
CatchClause  catch ( TypedIdentifierallowIn ) AnnotatedBlock
FinallyClause  finally AnnotatedBlock

Definitions

AnnotatedDefinitionw  Attributes Definitionw
Attributes 
   «empty»
|  FixedAttribute [no line break] Attributes
|  Identifier [no line break] Attributes
|  get [no line break] Attributes
|  set [no line break] Attributes
|  language [no line break] Attributes

The third through sixth Attributes productions are merely the result of manually inlining the Identifier rule inside Attributes Identifier [no line break] Attributes. Without manually inlining the Identifier rule here the grammar would not be LR(1).

FixedAttribute 
   private
|  public
|  final
Definitionw 
   VariableDefinition Semicolonw
|  FunctionDefinitionw
|  ClassDefinition

Variable Definition

VariableDefinition  VariableDefinitionKind VariableBindingListallowIn
VariableDefinitionKind 
   var
|  const
VariableBindingListb 
   VariableBindingb
|  VariableBindingListb , VariableBindingb
VariableBindingb  TypedIdentifierb VariableInitializerb
TypedIdentifierb 
   Identifier
|  Identifier : TypeExpressionb
VariableInitializerb 
   «empty»
|  = AssignmentExpressionb

Function Definition

FunctionDefinitionw 
   ConcreteFunctionDefinition
|  AbstractFunctionDefinitionw
ConcreteFunctionDefinition  function FunctionName FunctionSignature Block
AbstractFunctionDefinitionw  function FunctionName FunctionSignature Semicolonw
FunctionName 
   Identifier
|  get [no line break] Identifier
|  set [no line break] Identifier
|  new [no line break] Identifier
|  new
FunctionSignature  ParameterSignature ResultSignature
ParameterSignature  ( Parameters )
Parameters 
   «empty»
|  RestParameter
|  RequiredParameters
|  OptionalParameters
|  RequiredParameters , RestParameter
|  OptionalParameters , RestParameter
RequiredParameters 
   RequiredParameter
|  RequiredParameters , RequiredParameter
OptionalParameters 
   OptionalParameter
|  RequiredParameters , OptionalParameter
|  OptionalParameters , OptionalParameter
RequiredParameter  TypedIdentifierallowIn
OptionalParameter  TypedIdentifierallowIn = AssignmentExpressionallowIn
RestParameter 
   ...
|  ... TypedIdentifierallowIn
|  ... TypedIdentifierallowIn = AssignmentExpressionallowIn
ResultSignature 
   «empty»
|  : TypeExpressionallowIn

Class Definition

ClassDefinition  class Identifier Superclasses Block
Superclasses 
   «empty»
|  extends TypeExpressionallowIn

Language Declaration

LanguageDeclarationw  language [no line break] LanguageIds LanguageAlternatives LanguageSemicolonw
LanguageSemicolonabbrev 
   ;
|  «empty»
LanguageSemicolonabbrevNonEmpty 
   ;
|  «empty»
LanguageSemicolonfull  ;
LanguageAlternatives 
   «empty»
|  |
|  | LanguageIds LanguageAlternatives
LanguageIds 
   Identifier LanguageIdsRest
|  get LanguageIdsRest
|  set LanguageIdsRest
|  language LanguageIdsRest
|  Number LanguageIdsRest
LanguageIdsRest 
   «empty»
|  [no line break] LanguageIds

The first through fourth LanguageIds productions are merely the result of manually inlining the Identifier rule inside LanguageIds Identifier LanguageIdsRest. Without manually inlining the Identifier rule here the grammar would not be LR(1).

Package Definition

PackageDefinition  package Identifier Block

Programs

Program  TopStatements

Waldemar Horwat
Last modified Tuesday, February 15, 2000
previousupnext