Browsable Java (J2SE 1.2 “Playground”) Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: java/java-2/jls/read/extracted
Source used for this grammar: James Gosling, Bill Joy, Guy Steele, Gilad Bracha, Java Language Specification, 2nd edition, 1998 [HTML] [PDF]

Summary

Syntax

ClassBodyDeclarations ::=
	ClassBodyDeclaration
ClassBodyDeclarations ::=
	ClassBodyDeclarations ClassBodyDeclaration
PostDecrementExpression ::=
	PostfixExpression "--"
ExpressionStatement ::=
	StatementExpression ";"
PrimitiveType ::=
	NumericType
PrimitiveType ::=
	"boolean"
InterfaceTypeList ::=
	InterfaceType
InterfaceTypeList ::=
	InterfaceTypeList "," InterfaceType
InterfaceDeclaration ::=
	InterfaceModifiers? "interface" Identifier ExtendsInterfaces? InterfaceBody
Statement ::=
	StatementWithoutTrailingSubstatement
Statement ::=
	LabeledStatement
Statement ::=
	IfThenStatement
Statement ::=
	IfThenElseStatement
Statement ::=
	WhileStatement
Statement ::=
	ForStatement
AndExpression ::=
	EqualityExpression
AndExpression ::=
	AndExpression "&" EqualityExpression
ConstructorBody ::=
	(ExplicitConstructorInvocation? BlockStatements?)*
ArgumentList ::=
	Expression
ArgumentList ::=
	ArgumentList "," Expression
MethodName ::=
	Identifier
MethodName ::=
	AmbiguousName "." Identifier
ClassMemberDeclaration ::=
	FieldDeclaration
ClassMemberDeclaration ::=
	MethodDeclaration
ClassMemberDeclaration ::=
	ClassDeclaration
ClassMemberDeclaration ::=
	InterfaceDeclaration
ClassMemberDeclaration ::=
	";"
Assignment ::=
	LeftHandSide AssignmentOperator AssignmentExpression
Interfaces ::=
	"implements" InterfaceTypeList
CatchClause ::=
	"catch" "(" FormalParameter ")" Block
AbstractMethodModifiers ::=
	AbstractMethodModifier
AbstractMethodModifiers ::=
	AbstractMethodModifiers AbstractMethodModifier
PostIncrementExpression ::=
	PostfixExpression "++"
TypeName ::=
	Identifier
TypeName ::=
	PackageOrTypeName "." Identifier
InterfaceType ::=
	TypeName
BlockStatement ::=
	LocalVariableDeclarationStatement
BlockStatement ::=
	ClassDeclaration
BlockStatement ::=
	Statement
IfThenStatement ::=
	"if" "(" Expression ")" Statement
SwitchBlockStatementGroups ::=
	SwitchBlockStatementGroup
SwitchBlockStatementGroups ::=
	SwitchBlockStatementGroups SwitchBlockStatementGroup
ClassModifier ::=
	"public"
ClassModifier ::=
	"protected"
ClassModifier ::=
	"private"
ClassModifier ::=
	"abstract"
ClassModifier ::=
	"static"
ClassModifier ::=
	"final"
ClassModifier ::=
	"strictfp"
SingleTypeImportDeclaration ::=
	"import" TypeName ";"
ConstantDeclaration ::=
	ConstantModifiers? Type VariableDeclarators
PreIncrementExpression ::=
	"++" UnaryExpression
ExplicitConstructorInvocation ::=
	"this" "(" ArgumentList? ")" ";"
ExplicitConstructorInvocation ::=
	"super" "(" ArgumentList? ")" ";"
ExplicitConstructorInvocation ::=
	Primary "." "super" "(" ArgumentList? ")" ";"
UnaryExpression ::=
	PreIncrementExpression
UnaryExpression ::=
	PreDecrementExpression
UnaryExpression ::=
	"+" UnaryExpression
UnaryExpression ::=
	"-" UnaryExpression
UnaryExpression ::=
	UnaryExpressionNotPlusMinus
VariableInitializers ::=
	VariableInitializer
VariableInitializers ::=
	VariableInitializers "," VariableInitializer
DimExpr ::=
	"[" Expression "]"
Throws ::=
	"throws" ClassTypeList
BlockStatements ::=
	BlockStatement
BlockStatements ::=
	BlockStatements BlockStatement
InterfaceModifiers ::=
	InterfaceModifier
