BNF Rules of Java


Source: http://cuiwww.unige.ch (Orignal source not attributed. One assumes Sun Microsystems, circa 1994.) HTML © 2008, Martin Rinehart (www.MartinRinehart.com)
compilation_unit [ package_statement ] < import_statement > < type_declaration >
package_statement "package" package_name ";"
import_statement "import" ( ( package_name "." "*" ";" ) / ( class_name / interface_name ) ) ";"
type_declaration [ doc_comment ] ( class_declaration / interface_declaration ) ";"
doc_comment "/**" "... text ..." "*/"
class_declaration = < modifier > "class" identifier [ "extends" class_name ] [ "implements" interface_name < "," interface_name > ] "{" < field_declaration > "}"
interface_declaration < modifier > "interface" identifier [ "extends" interface_name < "," interface_name > ] "{" < field_declaration > "}"
field_declaration ( [ doc_comment ] ( method_declaration / constructor_declaration / variable_declaration ) ) / static_initializer / ";"
method_declaration < modifier > type identifier "(" [ parameter_list ] ")" < "[" "]" > ( statement_block / ";" )
constructor_declaration < modifier > identifier "(" [ parameter_list ] ")"statement_block
statement_block "{" < statement > "}"
variable_declaration < modifier > type variable_declarator < "," variable_declarator > ";"
variable_declarator identifier < "[" "]" > [ "=" variable_initializer ]
variable_initializer expression / ( "{" [ variable_initializer < "," variable_initializer > [ "," ] ] "}" )
static_initializer "static" statement_block
parameter_list parameter < "," parameter >
parameter type identifier < "[" "]" >
statement variable_declaration / ( expression ";" ) / ( statement_block ) / ( if_statement ) / ( do_statement ) / ( while_statement ) / ( for_statement ) / ( try_statement ) / ( switch_statement ) / ( "synchronized" "(" expression ")" statement ) / ( "return" [ expression ] ";" ) / ( "throw" expression ";" ) / ( identifier ":" statement ) / ( "break" [ identifier ] ";" ) / ( "continue" [ identifier ] ";" ) / ( ";" )
if_statement "if" "(" expression ")" statement [ "else" statement ]
do_statement "do" statement "while" "(" expression ")" ";"
while_statement "while" "(" expression ")" statement
for_statement "for" "(" ( variable_declaration / ( expression ";" ) / ";" ) [ expression ] ";" [ expression ] ";" ")" statement
try_statement "try" statement < "catch" "(" parameter ")" statement > [ "finally" statement ]
switch_statement "switch" "(" expression ")" "{" < ( "case" expression ":" ) / ( "default" ":" ) / statement > "}"
expression numeric_expression / testing_expression / logical_expression / string_expression / bit_expression / casting_expression / creating_expression / literal_expression / "null" / "super" / "this" / identifier / ( "(" expression ")" ) / ( expression ( ( "(" [ arglist ] ")" ) / ( "[" expression "]" ) / ( "." expression ) / ( "," expression ) / ( "instanceof" ( class_name / interface_name ) ) ) )
numeric_expression ( ( "-" / "++" / "--" )expression ) / ( expression ( "++" / "--" ) ) / ( expression ( "+" / "+=" / "-" / "-=" / "*" / "*=" / "/" / "/=" / "%" / "%=" ) expression )
testing_expression ( expression ( ">" / "<" / ">=" / "<=" / "==" / "!=" ) expression )
logical_expression ( "!" expression ) / ( expression ( "ampersand" / "ampersand=" / "|" / "|=" / "^" / "^=" / ( "ampersand" "ampersand" ) / "||=" / "%" / "%=" ) expression ) / ( expression "?" expression ":" expression ) / "true" / "false"
string_expression ( expression ( "+" / "+=" )expression )
bit_expression ( "~" expression ) / ( expression ( ">>=" / "<<" / ">>" / ">>>" ) expression )
casting_expression "(" type ")" expression
creating_expression "new" ( ( classe_name "(" [ arglist ] ")" ) / ( type_specifier [ "[" expression "]" ] < "[" "]" > ) / ( "(" expression ")" ) )
literal_expression integer_literal / float_literal / string / character
arglist expression < "," expression >
type type_specifier < "[" "]" >
type_specifier "boolean" / "byte" / "char" / "short" / "int" / "float" / "long" / "double" / class_name / interface_name
modifier "public" / "private" / "protected" / "static" / "final" / "native" / "synchronized" / "abstract" / "threadsafe" / "transient"
package_name identifier / ( package_name "." identifier )
class_name identifier / ( package_name "." identifier )
interface_name identifier / ( package_name "." identifier )
integer_literal ( ( "1..9" < "0..9" > ) / < "0..7" > / ( "0" "x" "0..9a..f" < "0..9a..f" > ) ) [ "l" ]
float_literal ( decimal_digits "." [ decimal_digits ] [ exponent_part ] [ float_type_suffix ] ) / ( "." decimal_digits [ exponent_part ] [ float_type_suffix ] ) / ( decimal_digits [ exponent_part ] [ float_type_suffix ] )
decimal_digits "0..9" < "0..9" >
exponent_part "e" [ "+" / "-" ] decimal_digits
float_type_suffix "f" / "d"
character "based on the unicode character set"
string "''" < character > "''"
identifier "a..z,$,_" < "a..z,$,_,0..9,unicode character over 00C0" >