Select this to skip to main content [CSUSB] >> [CNS] >> [Comp Sci Dept] >> [R J Botting] >> [CS320 Course Materials] >> [Java] >> java.syntax [Source]
[Index] [Schedule] [Syllabi] [Text] [Labs] [Projects] [Resources] [Search] [Grading]
Thu Jan 22 13:43:07 PST 2004

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 ]

      Java imports C!

      Much of the Java Syntax is based on the syntax of C and/or C++.
    1. raw_C::documentation= [ 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.

    2. C::=raw_C(expression=>Expression, statement=>Statement, ...).

      Lexemes

      Quoted text signifies literal terminals. The following are defined as in C [ Lexemes in c.syntax ]
    3. Identifier::=C.identifier.
    4. Number::=C.integer_constant | C.float_constant. --??
    5. String::=C.string_constant.
    6. Character::=C.character_constant.

      Comments in Java

    7. comment::= C.Comment | C++.Comment | Doc_Comment.
    8. Doc_Comment::=documentation comment::= "/**" documentation "*/".
      1. // text
                  All characters from // to the end of the line are ignored.
      2. /* text */
                  All characters from /* to */ are ignored.
      3. /** 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.

      Compilation Units

      A Java program consists of one or more compilation units.
    9. Java_Program::=N(Compilation_Unit).

      A compilation unit can identify a package, import any number of other packages, classes or interfaces, and can declare any number of classes and interfaces:

    10. Compilation_Unit::= O(Package_Statement) #(Import_Statement) #(Type_Declaration).
    11. Package_Statement::= "package" Package_Name ";".
    12. Import_Statement::= "import" (Package_Name "." "*" | Class_Name | Interface_Name ) ";".

      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 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

      Help! ?? Can several files place classes into the same package or not?

      Declarations

    13. Type_Declaration::= Class_Declaration | Interface_Declaration | ";".

    14. Class_Declaration::= O(Doc_Comment) Possible_Modifiers "class" Identifier O(Class_Extension) O( Implements) Set_Of_Field_declarations.

    15. Class_Extension::="extends" Class_Name. -- the default is to extend the class called Object.

    16. extension::gloss=The addition and replacement of fields and methods in an existing class or interface.
    17. implement::gloss=To provide detailed code that satisfies a particular interface.
    18. interface::gloss=A description of how to use a set of classes, without definition of how they work.

    19. Implements::="implements" L(Interface_Name). -- A class implements an interface if it defines the things that are merely described or specified in that interface.

    20. Set_Of_Field_declarations::="{" #(Field_Declaration) "}".

    21. Interface_Declaration::= O(Doc_Comment) Possible_Modifiers "interface" Identifier O( Interface_extension) Set_Of_Field_declarations.
    22. Interface_extension::= "extends" L(Interface_Name) .

    23. Field_Declaration::= O(Doc_Comment) ( Method_Declaration | Constructor_Declaration | Variable_Declaration ) | Static_Initializer | ";".

    24. Method_Declaration::= Possible_Modifiers Type Identifier "(" O(Parameter_List) ")" Possible_Array_Indicators ( Block | ";" ).

    25. Constructor_Declaration::= Possible_Modifiers Class_Identifier "(" O(Parameter_List) ")" Block.
    26. Class_Identifier::=Identifer & the name of the class of object being constructed.
    27. Variable_Declaration::= Possible_Modifiers Type L(Variable_Declarator) ";".

    28. Variable_Declarator::= Identifier Possible_Array_Indicators O("=" Variable_Initializer).

    29. Variable_Initializer::= Expression | "{" O( L(Variable_Initializer) O(",") ) "}".

    30. Static_Initializer::= "static" Block

    31. Parameter_List::= L(Parameter).

    32. Parameter::= Type_Specifier Identifier Possible_Array_Indicators.

      Statements

      Java Statements follow rules very like those of C [ Statements in c.syntax ]

    33. Statement::=C.statement(statement=>Statement, expression=>Expression) | Non_C_Statement.


    34. |-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) ";" | ";".

    35. Non_C_Statement::="try" Block #("catch" "(" Parameter ")" Block) O("finally" Block) | "synchronized" "(" Expression ")" Block | "throw" Expression ";" .

    36. Old_Non_C_Statement::="try" Statement #("catch" "(" Parameter ")" Statement) O("finally" Statement) | "synchronized" "(" Expression ")" Statement | "throw" Expression ";" .

    37. Block::="{" Statements "}".

    38. Statements::=N(Statement). -- one or more statements.

      Expressions

      Expressions follow rules very like those of C [ Expression in c.syntax ]
    39. 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:

    40. Abstract_syntax::=
      Net{
      1. E::=C.Expression.
      2. (c.syntax) |- E==>Literal | E Infix E | Prefix E | E Postfix | Conditional_E | Other_E.

      3. Infix::= "+" | "-" | "*" | "/" | "%" | "^" | "&" | "|" | "&&" | "||" | "<<" | ">>" | ">>>" | "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" | "&=" | "|=" | "<<=" | ">>=" | ">>>=" | "<" | ">" | "<=" | ">=" | "==" | "!=" | "." | "," | "->".

      4. Prefix::= "++" | "--" | "-" | "~" | "!" | "*".
      5. Postfix::= "++" | "--".

      6. Conditional_E::=E "?" E ":" E.

      7. Other_E::= E "[" E "]" | "(" E ")" | "(" Type ")" E | E "(" O(Arg_List) ")".

      8. Non_C_Expression::=run_time_type_test_expression | object_creation_expression,
      9. run_time_type_test_expression::=E "instanceof" ( Class_Name | Interface_Name ),
      10. object_creation_expression::="new" Class_Name "(" O(Arg_List)")" | "new" Type_Specifier N( "[" E "]" ) Possible_Array_Indicators | "new" "(" E ")".

      11. Literal::= Boolean | Object | Identifier | Number | String | Character.
      12. Boolean::="true" | "false".
      13. Object::="null" | "super" | "this".

      14. Arg_List::= L(E).


      }=::Abstract_syntax.

      Types

    41. Type::= Type_Specifier Possible_Array_Indicators.
    42. Possible_Array_Indicators::=#("[" "]").

    43. Type_Specifier::= "boolean" | "byte" | "char" | "short" | "int" | "float" | "long" | "double" | Class_Name | Interface_Name.

    44. Possible_Modifiers::=#(Modifier).

    45. Modifier::= "public" | "private" | "protected" | "static" | "final" | "native" | "synchronized" | "abstract" | "threadsafe" | "transient". [ Modifiers in java.glossary ]

      Compound Names

    46. Package_Name::= Identifier | Package_Name "." Identifier.

    47. Class_Name::= Identifier | Package_Name "." Identifier.

    48. Interface_Name::= Identifier | Package_Name "." Identifier.

    . . . . . . . . . ( end of section Java Language Grammar) <<Contents | Index>>


Formulae and Definitions in Alphabetical Order