InterfaceModifiers ::=
	InterfaceModifiers InterfaceModifier
ClassTypeList ::=
	ClassType
ClassTypeList ::=
	ClassTypeList "," ClassType
PrimaryNoNewArray ::=
	Literal
PrimaryNoNewArray ::=
	Type "." "class"
PrimaryNoNewArray ::=
	"void" "." "class"
PrimaryNoNewArray ::=
	"this"
PrimaryNoNewArray ::=
	"ClassName" "." "this"
PrimaryNoNewArray ::=
	"(" Expression ")"
PrimaryNoNewArray ::=
	ClassInstanceCreationExpression
PrimaryNoNewArray ::=
	FieldAccess
PrimaryNoNewArray ::=
	MethodInvocation
PrimaryNoNewArray ::=
	ArrayAccess
AssignmentOperator ::=
	"="
AssignmentOperator ::=
	"*="
AssignmentOperator ::=
	"/="
AssignmentOperator ::=
	"%="
AssignmentOperator ::=
	"+="
AssignmentOperator ::=
	"-="
AssignmentOperator ::=
	"<<="
AssignmentOperator ::=
	">>="
AssignmentOperator ::=
	">>>="
AssignmentOperator ::=
	"&="
AssignmentOperator ::=
	"^="
AssignmentOperator ::=
	"|="
Literal ::=
	IntegerLiteral
Literal ::=
	FloatingPointLiteral
Literal ::=
	BooleanLiteral
Literal ::=
	CharacterLiteral
Literal ::=
	StringLiteral
Literal ::=
	NullLiteral
FieldDeclaration ::=
	FieldModifiers? Type VariableDeclarators ";"
ReturnStatement ::=
	"return" Expression? ";"
VariableDeclarator ::=
	VariableDeclaratorId
VariableDeclarator ::=
	VariableDeclaratorId "=" VariableInitializer
PostfixExpression ::=
	Primary
PostfixExpression ::=
	ExpressionName
PostfixExpression ::=
	PostIncrementExpression
PostfixExpression ::=
	PostDecrementExpression
SwitchLabel ::=
	"case" ConstantExpression ":"
SwitchLabel ::=
	"default" ":"
ConstantModifier ::=
	"public"
ConstantModifier ::=
	"static"
ConstantModifier ::=
	"final"
FormalParameter ::=
	"final"? Type VariableDeclaratorId
PackageName ::=
	Identifier
PackageName ::=
	PackageName "." Identifier
ConditionalAndExpression ::=
	InclusiveOrExpression
ConditionalAndExpression ::=
	ConditionalAndExpression "&&" InclusiveOrExpression
MethodModifiers ::=
	MethodModifier
MethodModifiers ::=
	MethodModifiers MethodModifier
Type ::=
	PrimitiveType
Type ::=
	ReferenceType
EqualityExpression ::=
	RelationalExpression
EqualityExpression ::=
	EqualityExpression "==" RelationalExpression
EqualityExpression ::=
	EqualityExpression "!=" RelationalExpression
InterfaceMemberDeclaration ::=
	ConstantDeclaration
InterfaceMemberDeclaration ::=
	AbstractMethodDeclaration
InterfaceMemberDeclaration ::=
	ClassDeclaration
InterfaceMemberDeclaration ::=
	InterfaceDeclaration
InterfaceMemberDeclaration ::=
	";"
MultiplicativeExpression ::=
	UnaryExpression
MultiplicativeExpression ::=
	MultiplicativeExpression "*" UnaryExpression
MultiplicativeExpression ::=
	MultiplicativeExpression "/" UnaryExpression
MultiplicativeExpression ::=
	MultiplicativeExpression "%" UnaryExpression
BreakStatement ::=
	"break" Identifier? ";"
ConstructorModifier ::=
	"public"
ConstructorModifier ::=
	"protected"
ConstructorModifier ::=
	"private"
SynchronizedStatement ::=
	"synchronized" "(" Expression ")" Block
ClassInstanceCreationExpression ::=
	"new" ClassOrInterfaceType "(" ArgumentList? ")" ClassBody?
ClassInstanceCreationExpression ::=
	Primary "." "new" Identifier "(" ArgumentList? ")" ClassBody?
Super ::=
	"extends" ClassType
ArrayCreationExpression ::=
	"new" PrimitiveType DimExprs Dims?
