0% found this document useful (0 votes)
3 views12 pages

Java Programming Language Grammar

This chapter provides a comprehensive grammar for the Java programming language, suitable for parser implementation. It outlines various components such as identifiers, literals, expressions, and statements using BNF-style conventions. The document details the structure and syntax rules necessary for understanding and parsing Java code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

Java Programming Language Grammar

This chapter provides a comprehensive grammar for the Java programming language, suitable for parser implementation. It outlines various components such as identifiers, literals, expressions, and statements using BNF-style conventions. The document details the structure and syntax rules necessary for understanding and parsing Java code.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

syntax.

fm Page 559 Friday, August 13, 2004 5:39 PM

C H A P T E R 18
Syntax

FT
Is there grammar in a title. There is grammar in a title. Thank you.
—Gertrude Stein, Arthur a Grammar, in How to Write (1931)

THIS chapter presents a grammar for the Java programming language.


The grammar presented piecemeal in the preceding chapters is much better
for exposition, but it is not ideally suited as a basis for a parser. The grammar pre-
sented in this chapter is the basis for the reference implementation.
The grammar below uses the following BNF-style conventions:
RA
• [x] denotes zero or one occurrences of x.
• {x} denotes zero or more occurrences of x.
x | y means one of either x or y.

18.1 The Grammar of the Java Programming Language

Identifier:
IDENTIFIER
QualifiedIdentifier:
D

Identifier { . Identifier }
Literal:
IntegerLiteral
FloatingPointLiteral
CharacterLiteral
StringLiteral
BooleanLiteral
NullLiteral
Expression:
Expression1 [AssignmentOperator Expression1]]
559
[Link] Page 560 Friday, August 13, 2004 5:39 PM

18.1 The Grammar of the Java Programming Language SYNTAX

AssignmentOperator:
=
+=
-=
*=
/=
&=
|=
^=
%=

FT
<<=
>>=
>>>=
Type:
Identifier [TypeArguments]{ . Identifier [TypeArguments]}
BracketsOpt
BasicType
TypeArguments:
RA
< TypeArgument {, TypeArgument} >

TypeArgument:
Type
? [( extends | super ) Type]

RawType:
Identifier { . Identifier } BracketsOpt
StatementExpression:
Expression
ConstantExpression:
Expression
D

Expression1:
Expression2 [Expression1Rest]
Expression1Rest:
[ ? Expression : Expression1]
Expression2 :
Expression3 [Expression2Rest]

560
[Link] Page 561 Friday, August 13, 2004 5:39 PM

SYNTAX The Grammar of the Java Programming Language 18.1

Expression2Rest:
{Infixop Expression3}
Expression3 instanceof Type
Infixop:
||
&&
|
^
&

FT
==
!=
<
>
<=
>=
<<
>>
>>>
+
RA
-
*
/
%

Expression3:
PrefixOp Expression3
( Expr | Type ) Expression3
Primary {Selector} {PostfixOp}
Primary:
( Expression )
D

NonWildcardTypeArguments (ExplicitGenericInvocationSuffix | this


Arguments)
this [Arguments]
super SuperSuffix
Literal
new Creator
Identifier { . Identifier }[ IdentifierSuffix]
BasicType BracketsOpt .class
[Link]

561
[Link] Page 562 Friday, August 13, 2004 5:39 PM

18.1 The Grammar of the Java Programming Language SYNTAX

IdentifierSuffix:
[ ( ] BracketsOpt . class | Expression ])
Arguments
. ( class | ExplicitGenericInvocation | this | super Arguments | new
[NonWildcardTypeArguments] InnerCreator )
ExplicitGenericInvocation:
NonWildcardTypeArguments ExplicitGenericInvocationSuffix
NonWildcardTypeArguments:
< TypeList >

FT
ExplicitGenericInvocationSuffix:
super SuperSuffix
Identifier Arguments

PrefixOp:
++
--
RA
!
~
+
-

PostfixOp:
++
--

Selector: Selector:
. Identifier [Arguments]
. ExplicitGenericInvocation
. this
D

. super SuperSuffix
. new [NonWildcardTypeArguments] InnerCreator
[ Expression ]

SuperSuffix:
Arguments
. Identifier [Arguments]

562
[Link] Page 563 Friday, August 13, 2004 5:39 PM

SYNTAX The Grammar of the Java Programming Language 18.1

BasicType:
byte
short
char
int
long
float
double
boolean

