Browsable Java (J2SE 5.0 “Tiger”) Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: java/java-5/jls/impl/extracted
Source used for this grammar: James Gosling, Bill Joy, Guy Steele, Gilad Bracha, Java Language Specification, 3rd edition, 2004, §18: Syntax, pages 585–596 [HTML] [PDF]

Summary

Syntax

NormalClassDeclaration ::=
	"class" Identifier TypeParameters? ("extends" Type)? ("implements" TypeList)? ClassBody
ExplicitGenericInvocation ::=
	NonWildcardTypeArguments ExplicitGenericInvocationSuffix
InterfaceDeclaration ::=
	NormalInterfaceDeclaration
InterfaceDeclaration ::=
	AnnotationTypeDeclaration
Statement ::=
	Block
Statement ::=
	"assert" Expression (":" Expression)? ";"
Statement ::=
	"if" ParExpression Statement ("else" Statement)?
Statement ::=
	"for" "(" ForControl ")" Statement
Statement ::=
	"while" ParExpression Statement
Statement ::=
	"do" Statement "while" ParExpression ";"
Statement ::=
	"try" Block (Catches | Catches? "finally" Block)
Statement ::=
	"switch" ParExpression "{" SwitchBlockStatementGroups "}"
Statement ::=
	"synchronized" ParExpression Block
Statement ::=
	"return" Expression? ";"
Statement ::=
	"throw" Expression ";"
Statement ::=
	"break" Identifier?
Statement ::=
	"continue" Identifier?
Statement ::=
	";"
Statement ::=
	StatementExpression ";"
Statement ::=
	Identifier ":" Statement
Annotations ::=
	Annotation Annotations?
ElementValue ::=
	ConditionalExpression
ElementValue ::=
	Annotation
ElementValue ::=
	ElementValueArrayInitializer
TypeParameter ::=
	Identifier ("extends" Bound)?
CatchClause ::=
	"catch" "(" FormalParameter ")" Block
ClassCreatorRest ::=
	Arguments ClassBody?
ClassBodyDeclaration ::=
	";"
ClassBodyDeclaration ::=
	"static"? Block
ClassBodyDeclaration ::=
	Modifier* MemberDecl
BlockStatement ::=
	LocalVariableDeclarationStatement
BlockStatement ::=
	ClassOrInterfaceDeclaration
BlockStatement ::=
	(Identifier ":")? Statement
Annotation ::=
	"@" TypeName ("(" (Identifier "=")? ElementValue ")")?
SwitchBlockStatementGroups ::=
	SwitchBlockStatementGroup*
EnumConstant ::=
	Annotations Identifier Arguments? ClassBody?
QualifiedIdentifier ::=
	Identifier ("." Identifier)*
ConstantDeclaratorsRest ::=
	ConstantDeclaratorRest ("," ConstantDeclarator)*
AnnotationTypeDeclaration ::=
	"@" "interface" Identifier AnnotationTypeBody
NonWildcardTypeArguments ::=
	"<" TypeList ">"
MoreStatementExpressions ::=
	("," StatementExpression)*
Expression3 ::=
	PrefixOp Expression3
Expression3 ::=
	"(" (Expression | Type) ")" Expression3
Expression3 ::=
	Primary Selector* PostfixOp*
BlockStatements ::=
	BlockStatement*
IdentifierSuffix ::=
	"[" ("]" ("[" "]")* "." "class" | Expression "]")
IdentifierSuffix ::=
	Arguments
IdentifierSuffix ::=
	"." ("class" | ExplicitGenericInvocation | "this" | "super" Arguments | "new" NonWildcardTypeArguments? InnerCreator)
InterfaceMethodDeclaratorRest ::=
	FormalParameters ("[" "]")* ("throws" QualifiedIdentifierList)? ";"
FormalParameterDeclsRest ::=
	VariableDeclaratorId ("," FormalParameterDecls)?
FormalParameterDeclsRest ::=
	"..." VariableDeclaratorId
AssignmentOperator ::=
	"="
AssignmentOperator ::=
	"+="
