April 2002 Draft
JavaScript 2.0
Formal Description
Syntactic Grammar
previousupnext

Tuesday, February 26, 2002

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: ! != !== % %= & && &&= &= ( ) * *= + ++ += , - -- -= . ... / /= : :: ; < << <<= <= = == === > >= >> >>= >>> >>>= ? [ ] ^ ^= ^^ ^^= { | |= || ||= } ~

Reserved words: abstract as break case catch class const continue default delete do else export extends false final finally for function if implements import in instanceof interface is namespace new null package private public return static super switch this throw true try typeof use var void while with

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

Non-reserved words: exclude get include named set

Expressions

  {allowInnoIn}

Identifiers

Identifier 
   Identifier
|  get
|  set
|  exclude
|  include
|  named

Qualified Identifiers

Qualifier 
   Identifier
|  public
|  private
SimpleQualifiedIdentifier 
   Identifier
|  Qualifier :: Identifier
ExpressionQualifiedIdentifier  ParenExpression :: Identifier
QualifiedIdentifier 
   SimpleQualifiedIdentifier
|  ExpressionQualifiedIdentifier

Unit Expressions

UnitExpression 
   ParenListExpression
|  Number [no line break] String
|  UnitExpression [no line break] String

Primary Expressions

PrimaryExpression 
   null
|  true
|  false
|  public
|  Number
|  String
|  this
|  RegularExpression
|  UnitExpression
|  ArrayLiteral
|  ObjectLiteral
|  FunctionExpression
ParenExpression  ( AssignmentExpressionallowIn )
ParenListExpression 
   ParenExpression
|  ( ListExpressionallowIn , AssignmentExpressionallowIn )

Function Expressions

FunctionExpression 
   function FunctionSignature Block
|  function Identifier FunctionSignature Block

Object Literals

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

Array Literals

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

Super Expressions

SuperExpression 
   super
|  FullSuperExpression
FullSuperExpression  super ParenExpression

Postfix Expressions

PostfixExpression 
   AttributeExpression
|  FullPostfixExpression
|  ShortNewExpression
PostfixExpressionOrSuper 
   PostfixExpression
|  SuperExpression
AttributeExpression 
   SimpleQualifiedIdentifier
|  AttributeExpression MemberOperator
|  AttributeExpression Arguments
FullPostfixExpression 
   PrimaryExpression
|  ExpressionQualifiedIdentifier
|  FullNewExpression
|  FullPostfixExpression MemberOperator
|  SuperExpression DotOperator
|  FullPostfixExpression Arguments
|  FullSuperExpression Arguments
|  PostfixExpressionOrSuper [no line break] ++
|  PostfixExpressionOrSuper [no line break] --
FullNewExpression 
   new FullNewSubexpression Arguments
|  new FullSuperExpression Arguments
FullNewSubexpression 
   PrimaryExpression
|  QualifiedIdentifier
|  FullNewExpression
|  FullNewSubexpression MemberOperator
|  SuperExpression DotOperator
ShortNewExpression 
   new ShortNewSubexpression
|  new SuperExpression
ShortNewSubexpression 
   FullNewSubexpression
|  ShortNewExpression

Member Operators

MemberOperator 
   DotOperator
|  . ParenExpression
DotOperator 
   . QualifiedIdentifier
|  Brackets
Brackets 
   [ ]
|  [ ListExpressionallowIn ]
|  [ NamedArgumentList ]
Arguments 
   ParenExpressions
|  ( NamedArgumentList )
ParenExpressions 
   ( )
|  ParenListExpression
NamedArgumentList 
   LiteralField
|  ListExpressionallowIn , LiteralField
|  NamedArgumentList , LiteralField

Unary Operators

UnaryExpression 
   PostfixExpression
|  delete PostfixExpression
|  void UnaryExpression
|  typeof UnaryExpression
|  ++ PostfixExpressionOrSuper
|  -- PostfixExpressionOrSuper
|  + UnaryExpressionOrSuper
|  - UnaryExpressionOrSuper
|  ~ UnaryExpressionOrSuper
|  ! UnaryExpression
UnaryExpressionOrSuper 
   UnaryExpression
|  SuperExpression

Multiplicative Operators