ArrayCreationExpression ::=
	"new" TypeName DimExprs Dims?
ArrayCreationExpression ::=
	"new" PrimitiveType Dims ArrayInitializer
ArrayCreationExpression ::=
	"new" TypeName Dims ArrayInitializer
ArrayInitializer ::=
	"{" VariableInitializers? ","? "}"
InterfaceModifier ::=
	"public"
InterfaceModifier ::=
	"protected"
InterfaceModifier ::=
	"private"
InterfaceModifier ::=
	"abstract"
InterfaceModifier ::=
	"static"
InterfaceModifier ::=
	"strictfp"
UnaryExpressionNotPlusMinus ::=
	PostfixExpression
UnaryExpressionNotPlusMinus ::=
	"~" UnaryExpression
UnaryExpressionNotPlusMinus ::=
	"!" UnaryExpression
UnaryExpressionNotPlusMinus ::=
	CastExpression
SwitchBlock ::=
	(SwitchBlockStatementGroups? SwitchLabels?)*
EmptyStatement ::=
	";"
StatementNoShortIf ::=
	StatementWithoutTrailingSubstatement
StatementNoShortIf ::=
	LabeledStatementNoShortIf
StatementNoShortIf ::=
	IfThenElseStatementNoShortIf
StatementNoShortIf ::=
	WhileStatementNoShortIf
StatementNoShortIf ::=
	ForStatementNoShortIf
MethodInvocation ::=
	MethodName "(" ArgumentList? ")"
MethodInvocation ::=
	Primary "." Identifier "(" ArgumentList? ")"
MethodInvocation ::=
	"super" "." Identifier "(" ArgumentList? ")"
MethodInvocation ::=
	ClassName "." "super" "." Identifier "(" ArgumentList? ")"
AbstractMethodDeclaration ::=
	AbstractMethodModifiers? ResultType MethodDeclarator Throws? ";"
IfThenElseStatement ::=
	"if" "(" Expression ")" StatementNoShortIf "else" Statement
ArrayAccess ::=
	ExpressionName "[" Expression "]"
ArrayAccess ::=
	PrimaryNoNewArray "[" Expression "]"
Block ::=
	BlockStatements?*
ForUpdate ::=
	StatementExpressionList
ForInit ::=
	StatementExpressionList
ForInit ::=
	LocalVariableDeclaration
VariableDeclarators ::=
	VariableDeclarator
VariableDeclarators ::=
	VariableDeclarators "," VariableDeclarator
ThrowStatement ::=
	"throw" Expression ";"
ExclusiveOrExpression ::=
	AndExpression
ExclusiveOrExpression ::=
	ExclusiveOrExpression "^" AndExpression
Primary ::=
	PrimaryNoNewArray
Primary ::=
	ArrayCreationExpression
PackageDeclaration ::=
	"package" PackageName ";"
RelationalExpression ::=
	ShiftExpression
RelationalExpression ::=
	RelationalExpression "<" ShiftExpression
RelationalExpression ::=
	RelationalExpression ">" ShiftExpression
RelationalExpression ::=
	RelationalExpression "<=" ShiftExpression
RelationalExpression ::=
	RelationalExpression ">=" ShiftExpression
RelationalExpression ::=
	RelationalExpression "instanceof" ReferenceType
WhileStatementNoShortIf ::=
	"while" "(" Expression ")" StatementNoShortIf
ExtendsInterfaces ::=
	"extends" InterfaceType
ExtendsInterfaces ::=
	ExtendsInterfaces "," InterfaceType
AbstractMethodModifier ::=
	"public"
AbstractMethodModifier ::=
	"abstract"
ConstantExpression ::=
	Expression
VariableInitializer ::=
	Expression
VariableInitializer ::=
	ArrayInitializer
StaticInitializer ::=
	"static" Block
Finally ::=
	"finally" Block
ConstructorDeclaration ::=
	ConstructorModifiers? ConstructorDeclarator Throws? ConstructorBody
TypeImportOnDemandDeclaration ::=
	"import" PackageOrTypeName "." "*" ";"
PreDecrementExpression ::=
	"--" UnaryExpression
InterfaceMemberDeclarations ::=
	InterfaceMemberDeclaration
InterfaceMemberDeclarations ::=
	InterfaceMemberDeclarations InterfaceMemberDeclaration
ForStatement ::=
	"for" "(" ForInit? ";" Expression? ";" ForUpdate? ")" Statement
