Browsable Java (J2SE 5.0 “Tiger”) Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: java/java-5/stahl/extracted
Source used for this grammar: Michael Stahl, java15.g, 2004 [ANTLR2]

Summary

Syntax

compilationUnit ::=
	(packageDefinition | ε) importDefinition* typeDefinition*
packageDefinition ::=
	annotations [p]::"package" identifier SEMI
annotations ::=
	annotation*
annotation ::=
	AT identifier annotationInit
AT ::=
	"@"
identifier ::=
	IDENT (DOT IDENT)*
IDENT ::=
	("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "_" | "$") ("a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" | "_" | "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "$")*
DOT ::=
	"."
annotationInit ::=
	[lp]::LPAREN (annotationMemberInit (COMMA annotationMemberInit)* | annotationMemberValue | ε) RPAREN
	ε
LPAREN ::=
	"("
annotationMemberInit ::=
	IDENT ASSIGN annotationMemberValue
ASSIGN ::=
	"="
annotationMemberValue ::=
	annotation
	conditionalExpression
	annotationMemberArrayInitializer
conditionalExpression ::=
	logicalOrExpression (QUESTION assignmentExpression COLON conditionalExpression)?
logicalOrExpression ::=
	logicalAndExpression (LOR logicalAndExpression)*
logicalAndExpression ::=
	inclusiveOrExpression (LAND inclusiveOrExpression)*
inclusiveOrExpression ::=
	exclusiveOrExpression (BOR exclusiveOrExpression)*
exclusiveOrExpression ::=
	andExpression (BXOR andExpression)*
andExpression ::=
	equalityExpression (BAND equalityExpression)*
equalityExpression ::=
	relationalExpression ((NOT_EQUAL | EQUAL) relationalExpression)*
relationalExpression ::=
	shiftExpression (((LT | GT | LE | GE) shiftExpression)* | "instanceof" typeSpec)
shiftExpression ::=
	additiveExpression ((SL | SR | BSR) additiveExpression)*
additiveExpression ::=
	multiplicativeExpression ((PLUS | MINUS) multiplicativeExpression)*
multiplicativeExpression ::=
	unaryExpression ((STAR | DIV | MOD) unaryExpression)*
unaryExpression ::=
	INC unaryExpression
	DEC unaryExpression
	MINUS unaryExpression
	PLUS unaryExpression
	unaryExpressionNotPlusMinus
INC ::=
	"++"
DEC ::=
	"--"
MINUS ::=
	"-"
PLUS ::=
	"+"
unaryExpressionNotPlusMinus ::=
	BNOT unaryExpression
	LNOT unaryExpression
	[lpb]::LPAREN builtInTypeSpec RPAREN unaryExpression
	[lp]::LPAREN classTypeSpec RPAREN unaryExpressionNotPlusMinus
	postfixExpression
BNOT ::=
	"~"
LNOT ::=
	"!"
builtInTypeSpec ::=
	builtInType ([lb]::LBRACK RBRACK)*
builtInType ::=
	"void"
	"boolean"
	"byte"
	"char"
	"short"
	"int"
	"float"
	"long"
	"double"
LBRACK ::=
	"["
RBRACK ::=
	"]"
RPAREN ::=
	")"
classTypeSpec ::=
	classOrInterfaceType ([lb]::LBRACK RBRACK)*
classOrInterfaceType ::=
	IDENT typeArguments (DOT IDENT typeArguments)*
typeArguments ::=
	[lt]::LT typeArgument (COMMA typeArgument)* typeArgumentsEnd?
	ε
LT ::=
	"<"
typeArgument ::=
	[q]::QUESTION ("extends" referenceTypeSpec | "super" referenceTypeSpec)?
	referenceTypeSpec
QUESTION ::=
	"?"
referenceTypeSpec ::=
	classTypeSpec
	arrayTypeSpec
arrayTypeSpec ::=
	builtInType ([lb]::LBRACK RBRACK)+
COMMA ::=
	","
typeArgumentsEnd ::=
	GT
	SR
	BSR
GT ::=
	">"
SR ::=
	">>"
BSR ::=
	">>>"
postfixExpression ::=
	primaryExpression (DOT "this" | DOT [ta1]::typeArguments (IDENT ([lp]::LPAREN argList RPAREN)? | "super" ([lp3]::LPAREN argList RPAREN | DOT [ta2]::typeArguments IDENT ([lps]::LPAREN argList RPAREN)?)) | DOT newExpression | [lb]::LBRACK expression RBRACK)* ([in]::INC | [de]::DEC)?
primaryExpression ::=
	identPrimary (DOT "class")?
	constant
	"true"
	"false"
	"null"
	newExpression
	"this"
	"super"
	LPAREN assignmentExpression RPAREN
	builtInType ([lbt]::LBRACK RBRACK)* DOT "class"
identPrimary ::=
	[ta1]::typeArguments IDENT (DOT [ta2]::typeArguments IDENT | ε)* ([lp]::LPAREN argList RPAREN | ([lbc]::LBRACK RBRACK)+)?
argList ::=
	expressionList
	ε
expressionList ::=
	expression (COMMA expression)*
