July 2000 Draft
JavaScript 2.0
Formal Description
Parser Grammar
previousupnext

Friday, May 26, 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   export   extends   false   final   finally   for   function   if   implements   import   in   instanceof   interface   new   null   package   private   public   return   static   super   switch   this   throw   true   try   typeof   var   volatile   while   with

Future reserved words: abstract   debugger   enum   goto   native   protected   synchronized   throws   transient

Non-reserved words: attribute   constructor   get   language   namespace   set   use

Expressions

β ∈ {allowInnoIn}

Identifiers

AttributeIdentifier 
   Identifier
|  get
|  set
Identifier 
   AttributeIdentifier
|  attribute
|  constructor
|  language
|  namespace
|  use
QualifiedIdentifier 
   Identifier
|  Qualifier :: QualifiedIdentifier
Qualifier 
   Identifier
|  public
|  package
|  private
|  super
|  ParenthesizedExpression

Primary Expressions

PrimaryExpression 
   null
|  true
|  false
|  public
|  Number
|  Number [no line break] String
|  String
|  this
|  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

EqualityExpressionβ 
   RelationalExpressionβ
|  EqualityExpressionβ == RelationalExpressionβ
|  EqualityExpressionβ != RelationalExpressionβ
|  EqualityExpressionβ === RelationalExpressionβ
|  EqualityExpressionβ !== RelationalExpressionβ

Binary Bitwise Operators

BitwiseAndExpressionβ 
   EqualityExpressionβ
|  BitwiseAndExpressionβ & EqualityExpressionβ
BitwiseXorExpressionβ 
   BitwiseAndExpressionβ
|  BitwiseXorExpressionβ ^ BitwiseAndExpressionβ
BitwiseOrExpressionβ 
   BitwiseXorExpressionβ
|  BitwiseOrExpressionβ | BitwiseXorExpressionβ

Binary Logical Operators

LogicalAndExpressionβ 
   BitwiseOrExpressionβ
|  LogicalAndExpressionβ && BitwiseOrExpressionβ
LogicalXorExpressionβ 
   LogicalAndExpressionβ
|  LogicalXorExpressionβ ^^ LogicalAndExpressionβ
LogicalOrExpressionβ 
   LogicalXorExpressionβ
|  LogicalOrExpressionβ || LogicalXorExpressionβ

Conditional Operator

ConditionalExpressionβ 
   LogicalOrExpressionβ
|  LogicalOrExpressionβ ? AssignmentExpressionβ : AssignmentExpressionβ
NonAssignmentExpressionβ 
   LogicalOrExpressionβ
|  LogicalOrExpressionβ ? NonAssignmentExpressionβ : NonAssignmentExpressionβ

Assignment Operators

AssignmentExpressionβ 
   ConditionalExpressionβ
|  PostfixExpression = AssignmentExpressionβ
|  PostfixExpression CompoundAssignment AssignmentExpressionβ
CompoundAssignment 
   *=
|  /=
|  %=
|  +=
|  -=
|  <<=
|  >>=
|  >>>=
|  &=
|  ^=
|  |=
|  &&=
|  ^^=
|  ||=

Expressions

Expressionβ 
   AssignmentExpressionβ
|  Expressionβ , AssignmentExpressionβ
OptionalExpression 
   ExpressionallowIn
|  «empty»

Type Expressions

TypeExpressionβ ⇒ NonAssignmentExpressionβ

Statements

ω ∈ {abbrevabbrevNoShortIffull}
TopStatementω 
   Statementω
|  AttributeDefinition NoninsertableSemicolonω
|  LanguageDeclaration NoninsertableSemicolonω
|  PackageDefinition
Statementω 
   AnnotatedDefinitionω
|  EmptyStatement
|  ExpressionStatement Semicolonω
|  AnnotatedBlock
|  LabeledStatementω
|  IfStatementω
|  SwitchStatement
|  DoStatement Semicolonω
|  WhileStatementω
|  ForStatementω
|  WithStatementω
|  ContinueStatement Semicolonω
|  BreakStatement Semicolonω
|  ReturnStatement Semicolonω
|  ThrowStatement Semicolonω
|  TryStatement
|  ImportStatement Semicolonω
|  UseStatement Semicolonω
Semicolonabbrev 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonabbrevNoShortIf 
   ;
|  VirtualSemicolon
|  «empty»
Semicolonfull 
   ;
|  VirtualSemicolon
NoninsertableSemicolonabbrev 
   ;
|  «empty»
NoninsertableSemicolonfull ⇒ ;

Empty Statement

EmptyStatement ⇒ ;

Expression Statement

