The syntax of Java in Backus-Naur form

Course links

<compilation unit> ::= {{<annotations>}? "package" <qualified identifier> ";"}? {<import declaration>}* {<type declaration>}*

<annotations> ::= {<annotation>}+

<annotation> ::= "@" <type name> {"(" {<identifier> "="}? <element value> ")"}?

<type name> ::= <identifier>

<element value> ::= <conditional expression>
                  | <annotation>
                  | <element value array initializer>

<conditional expression> ::= <expression 2> <expression 1 rest>

<expression 2> ::= <expression 3> {<expression 2 rest>}?

<expression 3> ::= <prefix op> <expression 3>
                 | "(" <expression> ")" <expression 3>
                 | "(" <type> ")" <expression 3>
                 | <primary> {<selector>}* {<postfix op>}*

<prefix op> ::= "++"
               | "--"
               | "!"
               | "~"
               | "+"
               | "-"

<expression> ::= <expression 1> {<assignment operator> <expression 1>}?

<expression 1> ::= <expression 2> {<expression 1 rest>}?

<expression 1 rest> ::= "?" <expression> ":" <expression 1>

<assignment operator> ::= "="
                        | "+="
                        | "-="
                        | "*="
                        | "/="
                        | "&="
                        | "|="
                        | "^="
                        | "%="
                        | "<<="
                        | ">>="
                        | ">>>="

<type> ::= <identifier> {<type arguments>}? {"." <identifier> {<type arguments>}?}* {"[" "]"}*
         | <basic type>

<type arguments> ::= "<" <type-argument> {"," <type argument>}* ">"

<type argument> ::= <type>
                  | "?" {"extends" <type>}?
                  | "?" {"super" <type>}?

<basic type> ::= "byte"
               | "short"
               | "char"
               | "int"
               | "long"
               | "float"
               | "double"
               | "boolean"

<primary> ::= <par expression>
            | <non-wildcard type arguments> <explicit generic invocation suffix>
            | <non-wildcard type arguments> "this" <arguments>
            | "this" {<arguments>}?
            | "super" <super suffix>
            | <literal>
            | "new" <creator>
            | <identifier> {"." <identifier>}* {<identifier suffix>}?
            | <basic type> {"[" "]"}* "." "class"
            | "void" "." "class"

<par expression> ::= "(" <expression> ")"

<non-wildcard type arguments> ::= "<" <type list> ">"

<type list> ::= <type> {"," <type>}*

<explicit generic invocation suffix> ::= "super" <super suffix>
                                       | <identifier> <arguments>

<super suffix> ::= <arguments>
                 | "." <identifier> {<arguments>}?

<arguments> ::= "(" {<expression> {"," <expression>}*}? ")"

<literal> ::= <integer literal>
            | <floating-point literal>
            | <character literal>
            | <string literal>
            | <boolean literal>
            | <null literal>

<creator> ::= {<non-wildcard type arguments}? <created name> <array creator rest>
            | {<non-wildcard type arguments}? <created name> <class creator rest>

<created name> ::= <identifier> {<non-wildcard type arguments}? {"." <identifier> {<non-wildcard type arguments>}?}*

<array creator rest> ::= "[" "]" {"[" "]"}* <array initializer>
                       | "[" <expression> "]" {"[" <expression> "]"}* {"[" "]"}*

<array initializer> ::= "{" {<variable initializer> {"," <variable initializer>}* {","}?}? "}"

<variable initializer> ::= <array initializer>
                         | <expression>

<class creator rest> ::= <arguments> {<class body>}?

<class body> ::= "{" {<class body declaration}* "}"

<class body declaration> ::= ";"
                           | {"static"}? <block>
                           | {<modifier>}* <member decl>

<block> ::= "{" <block statements> "}"

<block statements> ::= {<block statement}*

<block statement> ::= <local variable declaration statement>
                    | <class or interface declaration>
                    | {<identifier> ":"}? <statement>

<local variable declaration statement> ::= {"final"}? <type> <variable declarators> ";"

<variable declarators> ::= <variable declarator> {"," <variable declarator>}*

<variable declarator> ::= <identifier> <variable declarator rest>

<variable declarator rest> ::= {"[" "]"}* {"=" <variable initializer>}?