MultiplicativeExpression 
   UnaryExpression
|  MultiplicativeExpressionOrSuper * UnaryExpressionOrSuper
|  MultiplicativeExpressionOrSuper / UnaryExpressionOrSuper
|  MultiplicativeExpressionOrSuper % UnaryExpressionOrSuper
MultiplicativeExpressionOrSuper 
   MultiplicativeExpression
|  SuperExpression

Additive Operators

AdditiveExpression 
   MultiplicativeExpression
|  AdditiveExpressionOrSuper + MultiplicativeExpressionOrSuper
|  AdditiveExpressionOrSuper - MultiplicativeExpressionOrSuper
AdditiveExpressionOrSuper 
   AdditiveExpression
|  SuperExpression

Bitwise Shift Operators

ShiftExpression 
   AdditiveExpression
|  ShiftExpressionOrSuper << AdditiveExpressionOrSuper
|  ShiftExpressionOrSuper >> AdditiveExpressionOrSuper
|  ShiftExpressionOrSuper >>> AdditiveExpressionOrSuper
ShiftExpressionOrSuper 
   ShiftExpression
|  SuperExpression

Relational Operators

RelationalExpressionallowIn 
   ShiftExpression
|  RelationalExpressionOrSuperallowIn < ShiftExpressionOrSuper
|  RelationalExpressionOrSuperallowIn > ShiftExpressionOrSuper
|  RelationalExpressionOrSuperallowIn <= ShiftExpressionOrSuper
|  RelationalExpressionOrSuperallowIn >= ShiftExpressionOrSuper
|  RelationalExpressionallowIn is ShiftExpression
|  RelationalExpressionallowIn as ShiftExpression
|  RelationalExpressionallowIn in ShiftExpressionOrSuper
|  RelationalExpressionallowIn instanceof ShiftExpression
RelationalExpressionnoIn 
   ShiftExpression
|  RelationalExpressionOrSupernoIn < ShiftExpressionOrSuper
|  RelationalExpressionOrSupernoIn > ShiftExpressionOrSuper
|  RelationalExpressionOrSupernoIn <= ShiftExpressionOrSuper
|  RelationalExpressionOrSupernoIn >= ShiftExpressionOrSuper
|  RelationalExpressionnoIn is ShiftExpression
|  RelationalExpressionnoIn as ShiftExpression
|  RelationalExpressionnoIn instanceof ShiftExpression
RelationalExpressionOrSuper 
   RelationalExpression
|  SuperExpression

Equality Operators

EqualityExpression 
   RelationalExpression
|  EqualityExpressionOrSuper == RelationalExpressionOrSuper
|  EqualityExpressionOrSuper != RelationalExpressionOrSuper
|  EqualityExpressionOrSuper === RelationalExpressionOrSuper
|  EqualityExpressionOrSuper !== RelationalExpressionOrSuper
EqualityExpressionOrSuper 
   EqualityExpression
|  SuperExpression

Binary Bitwise Operators

BitwiseAndExpression 
   EqualityExpression
|  BitwiseAndExpressionOrSuper & EqualityExpressionOrSuper
BitwiseXorExpression 
   BitwiseAndExpression
|  BitwiseXorExpressionOrSuper ^ BitwiseAndExpressionOrSuper
BitwiseOrExpression 
   BitwiseXorExpression
|  BitwiseOrExpressionOrSuper | BitwiseXorExpressionOrSuper
BitwiseAndExpressionOrSuper 
   BitwiseAndExpression
|  SuperExpression
BitwiseXorExpressionOrSuper 
   BitwiseXorExpression
|  SuperExpression
BitwiseOrExpressionOrSuper 
   BitwiseOrExpression
|  SuperExpression

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
|  PostfixExpressionOrSuper CompoundAssignment AssignmentExpression
|  PostfixExpressionOrSuper CompoundAssignment SuperExpression
|  PostfixExpression LogicalAssignment AssignmentExpression
CompoundAssignment 
   *=
|  /=
|  %=
|  +=
|  -=
|  <<=
|  >>=
|  >>>=
|  &=
|  ^=
|  |=
LogicalAssignment 
   &&=
|  ^^=
|  ||=

Comma Expressions

