A context free grammar for Java ------------------------------- compilationUnit ::= [PACKAGE qualifiedIdentifier SEMI] {IMPORT qualifiedIdentifierStar SEMI} {typeDeclaration} EOF qualifiedIdentifier ::= IDENTIFIER {DOT IDENTIFIER} qualifiedIdentifierStar ::= IDENTIFIER {DOT IDENTIFIER} [DOT STAR] typeDeclaration ::= typeDeclarationModifiers (classDeclaration | interfaceDeclaration) | SEMI typeDeclarationModifiers ::= { PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL | STRICTFP } classDeclaration ::= CLASS IDENTIFIER [EXTENDS qualifiedIdentifier] [IMPLEMENTS qualifiedIdentifier {COMMA qualifiedIdentifier}] classBody interfaceDeclaration ::= INTERFACE IDENTIFIER // can't be final [EXTENDS qualifiedIdentifier {COMMA qualifiedIdentifier}] interfaceBody classBody ::= LCURLY { SEMI | STATIC block | block | modifiers memberDecl } RCURLY interfaceBody ::= LCURLY { SEMI | modifiers interfaceMemberDecl } RCURLY memberDecl ::= classDeclaration // inner class | interfaceDeclaration // inner interface | IDENTIFIER // constructor formalParameters [THROWS qualifiedIdentifier {COMMA qualifiedIdentifier}] block | (VOID | type) IDENTIFIER // method formalParameters {LBRACK RBRACK} [THROWS qualifiedIdentifier {COMMA qualifiedIdentifier}] (block | SEMI) | type variableDeclarators SEMI interfaceMemberDecl ::= classDeclaration // inner class | interfaceDeclaration // inner interface | (VOID | type) IDENTIFIER // method formalParameters {LBRACK RBRACK} [THROWS qualifiedIdentifier {COMMA qualifiedIdentifier}] SEMI | type variableDeclarators SEMI // must have inits block ::= LCURLY {blockStatement} RCURLY blockStatement ::= localVariableDeclarationStatement | typeDeclarationModifiers classDeclaration | statement statement ::= block | ASSERT expression [COLON expression] SEMI | IF parExpression statement [ELSE statement] | FOR LPAREN ( [forInit] SEMI [expression] SEMI [forUpdate] | type IDENTIFIER : expression ) RPAREN statement | WHILE parExpression statement | DO statement WHILE parExpression SEMI | TRY block {CATCH LPAREN formalParameter RPAREN block} [FINALLY block] // must be present if no catches | SWITCH parExpression LCURLY {switchBlockStatementGroup} RCURLY | SYNCHRONIZED parExpression block | RETURN [expression] SEMI | THROW expression SEMI | BREAK [IDENTIFIER] SEMI | CONTINUE [IDENTIFIER] SEMI | SEMI | IDENTIFIER COLON statement | statementExpression SEMI formalParameters ::= LPAREN [formalParameter {COMMA formalParameter}] RPAREN formalParameter ::= [FINAL] type IDENTIFIER {LBRACK RBRACK} parExpression ::= LPAREN expression RPAREN forInit ::= statementExpression {COMMA statementExpression} | [FINAL] type variableDeclarators forUpdate ::= statementExpression {COMMA statementExpression} switchBlockStatementGroup ::= switchLabel {switchLabel} {blockStatement} switchLabel ::= CASE expression COLON // must be constant | DEFAULT COLON localVariableDeclarationStatement ::= [FINAL] type variableDeclarators SEMI variableDeclarators ::= variableDeclarator {COMMA variableDeclarator} variableDeclarator ::= IDENTIFIER {LBRACK RBRACK} [ASSIGN variableInitializer] variableInitializer ::= arrayInitializer | expression arrayInitializer ::= LCURLY [variableInitializer {COMMA variableInitializer} [COMMA]] RCURLY arguments ::= LPAREN [expression {COMMA expression}] RPAREN modifiers ::= { PUBLIC | PROTECTED | PRIVATE | STATIC | FINAL | ABSTRACT | STRICTP | SYNCHRONIZED | TRANSIENT | THREADSAFE | VOLATILE } type ::= referenceType | basicType basicType ::= BOOLEAN | BYTE | CHAR | SHORT | INT | FLOAT | LONG | DOUBLE referenceType ::= basicType LBRACK RBRACK {LBRACK RBRACK} | qualifiedIdentifier {LBRACK RBRACK} statementExpression ::= expression // but see Java Spec. expression ::= assignmentExpression assignmentExpression ::= conditionalExpression // level 13 [( ASSIGN // conditionalExpression | PLUS_ASSIGN // must be valid lhs | MINUS_ASSIGN | STAR_ASSIGN | DIV_ASSIGN | MOD_ASSIGN | SR_ASSIGN | BSR_ASSIGN | SL_ASSIGN | BAND_ASSIGN | BXOR_ASSIGN | BOR_ASSIGN ) assignmentExpression] conditionalExpression ::= conditionalOrExpression // level 12 [QUESTION assignmentExpression COLON conditionalExpression] conditionalOrExpression ::= conditionalAndExpression // level 11 {LOR conditionalAndExpression} conditionalAndExpression ::= inclusiveOrExpression // level 10 {LAND inclusiveOrExpression} inclusiveOrExpression ::= exclusiveOrExpression // level 9 {BOR exclusiveOrExpression} exclusiveOrExpression ::= andExpression // level 8 {BXOR andExpression} andExpression ::= equalityExpression // level 7 {BAND equalityExpression} equalityExpression ::= relationalExpression // level 6 {(EQUAL | NOT_EQUAL) relationalExpression} relationalExpression ::= shiftExpression // level 5 [(LT | GT | LE | GE) shiftExpression | INSTANCEOF referenceType] shiftExpression ::= additiveExpression // level 4 {(SL | SR | BSR) additiveExpression} additiveExpression ::= multiplicativeExpression // level 3 {(PLUS | MINUS) multiplicativeExpression} multiplicativeExpression ::= unaryExpression // level 2 {(STAR | DIV | MOD) unaryExpression} unaryExpression ::= INC unaryExpression // level 1 | DEC unaryExpression | MINUS unaryExpression | PLUS unaryExpression | simpleUnaryExpression simpleUnaryExpression ::= BNOT unaryExpression | LNOT unaryExpression | LPAREN basicType RPAREN unaryExpression | LPAREN referenceType RPAREN simpleUnaryExpression | postfixExpression postfixExpression ::= primary {selector} {INC | DEC} selector ::= DOT ( IDENTIFIER [arguments] | THIS | SUPER superSuffix | NEW innerCreator ) | LBRACK expression RBRACK superSuffix ::= arguments | DOT IDENTIFIER [arguments] primary ::= LPAREN assignmentExpression RPAREN | THIS [arguments] | SUPER superSuffix | literal | NEW creator | IDENTIFIER {DOT IDENTIFIER} [identifierSuffix] | basicType {LBRACK RBRACK} DOT CLASS | VOID DOT CLASS identifierSuffix ::= LBRACK RBRACK {LBRACK RBRACK} DOT CLASS | LBRACK expression RBRACK | DOT ( CLASS | THIS | SUPER arguments | NEW innerCreator ) | arguments creator ::= type ( arguments [classBody] | newArrayDeclarator [arrayInitializer] ) newArrayDeclarator ::= LBRACK [expression] RBRACK {LBRACK [expression] RBRACK} innerCreator ::= IDENTIFIER arguments [classBody] literal ::= NUM_INT | NUM_FLOAT | CHAR_LITERAL | STRING_LITERAL | TRUE | FALSE | NULL Copyright 2001 Bill Campbell and Swaminathan Iyer Based on the Java Language Specification (2nd Edition).