ExpressionStatement ⇒ [lookahead∉{function{}] ExpressionallowIn

Block

AnnotatedBlock 
   Block
|  [lookahead∉{package}] Attributes Block
|  package [no line break] Block
Block ⇒ { TopStatements }
TopStatements 
   «empty»
|  TopStatementsPrefix TopStatementabbrev
TopStatementsPrefix 
   «empty»
|  TopStatementsPrefix TopStatementfull

Labeled Statements

LabeledStatementω ⇒ Identifier : Statementω

If Statement

IfStatementabbrev 
   if ParenthesizedExpression Statementabbrev
|  if ParenthesizedExpression StatementabbrevNoShortIf else Statementabbrev
IfStatementfull 
   if ParenthesizedExpression Statementfull
|  if ParenthesizedExpression StatementabbrevNoShortIf else Statementfull
IfStatementabbrevNoShortIf ⇒ if ParenthesizedExpression StatementabbrevNoShortIf else StatementabbrevNoShortIf

Switch Statement

SwitchStatement ⇒ switch ParenthesizedExpression { CaseStatements }
CaseStatementω 
   Statementω
|  CaseLabel
CaseLabel 
   case ExpressionallowIn :
|  default :
CaseStatements 
   «empty»
|  CaseLabel
|  CaseLabel CaseStatementsPrefix CaseStatementabbrev
CaseStatementsPrefix 
   «empty»
|  CaseStatementsPrefix CaseStatementfull

Do-While Statement

DoStatement ⇒ do Statementabbrev while ParenthesizedExpression

While Statement

WhileStatementω ⇒ while ParenthesizedExpression Statementω

For Statements

ForStatementω 
   for ( ForInitializer ; OptionalExpression ; OptionalExpression ) Statementω
|  for ( ForInBinding in ExpressionallowIn ) Statementω
ForInitializer 
   «empty»
|  ExpressionnoIn
|  Attributes VariableDefinitionKind VariableBindingListnoIn
ForInBinding 
   PostfixExpression
|  Attributes VariableDefinitionKind VariableBindingnoIn

With Statement

WithStatementω ⇒ with ParenthesizedExpression Statementω

Continue and Break Statements

ContinueStatement 
   continue
|  continue [no line break] Identifier
BreakStatement 
   break
|  break [no line break] Identifier

Return Statement

ReturnStatement 
   return
|  return [no line break] ExpressionallowIn

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 ( TypedIdentifier ) AnnotatedBlock
FinallyClause ⇒ finally AnnotatedBlock

Import Statement

ImportStatement 
   import ImportList
|  use [no line break] import ImportList
ImportList 
   ImportBinding
|  ImportList , ImportBinding
ImportBinding 
   PackageName
|  Identifier = PackageName
PackageName 
   String
|  CompoundPackageName
CompoundPackageName 
   Identifier
|  CompoundPackageName . Identifier

Use Statement

UseStatement ⇒ use [no line break] namespace NonassignmentExpressionList
NonassignmentExpressionList 
   NonAssignmentExpressionallowIn
|  NonassignmentExpressionList , NonAssignmentExpressionallowIn

Definitions

AnnotatedDefinitionω 
   Definitionω
|  [lookahead∉{package}] Attributes Definitionω
|  package [no line break] AnnotatedDefinitionω

The last two AnnotatedDefinitionω productions together have the same effect as AnnotatedDefinitionω Attributes Definitionω except that the latter would make the grammar non-LR(1).

Attributes 
   Attribute [no line break]
|  Attribute [no line break] Attributes
Attribute 
   AttributeIdentifier
|  final
|  package
|  private
|  public
|  static
|  volatile
Definitionω 
   ExportDefinition Semicolonω
|  VariableDefinition Semicolonω
|  FunctionDefinitionω
|  ClassDefinitionω
|  InterfaceDefinitionω
|  NamespaceDefinition Semicolonω

Export Definition

ExportDefinition ⇒ export ExportBindingList
ExportBindingList 
   ExportBinding
|  ExportBindingList , ExportBinding
ExportBinding 
   FunctionName
|  FunctionName = FunctionName

Variable Definition

VariableDefinition ⇒ VariableDefinitionKind VariableBindingListallowIn
VariableDefinitionKind 
   var
|  const
VariableBindingListβ 
   VariableBindingβ
|  VariableBindingListβ , VariableBindingβ
VariableBindingβ 
   TypedVariableβ
|  TypedVariableβ = AssignmentExpressionβ
TypedVariableβ 
   QualifiedIdentifier
|  QualifiedIdentifier : TypeExpressionβ

Function Definition

FunctionDefinitionω 
   FunctionDeclaration Block
|  FunctionDeclaration Semicolonω
FunctionDeclaration 
   function FunctionName FunctionSignature
|  constructor [no line break] Identifier FunctionSignature
FunctionName 
   QualifiedIdentifier
|  get [no line break] [lookahead∉{(}] QualifiedIdentifier
|  set [no line break] [lookahead∉{(}] QualifiedIdentifier
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 ⇒ TypedIdentifier
OptionalParameter ⇒ TypedIdentifier = AssignmentExpressionallowIn
TypedIdentifier 
   Identifier
|  Identifier : TypeExpressionallowIn
RestParameter 
   ...
|  ... TypedIdentifier
|  ... TypedIdentifier = AssignmentExpressionallowIn
ResultSignature 
   «empty»
|  : TypeExpressionallowIn

Class Definition

ClassDefinitionω 
   class QualifiedIdentifier Superclass ImplementsList Block
|  class QualifiedIdentifier Semicolonω
Superclass 
   «empty»
|  extends TypeExpressionallowIn
ImplementsList 
   «empty»
|  implements TypeExpressionList
TypeExpressionList 
   TypeExpressionallowIn
|  TypeExpressionList , TypeExpressionallowIn

Interface Definition

InterfaceDefinitionω 
   interface QualifiedIdentifier ExtendsList Block
|  interface QualifiedIdentifier Semicolonω
ExtendsList 
   «empty»
|  extends TypeExpressionList

Namespace Definition

NamespaceDefinition ⇒ namespace [no line break] Identifier ExtendsList

Attribute Definition

AttributeDefinition 
   attribute [no line break] Identifier =
|  default =
|  AttributeDefinition Attribute
|  AttributeDefinition namespace ( TypeExpressionallowIn )

Language Declaration

LanguageDeclaration ⇒ language [no line break] LanguageIds LanguageAlternatives
LanguageAlternatives 
   «empty»
|  |
|  | LanguageIds LanguageAlternatives
LanguageIds 
   LanguageId
|  LanguageId [no line break] LanguageIds
LanguageId 
   Identifier
|  Number

Package Definition

PackageDefinition ⇒ package [no line break] PackageName Block

Programs

Program ⇒ TopStatements

Waldemar Horwat
Last modified Friday, May 26, 2000
previousupnext