expression ::=
	assignmentExpression
assignmentExpression ::=
	conditionalExpression ((ASSIGN | PLUS_ASSIGN | MINUS_ASSIGN | STAR_ASSIGN | DIV_ASSIGN | MOD_ASSIGN | SR_ASSIGN | BSR_ASSIGN | SL_ASSIGN | BAND_ASSIGN | BXOR_ASSIGN | BOR_ASSIGN) assignmentExpression)?
PLUS_ASSIGN ::=
	"+="
MINUS_ASSIGN ::=
	"-="
STAR_ASSIGN ::=
	"*="
DIV_ASSIGN ::=
	"/="
MOD_ASSIGN ::=
	"%="
SR_ASSIGN ::=
	">>="
BSR_ASSIGN ::=
	">>>="
SL_ASSIGN ::=
	"<<="
BAND_ASSIGN ::=
	"&="
BXOR_ASSIGN ::=
	"^="
BOR_ASSIGN ::=
	"|="
constant ::=
	NUM_INT
	CHAR_LITERAL
	STRING_LITERAL
	NUM_FLOAT
	NUM_LONG
	NUM_DOUBLE
NUM_INT ::=
	"." ("." "." | (("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ EXPONENT? [f1]::FLOAT_SUFFIX?)?)
	("0" (("x" | "X") HEX_DIGIT+ | ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ | ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7")+)? | ("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9") ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")*) ("l" | "L" | "." ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")* EXPONENT? [f2]::FLOAT_SUFFIX? | EXPONENT [f3]::FLOAT_SUFFIX? | [f4]::FLOAT_SUFFIX)?
EXPONENT ::=
	("e" | "E") ("+" | "-")? ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+
FLOAT_SUFFIX ::=
	"f"
	"F"
	"d"
	"D"
HEX_DIGIT ::=
	"0"
	"1"
	"2"
	"3"
	"4"
	"5"
	"6"
	"7"
	"8"
	"9"
	"A"
	"B"
	"C"
	"D"
	"E"
	"F"
	"a"
	"b"
	"c"
	"d"
	"e"
	"f"
CHAR_LITERAL ::=
	"\'" (ESC | string) "\'"
ESC ::=
	"\\" ("n" | "r" | "t" | "b" | "f" | """ | "\'" | "\\" | "u"+ HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT | ("0" | "1" | "2" | "3") (("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7") ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7")?)? | ("4" | "5" | "6" | "7") ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7")?)
STRING_LITERAL ::=
	""" (ESC | string)* """
NUM_FLOAT ::=
	"." ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ EXPONENT? ("f" | "F")
	"0" ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ ("." | EXPONENT | "f" | "F")
	("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")* (("." ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")*)? EXPONENT? ("f" | "F") | EXPONENT)
NUM_LONG ::=
	("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")* ("l" | "L")
NUM_DOUBLE ::=
	"." ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ EXPONENT? ("d" | "D")
	"0" ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ ("." | EXPONENT | "d" | "D")
	("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+ ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")* (("." ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")*)? EXPONENT? ("d" | "D") | EXPONENT)
newExpression ::=
	"new" typeArguments type (LPAREN argList RPAREN classBlock? | newArrayDeclarator arrayInitializer?)
type ::=
	classOrInterfaceType
	builtInType
classBlock ::=
	LCURLY (classField | SEMI)* RCURLY
LCURLY ::=
	"{"
classField ::=
	[mods]::modifiers ([it]::innerTypeDef | [tp]::typeParameters ([h]::ctorHead [s]::constructorBody | [md]::memberDef))
	"static" [s3]::compoundStatement
	[s4]::compoundStatement
modifiers ::=
	(modifier | annotation)*
modifier ::=
	"private"
	"public"
	"protected"
	"static"
	"transient"
	"final"
	"abstract"
	"native"
	"threadsafe"
	"synchronized"
	"volatile"
	"strictfp"
innerTypeDef ::=
	[ed]::enumDefinition
	[cd]::classDefinition
	[id]::interfaceDefinition
	[ad]::annotationTypeDefinition
enumDefinition ::=
	ENUM IDENT [ic]::implementsClause [eb]::enumBlock
ENUM ::=
	"enum"
implementsClause ::=
	([i]::"implements" classOrInterfaceType (COMMA classOrInterfaceType)*)?
enumBlock ::=
	LCURLY (enumConst (COMMA enumConst)*)? COMMA? (SEMI (classField | SEMI)*)? RCURLY
enumConst ::=
	annotations IDENT enumConstInit classBlock?
enumConstInit ::=
	[lp]::LPAREN argList RPAREN
	ε
SEMI ::=
	";"
RCURLY ::=
	"}"
classDefinition ::=
	"class" IDENT [tp]::typeParameters [sc]::superClassClause [ic]::implementsClause [cb]::classBlock
typeParameters ::=
	[lt]::LT typeParameter (COMMA typeParameter)* typeArgumentsEnd?
	ε
typeParameter ::=
	IDENT ("extends" classOrInterfaceType (BAND classOrInterfaceType)*)?
BAND ::=
	"&"
superClassClause ::=
	("extends" classOrInterfaceType)?
interfaceDefinition ::=
	"interface" IDENT [tp]::typeParameters [ie]::interfaceExtends [ib]::interfaceBlock
interfaceExtends ::=
	([e]::"extends" classOrInterfaceType (COMMA classOrInterfaceType)*)?
interfaceBlock ::=
	LCURLY (interfaceField | SEMI)* RCURLY
interfaceField ::=
	[mods]::modifiers ([it]::innerTypeDef | [tp]::typeParameters [md]::memberDef)
memberDef ::=
	[t]::typeSpec (IDENT LPAREN [param]::parameterDeclarationList RPAREN [rt]::declaratorBrackets [tc]::throwsClause? (SEMI | [s2]::compoundStatement) | [v]::variableDefinitions SEMI)
typeSpec ::=
	classTypeSpec
	builtInTypeSpec
parameterDeclarationList ::=
	(parameterDeclaration (COMMA parameterDeclaration)*)?
parameterDeclaration ::=
	[pm]::parameterModifier [t]::typeSpec [el]::ELLIPSIS? [id]::IDENT [pd]::declaratorBrackets
parameterModifier ::=
	("final" | annotation)*
ELLIPSIS ::=
	"..."
declaratorBrackets ::=
	([lb]::LBRACK RBRACK)*
throwsClause ::=
	"throws" identifier (COMMA identifier)*
compoundStatement ::=
	[lc]::LCURLY statement* RCURLY
statement ::=
	compoundStatement
	declaration SEMI
	expression SEMI
	[m]::modifiers (enumDefinition | classDefinition)
	IDENT [c]::COLON statement
	"if" LPAREN expression RPAREN statement ("else" statement)?
	"for" LPAREN (parameterDeclaration COLON expression | forInit SEMI forCond SEMI forIter) RPAREN statement
	"while" LPAREN expression RPAREN statement
	"do" statement "while" LPAREN expression RPAREN SEMI
	"break" IDENT? SEMI
	"continue" IDENT? SEMI
	"return" expression? SEMI
	"switch" LPAREN expression RPAREN LCURLY casesGroup* RCURLY
	tryBlock
	"throw" expression SEMI
	"synchronized" LPAREN expression RPAREN compoundStatement
	ASSERT expression (COLON expression)? SEMI
	[s]::SEMI
declaration ::=
	[m]::modifiers [t]::typeSpec [v]::variableDefinitions
variableDefinitions ::=
	variableDeclarator (COMMA variableDeclarator)*
variableDeclarator ::=
	[id]::IDENT [d]::declaratorBrackets [v]::varInitializer
varInitializer ::=
	(ASSIGN initializer)?
initializer ::=
	expression
	arrayInitializer
arrayInitializer ::=
	[lc]::LCURLY (initializer (COMMA initializer)*)? COMMA? RCURLY
COLON ::=
	":"
forInit ::=
	(declaration | expressionList)?
forCond ::=
	expression?
forIter ::=
	expressionList?
casesGroup ::=
	aCase+ caseSList
aCase ::=
	("case" expression | "default") COLON
caseSList ::=
	statement*
tryBlock ::=
	"try" compoundStatement handler* finallyClause?
handler ::=
	"catch" LPAREN parameterDeclaration RPAREN compoundStatement
finallyClause ::=
	"finally" compoundStatement
ASSERT ::=
	"assert"
annotationTypeDefinition ::=
	AT "interface" IDENT [ab]::annotationBlock
annotationBlock ::=
	LCURLY (annotationField | SEMI)* RCURLY
annotationField ::=
	[mods]::modifiers ([it]::innerTypeDef | [ts]::typeSpec ([i]::IDENT LPAREN RPAREN [dv]::defaultValue SEMI | [v]::variableDefinitions SEMI))
defaultValue ::=
	("default" annotationMemberValue)?
ctorHead ::=
	IDENT LPAREN parameterDeclarationList RPAREN throwsClause?
constructorBody ::=
	[lc]::LCURLY explicitConstructorInvocation? statement* RCURLY
explicitConstructorInvocation ::=
	typeArguments ("this" [lp1]::LPAREN argList RPAREN SEMI | "super" [lp2]::LPAREN argList RPAREN SEMI)
newArrayDeclarator ::=
	([lb]::LBRACK expression? RBRACK)+
STAR ::=
	"*"
DIV ::=
	"/"
MOD ::=
	"%"
SL ::=
	"<<"
LE ::=
	"<="
GE ::=
	">="
NOT_EQUAL ::=
	"!="
EQUAL ::=
	"=="
BXOR ::=
	"^"
BOR ::=
	"|"
LAND ::=
	"&&"
LOR ::=
	"||"
annotationMemberArrayInitializer ::=
	[lc]::LCURLY (annotationMemberValue (COMMA annotationMemberValue)*)? COMMA? RCURLY
importDefinition ::=
	[i]::"import" "static"? identifierStar SEMI
identifierStar ::=
	IDENT (DOT IDENT)* (DOT STAR)?
typeDefinition ::=
	[m]::modifiers (classDefinition | enumDefinition | interfaceDefinition | annotationTypeDefinition)
	SEMI

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