This page generated at Thu Apr 9 14:20:11 PDT 1998.
This page is a sample piece of documentation generated from java.syntax.mth .
For more information see the documentation on Dr. Botting's MATHS project.
Contents
Java Language Grammar
The Java Language Specification(1.0Alpha3)
[ HEADING75 in javaspec_11 ]
For more general information follow these pointers:
[ java.html ]
[ java.glossary.html ]
For information on semantics and pre-defined classes follow these pointers:
[ java.semantics.html ]
,
[ java.html ]
,
[ java.classes.html ]
Note
This is a simplified grammar for a Java compilation unit. A Java program consists of one or more compilation units.
Notation
This uses my XBNF Extended BNF Notation where "|" indicates "or", "(...)" indicates priority.
O(_) stands for 0 or 1 occurrences,
N(_) for 1 or more occurrence, L(_) for a comma separated list, and #(_)
for 0 or more occurrences. For more information see
[ intro_ebnf.html ]
- O::=optional
- N::=one or more of
- L::=list of
Java imports C!
Much of the Java Syntax is based on the syntax of C and/or C++.
- raw_C::= See http://www.csci.csusb.edu/dick/samples/c.syntax.html.
The following formalizes the use of terms and definitions
from the syntax of C here. It also changes the names in the
C syntax to those used in the Java syntax.
- C::=raw_C(expression=>Expression, statement=>Statement, ...).
Lexemes
Quoted text signifies literal terminals.
The following are defined as in C
[ Lexemes in c.syntax ]
- Identifier::=C.identifier.
- Number::=C.integer_constant | C.float_constant. --??
- String::=C.string_constant.
- Character::=C.character_constant.
Comments in Java
- // text
All characters from // to the end of the line are ignored.
- /* text */
All characters from /* to */ are ignored.
- /** text */
These comments are treated specially when they occur
immediately before any declaration. They should not be used any
other place in the code. These comments indicate that the
enclosed text should be included in automatically generated
documentation as a description of the declared item.
- comment::= C.Comment | C++.Comment | Doc_Comment.
- Doc_Comment::=documentation comment::= "/**" documentation "*/".
Compilation Units
A Java program consists of one or more compilation units.
- Java_Program::=N(Compilation_Unit).
A compilation unit can identify its package, import any number of other
packages, classes, or interfaces,
and can declare any number of classes and interfaces:
- Compilation_Unit::= O(Package_Statement) #(Import_Statement) #(Type_Declaration).
- Package_Statement::= "package" Package_Name ";".
- Import_Statement::= "import" (Package_Name "." "*" | Class_Name | Interface_Name ) ";".
(package): Note that a file can only place items into at most one package. Packages are
a collection of comparatively unrelated classes and interfaces that are stored
in a single directory named after the package,
and imported by using the '.' notation:
import mystuff.Fun;
would import the content of a class in file Fun.class in directory mystuff.
The code for Fun must be in a file which states with
package mystuff
Declarations
- Type_Declaration::= Class_Declaration | Interface_Declaration | ";".
- Class_Declaration::= O(Doc_Comment) Possible_Modifiers "class" Identifier O(Class_Extension) O( Implements) Set_Of_Field_declarations.
- Class_Extension::="extends" Class_Name.
-- the default is to extend the class called Object.
- extension::gloss=The addition and replacement of fields and methods in an existing class or interface.
- implement::gloss=To provide detailed code that satisfies a particular interface.
- interface::gloss=A description of how to use a set of classes, without definition of how they work.
- Implements::="implements" L(Interface_Name).
-- A class implements an interface if it defines the things that
are merely described or specified in that interface.
- Set_Of_Field_declarations::="{" #(Field_Declaration) "}".
- Interface_Declaration::= O(Doc_Comment) Possible_Modifiers "interface" Identifier O( Interface_extension) Set_Of_Field_declarations.
- Interface_extension::= "extends" L(Interface_Name) .
- Field_Declaration::= O(Doc_Comment) ( Method_Declaration | Constructor_Declaration | Variable_Declaration ) | Static_Initializer | ";".
- Method_Declaration::= Possible_Modifiers Returned_Type_Description Identifier "(" O(Parameter_List) ")" Possible_Array_Indicators ( Block | ";" ).
- Returned_Type_Description::== void | Type.
- void::lexeme="void", indicating that nothing is returned and that the method
may not be invoked as part of an expression.
- Constructor_Declaration::= Possible_Modifiers Class_Identifier "(" O(Parameter_List) ")" Block.
- Class_Identifier::=Identifer & the name of the class of object being constructed.
- Variable_Declaration::= Possible_Modifiers Type L(Variable_Declarator) ";".
- Variable_Declarator::= Identifier Possible_Array_Indicators O("=" Variable_Initializer).
- Variable_Initializer ::= Expression | "{" O( L(Variable_Initializer) O(",") ) "}".
- Static_Initializer::= "static" Block
- Parameter_List::= L(Parameter).
- Parameter::= Type_Specifier Identifier Possible_Array_Indicators.
Statements
Java Statements follow rules very like those of C
[ Statements in c.syntax ]
- Statement::=C.statement(statement=>Statement, expression=>Expression) | Non_C_Statement.
- (above)|-C.Statement = Variable_Declaration | Expression ";" | Block | "if" "(" Expression ")" Statement O("else" Statement) | "while" "(" Expression ")" Statement | "do" Statement "while" "(" Expression ")" ";" | "switch" "(" Expression ")" Block
| "return" O(Expression) ";" | "case" Expression ":" | "default" ":" | Identifier ":" Statement | "break" O(Identifier) ";" | "continue" O(Identifier) ";" | ";".
- Non_C_Statement::="try" Block #("catch" "(" Parameter ")" Block) O("finally" Block) | "synchronized" "(" Expression ")" Block | "throw" Expression ";" .
- Old_Non_C_Statement::="try" Statement #("catch" "(" Parameter ")" Statement) O("finally" Statement) | "synchronized" "(" Expression ")" Statement | "throw" Expression ";" .
- Block::="{" Statements "}".
- Statements::=N(Statement). -- one or more statements.
Expressions
Expressions follow rules very like those of C
[ Expression in c.syntax ]
- Expression::= C.expression(expression=>Expression) | Non_C_Expression.
Here is a quick description of the abstract syntax of
a Java Expression ignoring the priorities of operators:
- Abstract_syntax::= Net{
- E::=C.Expression.
- (c.syntax) |-E==>Literal | E Infix E | Prefix E | E Postfix | Conditional_E | Other_E.
- Infix::= "+" | "-" | "*" | "/" | "%" | "^" | "&" | "|" | "&&" | "||" | "<<" | ">>" | ">>>" | "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" | "&=" | "|=" | "<<=" | ">>=" | ">>>=" | "<" | ">" | "<=" | ">=" | "==" | "!=" | "." | "," | "->".
- Prefix::= "++" | "--" | "-" | "~" | "!" | "*".
- Postfix::= "++" | "--".
- Conditional_E::=E "?" E ":" E.
- Other_E::= E "[" E "]" | "(" E ")" | "(" Type ")" E | E "(" O(Arg_List) ")".
- Non_C_Expression::=run_time_type_test_expression | object_creation_expression,
- run_time_type_test_expression::=E "instanceof" ( Class_Name | Interface_Name ),
- object_creation_expression::="new" Class_Name "(" O(Arg_List)")" | "new" Type_Specifier N( "[" E "]" ) Possible_Array_Indicators | "new" "(" E ")".
- Literal::= Boolean | Object | Identifier | Number | String | Character.
- Boolean::="true" | "false".
- Object::="null" | "super" | "this".
- Arg_List::= L(E).
}=::Abstract_syntax.
Types
- Type::= Type_Specifier Possible_Array_Indicators.
- Possible_Array_Indicators::=#("[" "]").
- Type_Specifier::= "boolean" | "byte" | "char" | "short" | "int" | "float" | "long" | "double" | Class_Name | Interface_Name.
- Possible_Modifiers::=#(Modifier).
- Modifier::= "public" | "private" | "protected" | "static" | "final" | "native" | "synchronized" | "abstract" | "threadsafe" | "transient".
[ Modifiers in java.glossary ]
Compound Names
- Package_Name::= Identifier | Package_Name "." Identifier.
- Class_Name::= Identifier | Package_Name "." Identifier.
- Interface_Name::= Identifier | Package_Name "." Identifier.
. . . . . . . . . To Contents, Index, or top of section Java Language Grammar.