Concise BNF Grammar for Java 2.0 By K.W. Regan, drawing on JLS and U.Helsinki CS -------------------------------------------------------------------------------- BNF: [item] means item is optional; {item} means 0 or more occurrences of item. (item1 | item2) means take item1 or item2; [item1 | item2] makes none an option. Quotes are used for literal "{", "[", "(", etc. and for clarity in other places. Nonterminals in CAPS, terminals in lowercase; <..> means implementation-defined. Some deprecated syntax has been removed. CMODS ::= [public] [abstract] [final] [strictfp] //here ordering is suggested IMODS ::= [public] [abstract] [strictfp] //for style but not required FMODS ::= [PPP] [static] [final] [transient] [volatile] MMODS ::= [PPP] [abstract] [static] [final] [synchronized] [native] [strictfp] PPP ::= public | protected | private DOC ::= /** */ //Documentation comment COMPUNIT ::= [package NAME";"] {import NAME[.*]";"} {[DOC] (CMODS CLASS | IMODS INTERFACE)} CLASS ::= class [extends NAME] [implements NAME{"," NAME}] "{"{MEMBER}"}" MEMBER ::= [static] BLOCK //Initializer code for class; not really a member | [DOC] [PPP] [abstract] [static] [final] [strictfp] CLASS | [DOC] [PPP] [abstract] [static] [final] [strictfp] INTERFACE | [DOC] [PPP] CONSTRUCTOR //Constructors cannot have "strictfp"; | [DOC] [MMODS] METHOD //they are strictfp if their class is. | [DOC] [FMODS] FIELD INTERFACE ::= "interface" [extends NAME{"," NAME}] "{" {IMEMBER} "}" IMEMBER ::= [DOC] [PPP] [abstract] [static] [final] [strictfp] CLASS | [DOC] [PPP] [abstract] [static] [final] [strictfp] INTERFACE | [DOC] [MMODS] METHOD //"abstract" legal but redundant. | [DOC] [PPP] static final [transient] [volatile] FIELD FIELD ::= TYPE VARIABLE {"," VARIABLE}";" VARIABLE::= [= EXP] | "[ ]" {"[ ]"} [= ARRINIT] INIT ::= ARRINIT | EXP ARRINIT ::= "{" [INIT{","INIT}][","] "}" //OK to have extra trailing comma! METHOD ::= (void | TYPE) PARAMS [throws NAME{"," NAME}] BLOCK PARAMS ::= "(" [PARAM{"," PARAM}] ")" PARAM ::= [final] TYPE {"[ ]"} CONSTRUCTOR ::= [PPP] PARAMS [throws NAME{"," NAME}] CBODY CBODY ::= "{" [[PRIMEXP.](this | super) ARGS";"] {BLOCKSTMT} "}" ARGS ::= "(" [EXP{","EXP}] ")" TYPE ::= (PRIMTYPE | NAME) {"[ ]"} PRIMTYPE::= boolean | char | byte | short | int | long | float | double NAME ::= {"."} EXP ::= EXP1 [ ASSGTOP EXP ] EXP1 ::= EXP2 [ "?" EXP ":" EXP1 ] EXP2 ::= EXP3 | EXP2 BINOP EXP2 | EXP2 instanceof TYPE //see precedence note EXP3 ::= +EXP3 | -EXP3 | [++ | --]PRIMEXP | EXP4 EXP4 ::= ~EXP3 | !EXP3 | PRIMEXP[++ | --] | CASTEXP CASTEXP ::= "(" PRIMTYPE {"[ ]"} ")" EXP3 | "(" TYPE ")" EXP4 ASSGTOP ::= = | *= | /= | %= | += | -= | <<= | >>= | >>>= | &= | ^= | "|=" BINOP ::= || && | ^ & ==,!= <,>,<=,>= <<,>>,>>> +,- *,/,% //Binops grouped by precedence, with "instanceof" thrown in with "<,>,<=,>=". PRIMEXP ::= NEWARRAY | ITEM //ITEM = "PrimaryNoNewArray" in JLS NEWARRAY::= new (NAME | PRIMTYPE) "[" EXP "]" {"[" EXP "]"} {"[ ]"} | new (NAME | PRIMTYPE) "[ ]" {"[ ]"} ARRINIT //for anonymous arrays ITEM ::= "(" EXP ")" | LITERAL | NAME | [NAME.]this | [NAME.]super. | INSTANCE | ARRAYACC | FIELDACC | METHCALL | (TYPE | void).class LITERAL ::= [d | D | f | F] //defaults to double | | | | true | false | null INSTANCE::= new TYPE ARGS ["{" {MEMBER} "}"] //part in [..] allows | PRIMEXP.new ARGS ["{" {MEMBER} "}"] //"anonymous classes" ARRAYACC::= (NAME | ITEM) "[" EXP "]" FIELDACC::= PRIMEXP. | [NAME.]super. //NAME must name a class METHCALL::= NAME ARGS | PRIMEXP. ARGS | [NAME.]super. ARGS BLOCK ::= "{" {BLOCKSTMT} "}" BLOCKSTMT ::= [":"] STMT | CLASS | INTERFACE | [final] FIELD STMT ::= ";" | BLOCK //yes, ";" and "{}" are legal statements! | STMTEXP ";" | switch "(" EXP ")" "{" {SWITCHLABEL {BLOCKSTMT}} "}" | if "(" EXP ")" STMT ["else" STMT] | while "(" EXP ")" STMT | do STMT while "(" EXP ")" ";" | for "(" [FORINIT] ";" [EXP] ";" [STMTEXP{"," STMTEXP}] ")" STMT | break [] ";" | continue [] ";" | return [EXP] ";" | throw EXP ";" | synchronized "(" EXP ")" BLOCK | try BLOCK catch "("PARAM")" BLOCK {catch "("PARAM")" BLOCK} [finally BLOCK] | try BLOCK finally BLOCK STMTEXP ::= ++PRIMEXP | --PRIMEXP | PRIMEXP[++ | --] | PRIMEXP ASSGTOP EXP SWITCHLABEL ::= case EXP ":" | default ":" //EXP must be a constant FORINIT ::= TYPE VARIABLE{"," VARIABLE} | STMTEXP{"," STMTEXP} References: James Gosling, Bill Joy, Guy Steele, The Java Language Specification Grammar at: http://www.javasoft.com/docs/books/jls/html/19.doc.html#52996 plus updates at: http://www.javasoft.com/docs/books/jls/clarify.html Helsinki CS grammar: http://www.cs.helsinki.fi/group/jasso/selain/Java1.1x.html