AssignmentOperator ::=
	"-="
AssignmentOperator ::=
	"*="
AssignmentOperator ::=
	"/="
AssignmentOperator ::=
	"&="
AssignmentOperator ::=
	"|="
AssignmentOperator ::=
	"^="
AssignmentOperator ::=
	"%="
AssignmentOperator ::=
	"<<="
AssignmentOperator ::=
	">>="
AssignmentOperator ::=
	">>>="
ParExpression ::=
	"(" Expression ")"
AnnotationTypeElementDeclarations ::=
	AnnotationTypeElementDeclaration
AnnotationTypeElementDeclarations ::=
	AnnotationTypeElementDeclarations AnnotationTypeElementDeclaration
VariableDeclaratorsRest ::=
	VariableDeclaratorRest ("," VariableDeclarator)*
ForControl ::=
	ForVarControl
ForControl ::=
	ForInit ";" Expression? ";" ForUpdate?
ForControl ::=
	"final"? Annotations? Type Identifier ForVarControlRest
VariableDeclarator ::=
	Identifier VariableDeclaratorRest
ConstantExpression ::=
	Expression
SwitchLabel ::=
	"case" ConstantExpression ":"
SwitchLabel ::=
	"case" EnumConstantName ":"
SwitchLabel ::=
	"default" ":"
ElementValues ::=
	ElementValue ElementValues?
InterfaceMemberDecl ::=
	InterfaceMethodOrFieldDecl
InterfaceMemberDecl ::=
	InterfaceGenericMethodDecl
InterfaceMemberDecl ::=
	"void" Identifier VoidInterfaceMethodDeclaratorRest
InterfaceMemberDecl ::=
	InterfaceDeclaration
InterfaceMemberDecl ::=
	ClassDeclaration
AnnotationConstantRest ::=
	VariableDeclarators
QualifiedIdentifierList ::=
	QualifiedIdentifier ("," QualifiedIdentifier)*
GenericMethodOrConstructorRest ::=
	(Type | "void") Identifier MethodDeclaratorRest
GenericMethodOrConstructorRest ::=
	Identifier ConstructorDeclaratorRest
ArrayCreatorRest ::=
	"[" ("]" ("[" "]")* ArrayInitializer | Expression "]" ("[" Expression "]")* ("[" "]")*)
Type ::=
	Identifier TypeArguments? ("." Identifier TypeArguments?)* ("[" "]")*
Type ::=
	BasicType
FormalParameters ::=
	"(" FormalParameterDecls? ")"
TypeList ::=
	Type ("," Type)*
Selector ::=
	"." Identifier Arguments?
Selector ::=
	"." ExplicitGenericInvocation
Selector ::=
	"." "this"
Selector ::=
	"." "super" SuperSuffix
Selector ::=
	"." "new" NonWildcardTypeArguments? InnerCreator
Selector ::=
	Expression?
VoidInterfaceMethodDeclaratorRest ::=
	FormalParameters ("throws" QualifiedIdentifierList)? ";"
InterfaceBodyDeclaration ::=
	";"
InterfaceBodyDeclaration ::=
	Modifier* InterfaceMemberDecl
MethodDeclaratorRest ::=
	FormalParameters ("[" "]")* ("throws" QualifiedIdentifierList)? (MethodBody | ";")
ArrayInitializer ::=
	(VariableInitializer ("," VariableInitializer)* ","?)?*
EnumConstants ::=
	EnumConstant
EnumConstants ::=
	EnumConstants "," EnumConstant
VariableDeclaratorRest ::=
	("[" "]")* ("=" VariableInitializer)?
MethodOrFieldRest ::=
	VariableDeclaratorRest
MethodOrFieldRest ::=
	MethodDeclaratorRest
Modifier ::=
	Annotation
Modifier ::=
	"public"
Modifier ::=
	"protected"
Modifier ::=
	"private"
Modifier ::=
	"static"
Modifier ::=
	"abstract"
Modifier ::=
	"final"
Modifier ::=
	"native"