<class or interface declaration> ::= {<modifier>}* <class declaration>
                                   | {<modifier>}* <interface declaration>

<modifier> ::= <annotation>
             | "public"
             | "protected"
             | "private"
             | "static"
             | "abstract"
             | "final"
             | "native"
             | "synchronized"
             | "transient"
             | "volatile"
             | "strictfp"

<class declaration> ::= <normal class declaration>
                      | <enum declaration>

<normal class declaration> ::= "class" <identifier> {<type parameters>}? {"extends" <type>}? {"implements" <type list>}? <class body>

<type parameters> ::= "<" <type parameter> {"," <type parameter>}* ">"

<type parameter> ::= <identifier> "extends" <bound>

<bound> ::= <type> {"&" <type>}*

<enum declaration> ::= "enum" <identifier> {"implements" <type list>}? <enum body>

<enum body> ::= {{<enum constants>}? {","}? {<enum body declarations>}?}*

<enum constants> ::= <enum constant>
                   | <enum constants> "," <enum constant>

<enum constant> ::= <annotations> <identifier> {<arguments>}? {<class body>}?

<enum body declarations> ::= ";" {<class body declaration>}*

<interface declaration> ::= <normal interface declaration>
                          | <annotation type declaration>

<normal interface declaration> ::= "interface" <identifier> {<type parameters>}? {"extends" <type list>}? <interface body>

<interface body> ::= "{" {<interface body declaration>}* "}"

<interface body declaration> ::= ";"
                               | {<modifier>}* <interface member decl>

<interface member decl> ::= <interface method or field decl>
                          | <interface generic method decl>
                          | "void" <identifier> <void interface method declarator rest>
                          | <interface declaration>
                          | <class declaration>

<interface method or field decl> ::= <type> <identifier> <interface method or field rest>

<interface method or field rest> ::= <constant declarators rest> ";"
                                   | <interface method declarator rest>

<constant declarators rest> ::= <constant declarator rest> {"," <constant declarator>}*

<constant declarator rest> ::= {"[" "]"}* "=" <variable initializer>

<constant declarator> ::= <identifier> <constant declarator rest>

<interface method declarator rest> ::= <formal parameters> {"[" "]"}* {"throws" <qualified identifier list>}? ";"

<formal parameters> ::= "(" {<formal parameter decls>}? ")"

<formal parameter decls> ::= {"final"}? {<annotations>}? <type> <formal parameter decls rest>

<formal parameter decls rest> ::= <variable declarator id> {"," <formal parameter decls>}?
                                | "..." <variable declarator id>

<variable declarator id> ::= <identifier> {"[" "]"}*

<qualified identifier list> ::= <qualified identifier> {"," <qualified identifier>}*

<qualified identifier> ::= <identifier> {"." <identifier>}*

<interface generic method decl> ::= <type parameters> <type> <identifier> <interface method declarator rest>
                                  | <type parameters> "void" <identifier> <interface method declarator rest>

<void interface method declarator rest> ::= <formal parameters> {"throws" <qualified identifier list>}? ";"

<annotation type declaration> ::= "@" "interface" <identifier> <annotation type body>

<annotation type body> ::= {<annotation type element declarations>}*

<annotation type element declarations> ::= {<annotation type element declaration>}+

<annotation type element declaration> ::= {<modifier>}* <annotation type element rest>

<annotation type element rest> ::= <type> <identifier> <annotation method or constant rest>
                                 | <class declaration>
                                 | <interface declaration>
                                 | <enum declaration>
                                 | <annotation type declaration>

<annotation method or constant rest> ::= <annotation method rest>
                                       | <annotation constant rest>

<annotation method rest> ::= "(" ")"  {<default value>}?

<default value> ::= "default" <element value>

<annotation constant rest> ::= <variable declarators>

<statement> ::= <block>
              | "assert" <expression> {":" <expression>}? ";"
              | "if" <par expression> <statement> {"else" <statement>}?
              | "for" "(" <for control> ")" <statement>
              | "while" <par expression> <statement>
              | "do" <statement> "while" <par expression> ";"
              | "try" <block> <catches>
              | "try" <block> {<catches>}? "finally" <block>
              | "switch" <par expression> "{" <switch block statement groups> "}"
              | "synchronized" <par expression> <block>
              | "return" {<expression>}? ";"
              | "throw" <expression> ";"
              | "break" {<identifier>}? ";"
              | "continue" {<identifier>}? ";"
              | ";"
              | <statement expression> ";"
              | <identifier> ":" <statement>