FT
ArgumentsOpt:
[ Arguments ]
Arguments:
( [Expression { , Expression }] )

BracketsOpt:
{[]}
Creator:
[NonWildcardTypeArguments] CreatedName ( ArrayCreatorRest |
RA
ClassCreatorRest )
CreatedName:
Identifier [NonWildcardTypeArguments] {. Identifier
[NonWildcardTypeArguments]}
InnerCreator:
Identifier ClassCreatorRest
ArrayCreatorRest:
[ ( ] BracketsOpt ArrayInitializer | Expression ] {[ Expression ]}
BracketsOpt )
ClassCreatorRest:
D

Arguments [ClassBody]
ArrayInitializer:
{ [VariableInitializer {, VariableInitializer} [,]] }

VariableInitializer:
ArrayInitializer
Expression
ParExpression:
( Expression )

563
[Link] Page 564 Friday, August 13, 2004 5:39 PM

18.1 The Grammar of the Java Programming Language SYNTAX

Block:
{ BlockStatements }

BlockStatements:
{ BlockStatement }
BlockStatement :
LocalVariableDeclarationStatement
ClassOrInterfaceDeclaration
[Identifier :] Statement
LocalVariableDeclarationStatement:

FT
[final] Type VariableDeclarators ;
Statement:
Block
assert Expression [ : Expression] ;
if ParExpression Statement [else Statement]
for ( ForControl ) Statement
while ParExpression Statement
do Statement while ParExpression ;
RA
try Block ( Catches | [Catches] finally Block )
switch ParExpression { SwitchBlockStatementGroups }
synchronized ParExpression Block
return [Expression] ;
throw Expression ;
break [Identifier]
continue [Identifier]
;
ExpressionStatement
Identifier : Statement
Catches:
D

CatchClause {CatchClause}
CatchClause:
catch ( FormalParameter ) Block

SwitchBlockStatementGroups:
{ SwitchBlockStatementGroup }
SwitchBlockStatementGroup:
SwitchLabel BlockStatements

564
[Link] Page 565 Friday, August 13, 2004 5:39 PM

SYNTAX The Grammar of the Java Programming Language 18.1

SwitchLabel:
case ConstantExpression :
default :

MoreStatementExpressions:
{ , StatementExpression }
ForControl:
; [Expression] ; ForUpdateOpt
StatementExpression MoreStatementExpressions; [Expression] ;
ForUpdateOpt

FT
[final] [Annotations] Type Identifier ForControlRest
ForControlRest:
VariableDeclaratorsRest; [Expression] ; ForUpdateOpt
: Expression

ForUpdate:
StatementExpression MoreStatementExpressions
ModifiersOpt:
{ Modifier }
RA
Modifier:
Annotation
public
protected
private
static
abstract
final
native
synchronized
D

transient
volatile
strictfp

VariableDeclarators:
VariableDeclarator { , VariableDeclarator }
VariableDeclaratorsRest:
VariableDeclaratorRest { , VariableDeclarator }
ConstantDeclaratorsRest:
ConstantDeclaratorRest { , ConstantDeclarator }

565
[Link] Page 566 Friday, August 13, 2004 5:39 PM

18.1 The Grammar of the Java Programming Language SYNTAX

VariableDeclarator:
Identifier VariableDeclaratorRest
ConstantDeclarator:
Identifier ConstantDeclaratorRest
VariableDeclaratorRest:
BracketsOpt [ = VariableInitializer]
ConstantDeclaratorRest:
BracketsOpt = VariableInitializer

FT
VariableDeclaratorId:
Identifier BracketsOpt
CompilationUnit:
[Annotationsopt package QualifiedIdentifier ; ] {ImportDeclaration}
{TypeDeclaration}
ImportDeclaration:
import [ static] Identifier { . Identifier } [ . * ];

TypeDeclaration:
RA
ClassOrInterfaceDeclaration
;

ClassOrInterfaceDeclaration:
ModifiersOpt (ClassDeclaration | InterfaceDeclaration)
ClassDeclaration:
NormalClassDeclaration
EnumDeclaration

NormalClassDeclaration:
D

class Identifier TypeParametersopt [extends Type] [implements


TypeList] ClassBody
TypeParameters:
< TypeParameter {, TypeParameter} >

TypeParameter:
Identifier [extends Bound]
Bound:
Type {& Type}

566
[Link] Page 567 Friday, August 13, 2004 5:39 PM

