-
The vertical bar, |, means "or" (it separates
alternatives).
-
Square brackets, [...], indicate that what
they enclose is optional (may occur zero or one times).
-
Parenthesis, (...), are used to group syntactic
elements.
-
Curly braces, {...}, indicate that what they enclose
may occur any number of (zero or more) time. (This notation is
non-standard, while the two above are very commonly used in what is
called
regular expression syntax
.)
-
Bold indicates
literal text, which must appear exactly as indicated.
-
Italic text name
syntactic categories, which are the "parts of speech" of the
Java language: the abstract elements that make up its grammar. (Another
common notation is to put angle brackets around the names of a
syntactic category.)
-
=> is used in a grammar rule to separate the name of
a syntactic category from one of its possible forms (or multiple
possible forms, if | is used to separate the alternatives).
These rules that follow do not express whitespace (spaces,
newlines, tabs, and comments), which is all the same as far as the
language is concerned..
-
program =>
source_file {
source_file }
A
program consists of one or more source files.
(Officially, Java calls them "compilation units", but in practice they
are files.)
-
source_file =>
{
import_declaration }
class_or_interface_declaration {
class_or_interface_declaration }
A
source_file consists of zero or more
import declarations followed by one or more
class or interface declarations. If there are more than one in
a file, only the first may be public and if there are any compilation
errors with multiple classes file the messages can be very confusing.
-
import_declaration =>
import
package_name
. {
* |
class_
name |
interface_name }
An
import declarationconsists of the keyword
import followed by a package name (such as
objectdraw or
java.awt), and then a period followed by a either an
asterisk (star), a
class name, or an
interface name .
-
class_or_interface_declaration =>
class_declaration
For now we don't need to know about interface declarations.
Class
-
class_declaration => [
public ]
class
class_name [
extends FrameWindowController ]
class_body
For now we only need to extend FrameWindowController, and
then only for the main class of an objectdraw program.
-
class_body =>
{ {
class_member_declaration }
}
A
class body consists of a left brace, followed by zero or more
class member declarations, and finally a right brace.
-
class_member_declaration =>
method_declaration | constructor_declaration
|
field_declaration
A
class member declaration is either a
method declaration , a
constructor declaration, or a
field declaration.
-
field_declaration => {
modifier }
type variable_name [
initializer ]
;
-
initializer =>
=
expression
-
method_declaration => {
modifier }
return_type
method_name parameter_list method_body
-
modifier =>
public | private | static |
final
-
return_type
=>
void |
type
-
type =>
class_or_interface_name |
primitive_type
-
parameter_list =>
( [
parameter_declaration {
,
parameter_declaration } ]
)
A
parameter list consists of a left parenthesis, followed by
zero or more
parameter_declarations
, separated by commas, followed by a right
parenthesis.
-
parameter_declaration =>
type variable_name
-
method_body =>
{ {
statement }
}
-
constructor_declaration => [
public ]
class_name parameter_list constructor_body
-
constructor_body =>
method_body
Statement
-
statement =>
method_call
;
|
assignment
; | new_expression
; |
return [
expression ]
; |
if_statement
-
if_statement =>
if (
expression
)
statement [
else
statement ]
An else clause is associated with the nearest enclosing if
statement.
-
assignment =>
variable_name
=
expression
Expression
-
expression => literal |
variable_name | method_call |
binary_operation |
prefix_operation | class_variable_reference
|
this
-
method_call => [
expression
. |
class_name
. ]
method_name argument_list
-
argument_list =>
( [
expression {
,
expression } ]
)
An
argument list is a list of zero or more
expressions, separated by commas, and enclosed by parenthesis.
-
binary_operation =>
expression binary_operator expression
-
prefix_operation =>
prefix_operator expression
-
class_variable_reference =>
class_name
. variable_name
-
new_expression =>
new
class_name argument_list
Tokens
-
binary_operator =>
+ | - | * | / |
% | < | <= | >
| >= | == | !=
-
prefix_operator =>
+ | -
-
primitive_type =>
int | boolean | double |
char | byte | short | long
| float
-
literal =>
integer_literal |
string_literal
-
string_literal =>
" {
string_character | escape_sequence }
"
A
string literal is any sequence of zero or more string
characters or escape sequences (which will be introduced later),
surrounded by double-quote characters.
-
string_character => any ASCII character other
than a newline, backslash, or doublequote
-
integer_literal =>
digit {
digit }
-
-
name =>
name_letter | {
name_letter |
digit }
-
digit =>
0 | 1 | 2 | 3
| 4 | 5 | 6 | 7 |
8 | 9
-
name_letter =>
a | ... | z | A | ...
| Z | _ | $
A name letter is a lower or uppercase letter, or a
dollar sign or underscore. Dollar signs should only be used in
compiler-generated names.
-
class_name, interface_name, method_name, and
variable_name may be any
name that is not a
keyword
, but the standard Java style convensions should
be followed (such as starting class and interface names with upper-case
letters and method and variable names with lower-case letters)
-
keyword =>
abstract | default | if | private | this | boolean | do |
implements | protected | throw | break | double | import | public |
throws | byte | else | instanceof | return | transient | case | extends
| int | short | try | catch | final | interface | static | void | char
| finally | long | strictfp | volatile | class | float | native | super
| while | const | for | new | switch | continue | goto | package |
synchronized