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