SYNTAX The Grammar of the Java Programming Language 18.1

EnumDeclaration:
ClassModifiersopt enum Identifier [implements TypeList] EnumBody
EnumBody:
{ EnumConstantsopt ,opt EnumBodyDeclarationsopt }
EnumConstants:
EnumConstant
EnumConstants , EnumConstant
EnumConstant:
Annotations Identifier Argumentsopt ClassBodyopt

FT
Arguments:
( ArgumentListopt )
EnumBodyDeclarations:
; ClassBodyDeclarationsopt
InterfaceDeclaration:
NormalInterfaceDeclaration
AnnotationTypeDeclaration
RA
NormalInterfaceDeclaration:
interface Identifier TypeParametersopt [extends TypeList]
InterfaceBody
TypeList:
Type { , Type}
AnnotationTypeDeclaration:
InterfaceModifiersopt @ interface Identifier AnnotationTypeBody
AnnotationTypeBody:
{ AnnotationTypeElementDeclarationsopt }
D

AnnotationTypeElementDeclarations:
AnnotationTypeElementDeclaration
AnnotationTypeElementDeclarations AnnotationTypeElementDeclaration

567
[Link] Page 568 Friday, August 13, 2004 5:39 PM

18.1 The Grammar of the Java Programming Language SYNTAX

AnnotationTypeElementDeclaration:
AbstractMethodModifiersopt Type Identifier ( ) DefaultValueopt ;
ConstantDeclaration
ClassDeclaration
InterfaceDeclaration
EnumDeclaration
AnnotationTypeDeclaration
;
DefaultValue:

FT
default ElementValue

ClassBody:
{ {ClassBodyDeclaration} }

InterfaceBody:
{ {InterfaceBodyDeclaration} }

ClassBodyDeclaration:
;
[static] Block
RA
ModifiersOpt MemberDecl
MemberDecl:
GenericMethodOrConstructorDecl
MethodOrFieldDecl
void Identifier MethodDeclaratorRest
Identifier ConstructorDeclaratorRest
ClassOrInterfaceDeclaration
GenericMethodOrConstructorDecl:
TypeParameters GenericMethodOrConstructorRest
GenericMethodOrConstructorRest:
D

Type Identifier MethodDeclaratorRest


Identifier ConstructorDeclaratorRest
MethodOrFieldDecl:
Type Identifier MethodOrFieldRest
MethodOrFieldRest:
VariableDeclaratorRest
MethodDeclaratorRest

568
[Link] Page 569 Friday, August 13, 2004 5:39 PM

SYNTAX The Grammar of the Java Programming Language 18.1

InterfaceBodyDeclaration:
;
ModifiersOpt InterfaceMemberDecl
InterfaceMemberDecl:
InterfaceMethodOrFieldDecl
InterfaceGenericMethodDecl
void Identifier VoidInterfaceMethodDeclaratorRest
ClassOrInterfaceDeclaration
InterfaceMethodOrFieldDecl:

FT
Type Identifier InterfaceMethodOrFieldRest
InterfaceMethodOrFieldRest:
ConstantDeclaratorsRest ;
InterfaceMethodDeclaratorRest
MethodDeclaratorRest:
FormalParameters BracketsOpt [throws QualifiedIdentifierList] (
MethodBody | ; )
VoidMethodDeclaratorRest:
RA
FormalParameters [throws QualifiedIdentifierList] ( MethodBody | ; )
InterfaceMethodDeclaratorRest:
FormalParameters BracketsOpt [throws QualifiedIdentifierList] ;
InterfaceGenericMethodDecl:
TypeParameters Type Identifier InterfaceMethodDeclaratorRest

VoidInterfaceMethodDeclaratorRest:
FormalParameters [throws QualifiedIdentifierList] ;
ConstructorDeclaratorRest:
D

FormalParameters [throws QualifiedIdentifierList] MethodBody


QualifiedIdentifierList:
QualifiedIdentifier { , QualifiedIdentifier}
FormalParameters:
( [FormalParameterDecls] )

FormalParameterDecls:
[final] [Annotations] Type FormalParameterDeclsRest]

569
[Link] Page 570 Friday, August 13, 2004 5:39 PM

18.1 The Grammar of the Java Programming Language SYNTAX

FormalParameterDeclsRest:
VariableDeclaratorId [ , FormalParameterDecls]
... VariableDeclaratorId

MethodBody:
Block

FT
RA
D

570

You might also like