<for control> ::= <for var control>
                | {<for init>}? ";" {<expression>}? ";" {<for update>}?

<for var control> ::=  {"final"}? {<annotations>}? <type> <identifier> <for var control rest>

<for var control rest> ::= <variable declarators rest> ";" {<expression>}? ";" {<for update>}?
                         | ":" <expression>

<variable declarators rest> ::= <variable declarator rest> {"," <variable declarator>}*

<for init> ::= <statement expression> <more statement expressions>

<more statement expressions> ::= {"," <statement expression>}*

<for update> ::= <statement expression> <more statement expressions>

<catches> ::= {<catch clause>}+

<catch clause> ::= "catch" "(" <formal parameter> ")" <block>

<formal parameter> ::= {"final"}? {<annotations>}? <type> <variable declarator id>

<switch block statement groups> ::= {<switch block statement group>}*

<switch block statement group> ::= <switch label> <block statements>

<switch label> ::= "case" <constant expression> ":"
                 | "case" <enum constant name> ":"
                 | "default" ":"

<member decl> ::= <generic method or constructor decl>
                | <method or field decl>
                | "void" <identifier> <void method declarator rest>
                | <identifier> <constructor declarator rest>
                | <interface declaration>
                | <class declaration>

<generic method or constructor decl> ::= <type parameters> <generic method or constructor rest>

<generic method or constructor rest> ::= <type> <identifier> <method declarator rest>
                                       | "void" <identifier> <method declarator rest>
                                       | <identifier> <constructor declarator rest>

<method declarator rest> ::= <formal parameters> {"[" "]"}* {"throws" <qualified identifier list>}? <method body>
                           | <formal parameters> {"[" "]"}* {"throws" <qualified identifier list>}? ";"

<method body> ::= <block>

<constructor declarator rest> ::= <formal parameters> {"throws" <qualified identifier list>}? <method body>

<method or field decl> ::= <type> <identifier> <method or field rest>

<method or field rest> ::= <variable declarator rest>
                         | <method declarator rest>

<void method declarator rest> ::= <formal parameters> {"throws" <qualified identifier list>}? <method body>
                                | <formal parameters> {"throws" <qualified identifier list>}? ";"

<identifier suffix> ::= {"[" "]"}+ "." "class"
                      | "[" <expression> "]"
                      | <arguments>
                      | "." "class"
                      | "." <explicit generic invocation>
                      | "." "this"
                      | "." "super" <arguments>
                      | "." "new" {<non-wildcard type arguments}? <inner creator>

<explicit generic invocation> ::= <non-wildcard type arguments> <explicit generic invocation suffix>

<inner creator> ::= <identifier> <class creator rest>

<selector> ::= "." <identifier> {<arguments>}?
             | "." <explicit generic invocation>
             | "." "this"
             | "." "super" <super suffix>
             | "." "new" {<non-wildcard type arguments}? <inner creator>
             | "[" <expression> "]"

<postfix op> ::= "++"
               | "--"

<expression 2 rest> ::= {<infix op> <expression 3>}*
                      | <expression 3> "instanceof" <type>

<infix op> ::= "||"
             | "&&"
             | "|"
             | "^"
             | "&"
             | "=="
             | "!="
             | "<"
             | ">"
             | "<="
             | ">="
             | "<<"
             | ">>"
             | ">>>"
             | "+"
             | "-"
             | "*"
             | "/"
             | "%"

<expression 1 rest> ::= "?" <expression> ":" <expression 1>

<element value array initializer> ::= "{" {<element values>}? {","}? "}"

<element values> ::= {<element value>}+

<qualified identifier> ::= <identifier> {"." <identifier>}*

<import declaration> ::= "import" {"static"}? <identifier> {"." <identifier>}* {"." "*"}?

<type declaration> ::= <class or interface declaration>
                     | ";"

This grammar was adapted from the one in chapter 18 of The Java language specification, third edition, by James Gosling, Bill Joy, Guy Steele, and Gilad Bracha (Upper Saddle River, New Jersey: Addison-Wesley, 2005; ISBN 0-321-24678-0), pages 585-596.