PackageOrTypeName ::=
	Identifier
PackageOrTypeName ::=
	PackageOrTypeName "." Identifier
AmbiguousName ::=
	Identifier
AmbiguousName ::=
	AmbiguousName "." Identifier
ContinueStatement ::=
	"continue" Identifier? ";"
ImportDeclaration ::=
	SingleTypeImportDeclaration
ImportDeclaration ::=
	TypeImportOnDemandDeclaration
CompilationUnit ::=
	PackageDeclaration? ImportDeclarations? TypeDeclarations?
SwitchStatement ::=
	"switch" "(" Expression ")" SwitchBlock
Dims ::=
	"[" "]"
Dims ::=
	Dims "[" "]"
ClassBody ::=
	ClassBodyDeclarations?*
ConditionalOrExpression ::=
	ConditionalAndExpression
ConditionalOrExpression ::=
	ConditionalOrExpression "||" ConditionalAndExpression
ClassBodyDeclaration ::=
	ClassMemberDeclaration
ClassBodyDeclaration ::=
	InstanceInitializer
ClassBodyDeclaration ::=
	StaticInitializer
ClassBodyDeclaration ::=
	ConstructorDeclaration
SwitchBlockStatementGroup ::=
	SwitchLabels BlockStatements
MethodDeclaration ::=
	MethodHeader MethodBody
MethodModifier ::=
	"public"
MethodModifier ::=
	"protected"
MethodModifier ::=
	"private"
MethodModifier ::=
	"abstract"
MethodModifier ::=
	"static"
MethodModifier ::=
	"final"
MethodModifier ::=
	"synchronized"
MethodModifier ::=
	"native"
MethodModifier ::=
	"strictfp"
DoStatement ::=
	"do" Statement "while" "(" Expression ")" ";"
ConditionalExpression ::=
	ConditionalOrExpression
ConditionalExpression ::=
	ConditionalOrExpression "?" Expression ":" ConditionalExpression
ClassOrInterfaceType ::=
	ClassType
ClassOrInterfaceType ::=
	InterfaceType
InstanceInitializer ::=
	Block
TryStatement ::=
	"try" Block Catches
TryStatement ::=
	"try" Block Catches? Finally
SwitchLabels ::=
	SwitchLabel
SwitchLabels ::=
	SwitchLabels SwitchLabel
FormalParameterList ::=
	FormalParameter
FormalParameterList ::=
	FormalParameterList "," FormalParameter
Expression ::=
	AssignmentExpression
StatementExpressionList ::=
	StatementExpression
StatementExpressionList ::=
	StatementExpressionList "," StatementExpression
AdditiveExpression ::=
	MultiplicativeExpression
AdditiveExpression ::=
	AdditiveExpression "+" MultiplicativeExpression
AdditiveExpression ::=
	AdditiveExpression "-" MultiplicativeExpression
FloatingPointType ::=
	"float"
FloatingPointType ::=
	"double"
LocalVariableDeclaration ::=
	"final"? Type VariableDeclarators
ConstructorDeclarator ::=
	SimpleTypeName "(" FormalParameterList? ")"
InclusiveOrExpression ::=
	ExclusiveOrExpression
InclusiveOrExpression ::=
	InclusiveOrExpression "|" ExclusiveOrExpression
ClassType ::=
	TypeName
WhileStatement ::=
	"while" "(" Expression ")" Statement
ClassDeclaration ::=
	ClassModifiers? "class" Identifier Super? Interfaces? ClassBody
AssignmentExpression ::=
	ConditionalExpression
AssignmentExpression ::=
	Assignment
CastExpression ::=
	"(" PrimitiveType ")" UnaryExpression
CastExpression ::=
	"(" ReferenceType ")" UnaryExpressionNotPlusMinus
CastExpression ::=
	"(" PrimitiveType Dims? ")" UnaryExpression
ImportDeclarations ::=
	ImportDeclaration
ImportDeclarations ::=
	ImportDeclarations ImportDeclaration
TypeDeclaration ::=
	ClassDeclaration
TypeDeclaration ::=
	InterfaceDeclaration
TypeDeclaration ::=
	";"
LabeledStatement ::=
	Identifier ":" Statement
ExpressionName ::=
	Identifier
ExpressionName ::=
	AmbiguousName "." Identifier
