Java (alpha 3): Grammar
Copied from Florida Institute of Technology
Author - Ryan Stansifer
The BNF grammar in this document was adapted from the
Java Specification (alpha3).
A couple of rules were reworked
to fit a slightly different form of extended BNF notation.
Also the BNF structure of the grammar was made more obvious
by formating the rules in HTML using, for example,
the "font" tag with the "color" attribute.
Metalanguage
The following conventions are used in the grammar.
NonTerminals
terminals
::= "is defined as"
| alternation
; end of rule
[ ] "optional"
{ } "zero or more occurrences"
{ }+ "one or more occurrences"
Reserved words
abstract else long short
extends static
boolean native super
break final new switch
byte finally null synchronized
byvalue float
for operator this
case future outer throw
catch thows
char generic package transient
class goto private try
const protected
continue if public var
implements void
import rest volatile
default inner return
do instanceof while
double int
interface
Compilation Units
CompilationUnit ::=
[ PackageStatement ] { ImportStatement } { TypeDeclaration }
;
PackageStatement ::=
package PackageName ;
;
ImportStatement ::=
import PackageName . * ;
| import ClassName ;
| import InterfaceName ;
;
Types and Names
Type ::=
TypeSpecifier { [ ] }
;
TypeSpecifier ::=
boolean
| byte
| char
| short
| int
| float
| long
| double
| ClassName
| InterfaceName
;
Modifier ::=
public
| private
| protected
| static
| final
| native
| synchronized
| abstract
| threadsafe
| transient
;
PackageName ::=
Identifier
| PackageName . Identifier
;
ClassName ::=
Identifier
| PackageName . Identifier
;
InterfaceName ::=
Identifier
| PackageName . Identifier
;
Declarations
TypeDeclaration ::=
ClassDeclaration
| InterfaceDeclaration
| ;
;
ClassDeclaration ::=
{ Modifier } class Identifier
[ extends ClassName ]
[implements InterfaceName { , InterfaceName } ]
{ { FieldDeclaration } }
;
InterfaceDeclaration ::=
{ Modifier } interface Identifier
[ extends InterfaceName { , InterfaceName } ]
{ { FieldDeclaration } }
;
FieldDeclaration ::=
[ DocComment ] MethodDeclaration
| [ DocComment ] ConstructorDeclaration
| [ DocComment ] VariableDeclaration
| StaticInitializer
| ;
;
MethodDeclaration ::=
{ Modifier } Type Identifier ( [ ParameterList ] ) { [ ] } ;
| { Modifier } Type Identifier ( [ ParameterList ] ) { [ ] } { { Statement } }
;
ConstructorDeclaration ::=
{ Modifier } Identifier ( [ ParameterList ] ) { { Statement } }
;
VariableDeclaration ::=
{ Modifier } Type VariableDeclarator { , VariableDeclarator } ;
;
VariableDeclarator ::=
Identifier { [ ] } [ = VariableInitializer ]
;
VariableInitializer ::=
Expression
| { [ VariableInitializer { , VariableInitializer } [ , ] ] }
;
StaticInitializer ::=
static { { Statement } }
;
ParameterList ::=
Parameter { , Parameter }
;
Parameter ::=
TypeSpecifier Identifier { [ ] }
;
Expressions
Expression ::=
Expression + Expression
| Expression - Expression
| Expression * Expression
| Expression / Expression
| Expression % Expression
| Expression ^ Expression
| Expression & Expression
| Expression | Expression
| Expression && Expression
| Expression || Expression
| Expression << Expression
| Expression >> Expression
| Expression >>> Expression
| Expression = Expression
| Expression += Expression
| Expression -= Expression
| Expression *= Expression
| Expression /= Expression
| Expression %= Expression
| Expression ^= Expression
| Expression &= Expression
| Expression |= Expression
| Expression <<= Expression
| Expression >>= Expression
| Expression >>>= Expression
| Expression < Expression
| Expression > Expression
| Expression <= Expression
| Expression >= Expression
| Expression == Expression
| Expression != Expression
| Expression . Expression
| Expression , Expression
| Expression instanceof ClassName
| Expression instanceof InterfaceName
| Expression ? Expression : Expression
| Expression [ Expression ]
| ++ Expression
| -- Expression
| Expression ++
| Expression --
| - Expression
| ! Expression
| ~ Expression
| ( Expression )
| ( Type ) Expression
| Expression ( [ ArgList ] )
| new ClassName ( [ ArgList ])
| new TypeSpecifier { [ Expression ] }+ { [ ] }
| new ( Expression )
| true
| false
| null
| super
| this
| Identifier
| Number
| String
| Character
;
ArgList ::=
Expression { , Expression }
;
Statements
Statement ::=
VariableDeclaration
| Expression ;
| { { Statement } }
| if ( Expression ) Statement [ else Statement ]
| while ( Expression ) Statement
| do Statement while ( Expression ) ;
| try Statement { catch ( Parameter ) Statement } [ finally Statement ]
| switch ( Expression ) { { Statement } }
| synchronized ( Expression ) Statement
| return [ Expression ] ;
| throw Expression ;
| case Expression :
| default :
| Identifier : Statement
| break [ Identifier ] ;
| continue [ Identifier ] ;
| ;
;