ListExpression 
   AssignmentExpression
|  ListExpression , AssignmentExpression
OptionalExpression 
   ListExpressionallowIn
|  «empty»

Type Expressions

TypeExpression  NonAssignmentExpression

Statements

  {abbrevnoShortIffull}
Statement 
   ExpressionStatement Semicolon
|  SuperStatement Semicolon
|  Block
|  LabeledStatement
|  IfStatement
|  SwitchStatement
|  DoStatement Semicolon
|  WhileStatement
|  ForStatement
|  WithStatement
|  ContinueStatement Semicolon
|  BreakStatement Semicolon
|  ReturnStatement Semicolon
|  ThrowStatement Semicolon
|  TryStatement
Substatement 
   EmptyStatement
|  Statement
|  SimpleVariableDefinition Semicolon
|  Attributes [no line break] { Substatements }
Substatements 
   «empty»
|  SubstatementsPrefix Substatementabbrev
SubstatementsPrefix 
   «empty»
|  SubstatementsPrefix Substatementfull
Semicolonabbrev 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonnoShortIf 
   ;
|  VirtualSemicolon
|  «empty»
Semicolonfull 
   ;
|  VirtualSemicolon

Empty Statement

EmptyStatement  ;

Expression Statement

ExpressionStatement  [lookahead{function{}] ListExpressionallowIn

Super Statement

SuperStatement  super Arguments

Block Statement

Block  { Directives }

Labeled Statements

LabeledStatement  Identifier : Substatement

If Statement

IfStatementabbrev 
   if ParenListExpression Substatementabbrev
|  if ParenListExpression SubstatementnoShortIf else Substatementabbrev
IfStatementfull 
   if ParenListExpression Substatementfull
|  if ParenListExpression SubstatementnoShortIf else Substatementfull
IfStatementnoShortIf  if ParenListExpression SubstatementnoShortIf else SubstatementnoShortIf

Switch Statement

SwitchStatement  switch ParenListExpression { CaseStatements }
CaseStatements 
   «empty»
|  CaseLabel
|  CaseLabel CaseStatementsPrefix CaseStatementabbrev
CaseStatementsPrefix 
   «empty»
|  CaseStatementsPrefix CaseStatementfull
CaseStatement 
   Substatement
|  CaseLabel
CaseLabel 
   case ListExpressionallowIn :
|  default :

Do-While Statement

DoStatement  do Substatementabbrev while ParenListExpression

While Statement

WhileStatement  while ParenListExpression Substatement

For Statements

ForStatement 
   for ( ForInitialiser ; OptionalExpression ; OptionalExpression ) Substatement
|  for ( ForInBinding in ListExpressionallowIn ) Substatement
ForInitialiser 
   «empty»
|  ListExpressionnoIn
|  VariableDefinitionKind VariableBindingListnoIn
|  Attributes [no line break] VariableDefinitionKind VariableBindingListnoIn
ForInBinding 
   PostfixExpression
|  VariableDefinitionKind VariableBindingnoIn
|  Attributes [no line break] VariableDefinitionKind VariableBindingnoIn

With Statement

WithStatement  with ParenListExpression Substatement

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] ListExpressionallowIn

Throw Statement

ThrowStatement  throw [no line break] ListExpressionallowIn

Try Statement

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

Directives

Directive 
   EmptyStatement
|  Statement
|  AnnotatableDirective
|  Attributes [no line break] AnnotatableDirective
|  Attributes [no line break] { Directives }
|  PackageDefinition
|  IncludeDirective Semicolon
|  Pragma Semicolon
AnnotatableDirective 
   ExportDefinition Semicolon
|  VariableDefinition Semicolon
|  FunctionDefinition
|  ClassDefinition
|  NamespaceDefinition Semicolon
|  InterfaceDefinition
|  ImportDirective Semicolon
|  UseDirective Semicolon
Directives 
   «empty»
|  DirectivesPrefix Directiveabbrev
DirectivesPrefix 
   «empty»
|  DirectivesPrefix Directivefull

Attributes

Attributes 
   Attribute
|  AttributeCombination
AttributeCombination  Attribute [no line break] Attributes
Attribute 
   AttributeExpression
