Prev Up Contents

## B Appendix: Java Language Grammar

This is a short grammar for a Java compilation unit. A Java program consists of one or more compilation units.

The grammar has undefined terminal symbols DocComment, Identifier, Number, String, and Character. Quoted text signifies literal terminals.

Each rule is of the form nonterminal = meta-expression ; Other meta-notation is: | for alternation, ( ... ) for grouping, postfix ? for 0 or 1 occurrences, postfix + for 1 or more occurrence, and postfix * for 0 or more occurrences.

    CompilationUnit =
    	PackageStatement? ImportStatement* TypeDeclaration*
    ;
    PackageStatement =
    	`package' PackageName `;'
    ;
    ImportStatement =
    	`import' PackageName `.' `*' `;'
    |	`import' ( ClassName | InterfaceName )`;'
    ;
    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? `)' ( `[' `]' )*
    	( `{' 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 (`[' `]')*
    ;
    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? `;'
    |	`;'
    ;
    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 | 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 )*
    ;
    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
    ;

           Prev       Up       Contents
    The Java Language Specification
    Generated with CERN WebMaker