Modifier ::=
	"synchronized"
Modifier ::=
	"transient"
Modifier ::=
	"volatile"
Modifier ::=
	"strictfp"
Block ::=
	BlockStatements*
PostfixOp ::=
	"++"
PostfixOp ::=
	"--"
ForInit ::=
	StatementExpression Expressions
EnumDeclaration ::=
	"enum" Identifier ("implements" TypeList)? EnumBody
VariableDeclarators ::=
	VariableDeclarator ("," VariableDeclarator)*
Primary ::=
	ParExpression
Primary ::=
	NonWildcardTypeArguments (ExplicitGenericInvocationSuffix | "this" Arguments)
Primary ::=
	"this" Arguments?
Primary ::=
	"super" SuperSuffix
Primary ::=
	Literal
Primary ::=
	"new" Creator
Primary ::=
	Identifier ("." Identifier)* IdentifierSuffix?
Primary ::=
	BasicType ("[" "]")* "." "class"
Primary ::=
	"void" "." "class"
Expression2Rest ::=
	(InfixOp Expression3)*
Expression2Rest ::=
	Expression3 "instanceof" Type
VariableInitializer ::=
	ArrayInitializer
VariableInitializer ::=
	Expression
Literal ::=
	IntegerLiteral
Literal ::=
	FloatingPointLiteral
Literal ::=
	CharacterLiteral
Literal ::=
	StringLiteral
Literal ::=
	BooleanLiteral
Literal ::=
	NullLiteral
ExplicitGenericInvocationSuffix ::=
	"super" SuperSuffix
ExplicitGenericInvocationSuffix ::=
	Identifier Arguments
Identifier ::=
	IDENTIFIER
ImportDeclaration ::=
	"import" "static"? Identifier ("." Identifier)* ("." "*")? ";"
BasicType ::=
	"byte"
BasicType ::=
	"short"
BasicType ::=
	"char"
BasicType ::=
	"int"
BasicType ::=
	"long"
BasicType ::=
	"float"
BasicType ::=
	"double"
BasicType ::=
	"boolean"
GenericMethodOrConstructorDecl ::=
	TypeParameters GenericMethodOrConstructorRest
ConstantDeclarator ::=
	Identifier ConstantDeclaratorRest
ClassBody ::=
	"{" ClassBodyDeclaration* "}"
MemberDecl ::=
	GenericMethodOrConstructorDecl
MemberDecl ::=
	MethodOrFieldDecl
MemberDecl ::=
	"void" Identifier VoidMethodDeclaratorRest
MemberDecl ::=
	Identifier ConstructorDeclaratorRest
MemberDecl ::=
	InterfaceDeclaration
MemberDecl ::=
	ClassDeclaration
SwitchBlockStatementGroup ::=
	SwitchLabel BlockStatements
Catches ::=
	CatchClause CatchClause*
InterfaceMethodOrFieldRest ::=
	ConstantDeclaratorsRest ";"
InterfaceMethodOrFieldRest ::=
	InterfaceMethodDeclaratorRest
Expression1Rest ::=
	"?" Expression ":" Expression1
TypeArguments ::=
	"<" TypeArgument ("," TypeArgument)* ">"
Expression ::=
	Expression1 (AssignmentOperator Expression1)?
ConditionalExpression ::=
	Expression2 Expression1Rest
Creator ::=
	NonWildcardTypeArguments? CreatedName (ArrayCreatorRest | ClassCreatorRest)
InfixOp ::=
	"||"
InfixOp ::=
	"&&"
InfixOp ::=
	"|"
InfixOp ::=
	"^"
InfixOp ::=
	"&"
InfixOp ::=
	"=="
InfixOp ::=
	"!="
InfixOp ::=
	"<"
InfixOp ::=
	">"
InfixOp ::=
	"<="
InfixOp ::=
	">="
InfixOp ::=
	"<<"
InfixOp ::=
	">>"
InfixOp ::=
	">>>"
InfixOp ::=
	"+"
InfixOp ::=
	"-"
InfixOp ::=
	"*"