|  true
|  false
|  public
|  NonexpressionAttribute
NonexpressionAttribute 
   abstract
|  final
|  private
|  static

Use Directive

UseDirective  use namespace ParenListExpression

Import Directive

ImportDirective 
   import ImportBinding IncludesExcludes
|  import ImportBinding , namespace ParenListExpression IncludesExcludes
ImportBinding 
   ImportSource
|  Identifier = ImportSource
ImportSource 
   String
|  PackageName
IncludesExcludes 
   «empty»
|  , exclude ( NamePatterns )
|  , include ( NamePatterns )
NamePatterns 
   «empty»
|  NamePatternList
NamePatternList 
   QualifiedIdentifier
|  NamePatternList , QualifiedIdentifier

Include Directive

IncludeDirective  include [no line break] String

Pragma

Pragma  use PragmaItems
PragmaItems 
   PragmaItem
|  PragmaItems , PragmaItem
PragmaItem 
   PragmaExpr
|  PragmaExpr ?
PragmaExpr 
   Identifier
|  Identifier ( PragmaArgument )
PragmaArgument 
   true
|  false
|  Number
|  - Number
|  String

Definitions

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  TypedIdentifier VariableInitialisation
VariableInitialisation 
   «empty»
|  = VariableInitialiser
VariableInitialiser 
   AssignmentExpression
|  NonexpressionAttribute
|  AttributeCombination
TypedIdentifier 
   Identifier
|  Identifier : TypeExpression

Simple Variable Definition

A SimpleVariableDefinition represents the subset of VariableDefinition expansions that may be used when the variable definition is used as a Substatement instead of a Directive in non-strict mode. In strict mode variable definitions may not be used as substatements.

SimpleVariableDefinition  var UntypedVariableBindingList
UntypedVariableBindingList 
   UntypedVariableBinding
|  UntypedVariableBindingList , UntypedVariableBinding
UntypedVariableBinding  Identifier VariableInitialisationallowIn

Function Definition

FunctionDefinition 
   FunctionDeclaration Block
|  FunctionDeclaration Semicolon
FunctionDeclaration  function FunctionName FunctionSignature
FunctionName 
   Identifier
|  get [no line break] Identifier
|  set [no line break] Identifier
|  String
FunctionSignature  ParameterSignature ResultSignature
ParameterSignature  ( Parameters )
Parameters 
   «empty»
|  AllParameters
AllParameters 
   Parameter
|  Parameter , AllParameters
|  OptionalParameters
OptionalParameters 
   OptionalParameter
|  OptionalParameter , OptionalParameters
|  RestAndNamedParameters
RestAndNamedParameters 
   NamedParameters
|  RestParameter
|  RestParameter , NamedParameters
|  NamedRestParameter
NamedParameters 
   NamedParameter
|  NamedParameter , NamedParameters
Parameter 
   TypedIdentifierallowIn
|  const TypedIdentifierallowIn
OptionalParameter  Parameter = AssignmentExpressionallowIn
TypedInitialiser  TypedIdentifierallowIn = AssignmentExpressionallowIn
NamedParameter 
   named TypedInitialiser
|  const named TypedInitialiser
|  named const TypedInitialiser
RestParameter 
   ...
|  ... Parameter
NamedRestParameter 
   ... named Identifier
|  ... const named Identifier
|  ... named const Identifier
ResultSignature 
   «empty»
|  : TypeExpressionallowIn

Class Definition

ClassDefinition  class Identifier Inheritance Block
Inheritance 
   «empty»
|  extends TypeExpressionallowIn
|  implements TypeExpressionList
|  extends TypeExpressionallowIn implements TypeExpressionList

Interface Definition

InterfaceDefinition 
   interface Identifier ExtendsList Block
|  interface Identifier Semicolon
ExtendsList 
   «empty»
|  extends TypeExpressionList
TypeExpressionList 
   TypeExpressionallowIn

Namespace Definition

NamespaceDefinition  namespace Identifier

Package Definition

PackageDefinition 
   package Block
|  package PackageName Block
PackageName 
   Identifier
|  PackageName . Identifier

Programs

Program  Directives

Waldemar Horwat
Last modified Tuesday, February 26, 2002
previousupnext