ArrayType ::=
	Type "[" "]"
StatementExpression ::=
	Assignment
StatementExpression ::=
	PreIncrementExpression
StatementExpression ::=
	PreDecrementExpression
StatementExpression ::=
	PostIncrementExpression
StatementExpression ::=
	PostDecrementExpression
StatementExpression ::=
	MethodInvocation
StatementExpression ::=
	ClassInstanceCreationExpression
MethodDeclarator ::=
	Identifer "(" FormalParameterList? ")"
MethodDeclarator ::=
	MethodDeclarator "[" "]"
VariableDeclaratorId ::=
	Identifier
VariableDeclaratorId ::=
	VariableDeclaratorId "[" "]"
IntegralType ::=
	"byte"
IntegralType ::=
	"short"
IntegralType ::=
	"int"
IntegralType ::=
	"long"
IntegralType ::=
	"char"
NumericType ::=
	IntegralType
NumericType ::=
	FloatingPointType
LeftHandSide ::=
	ExpressionName
LeftHandSide ::=
	FieldAccess
LeftHandSide ::=
	ArrayAccess
IfThenElseStatementNoShortIf ::=
	"if" "(" Expression ")" StatementNoShortIf "else" StatementNoShortIf
LocalVariableDeclarationStatement ::=
	LocalVariableDeclaration ";"
ShiftExpression ::=
	AdditiveExpression
ShiftExpression ::=
	ShiftExpression "<<" AdditiveExpression
ShiftExpression ::=
	ShiftExpression ">>" AdditiveExpression
ShiftExpression ::=
	ShiftExpression ">>>" AdditiveExpression
FieldModifiers ::=
	FieldModifier
FieldModifiers ::=
	FieldModifiers FieldModifier
MethodBody ::=
	Block
MethodBody ::=
	";"
Catches ::=
	CatchClause
Catches ::=
	Catches CatchClause
ResultType ::=
	Type
ResultType ::=
	"void"
ConstructorModifiers ::=
	ConstructorModifier
ConstructorModifiers ::=
	ConstructorModifiers ConstructorModifier
ReferenceType ::=
	ClassOrInterfaceType
ReferenceType ::=
	ArrayType
ClassModifiers ::=
	ClassModifier
ClassModifiers ::=
	ClassModifiers ClassModifier
ForStatementNoShortIf ::=
	"for" "(" ForInit? ";" Expression? ";" ForUpdate? ")" StatementNoShortIf
MethodHeader ::=
	MethodModifiers? ResultType MethodDeclarator Throws?
LabeledStatementNoShortIf ::=
	Identifier ":" StatementNoShortIf
InterfaceBody ::=
	InterfaceMemberDeclarations?*
ConstantModifiers ::=
	ConstantModifier
ConstantModifiers ::=
	ConstantModifiers ConstantModifer
FieldAccess ::=
	Primary "." Identifier
FieldAccess ::=
	"super" "." Identifier
FieldAccess ::=
	ClassName "." "super" "." Identifier
TypeDeclarations ::=
	TypeDeclaration
TypeDeclarations ::=
	TypeDeclarations TypeDeclaration
FieldModifier ::=
	"public"
FieldModifier ::=
	"protected"
FieldModifier ::=
	"private"
FieldModifier ::=
	"static"
FieldModifier ::=
	"final"
FieldModifier ::=
	"transient"
FieldModifier ::=
	"volatile"
StatementWithoutTrailingSubstatement ::=
	Block
StatementWithoutTrailingSubstatement ::=
	EmptyStatement
StatementWithoutTrailingSubstatement ::=
	ExpressionStatement
StatementWithoutTrailingSubstatement ::=
	SwitchStatement
StatementWithoutTrailingSubstatement ::=
	DoStatement
StatementWithoutTrailingSubstatement ::=
	BreakStatement
StatementWithoutTrailingSubstatement ::=
	ContinueStatement
StatementWithoutTrailingSubstatement ::=
	ReturnStatement
StatementWithoutTrailingSubstatement ::=
	SynchronizedStatement
StatementWithoutTrailingSubstatement ::=
	ThrowStatement
StatementWithoutTrailingSubstatement ::=
	TryStatement
DimExprs ::=
	DimExpr
DimExprs ::=
	DimExprs DimExpr

GrammarLabMaintained by Dr. Vadim Zaytsev a.k.a. @grammarware. Last updated in September 2015. []