InfixOp ::=
	"/"
InfixOp ::=
	"%"
ClassOrInterfaceDeclaration ::=
	Modifier* (ClassDeclaration | InterfaceDeclaration)
ConstructorDeclaratorRest ::=
	FormalParameters ("throws" QualifiedIdentifierList)? MethodBody
InterfaceMethodOrFieldDecl ::=
	Type Identifier InterfaceMethodOrFieldRest
ClassDeclaration ::=
	NormalClassDeclaration
ClassDeclaration ::=
	EnumDeclaration
MethodOrFieldDecl ::=
	Type Identifier MethodOrFieldRest
InnerCreator ::=
	Identifier ClassCreatorRest
PrefixOp ::=
	"++"
PrefixOp ::=
	"--"
PrefixOp ::=
	"!"
PrefixOp ::=
	"~"
PrefixOp ::=
	"+"
PrefixOp ::=
	"-"
CreatedName ::=
	Identifier NonWildcardTypeArguments? ("." Identifier NonWildcardTypeArguments?)*
TypeDeclaration ::=
	ClassOrInterfaceDeclaration
TypeDeclaration ::=
	";"
FormalParameterDecls ::=
	("final" Annotations? Type FormalParameterDeclsRest)?
ConstantDeclaratorRest ::=
	("[" "]")* "=" VariableInitializer
StatementExpression ::=
	Expression
Arguments ::=
	"(" (Expression ("," Expression)*)? ")"
ElementValueArrayInitializer ::=
	"{" ElementValues? ","? "}"
VariableDeclaratorId ::=
	Identifier ("[" "]")*
AnnotationMethodRest ::=
	"(" ")" DefaultValue?
VoidMethodDeclaratorRest ::=
	FormalParameters ("throws" QualifiedIdentifierList)? (MethodBody | ";")
AnnotationTypeBody ::=
	AnnotationTypeElementDeclarations?*
LocalVariableDeclarationStatement ::=
	"final"? Type VariableDeclarators ";"
Expression1 ::=
	Expression2 Expression1Rest?
TypeParameters ::=
	"<" TypeParameter ("," TypeParameter)* ">"
Expression2 ::=
	Expression3 Expression2Rest?
MethodBody ::=
	Block
ForVarControlRest ::=
	VariableDeclaratorsRest ";" Expression? ";" ForUpdate?
ForVarControlRest ::=
	":" Expression
EnumBodyDeclarations ::=
	";" ClassBodyDeclaration*
DefaultValue ::=
	"default" ElementValue
AnnotationMethodOrConstantRest ::=
	AnnotationMethodRest
AnnotationMethodOrConstantRest ::=
	AnnotationConstantRest
SuperSuffix ::=
	Arguments
SuperSuffix ::=
	"." Identifier Arguments?
InterfaceGenericMethodDecl ::=
	TypeParameters (Type | "void") Identifier InterfaceMethodDeclaratorRest
EnumBody ::=
	(EnumConstants? ","? EnumBodyDeclarations?)*
CompilationUnit ::=
	(Annotations? "package" QualifiedIdentifier ";")? ImportDeclaration* TypeDeclaration*
InterfaceBody ::=
	"{" InterfaceBodyDeclaration* "}"
EnumConstantName ::=
	Identifier
Bound ::=
	Type ("&" Type)*
TypeArgument ::=
	Type
TypeArgument ::=
	"?" (("extends" | "super") Type)?
AnnotationTypeElementRest ::=
	Type Identifier AnnotationMethodOrConstantRest ";"
AnnotationTypeElementRest ::=
	ClassDeclaration
AnnotationTypeElementRest ::=
	InterfaceDeclaration
AnnotationTypeElementRest ::=
	EnumDeclaration
AnnotationTypeElementRest ::=
	AnnotationTypeDeclaration
AnnotationTypeElementDeclaration ::=
	Modifier* AnnotationTypeElementRest
NormalInterfaceDeclaration ::=
	"interface" Identifier TypeParameters? ("extends" TypeList)? InterfaceBody

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