[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci & Eng] / [R J Botting] / [Samples] / java.syntax
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Tue Jun 15 15:23:26 PDT 2010

Contents


    Java Language Grammar

      This syntax is based on the 1.0 and 1.1 specifications:

      Source: The Java Language Specification(1.0Alpha3) [ HEADING75 in javaspec_11 ] I'm extending it to include the small changes made in Java2.0.

      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 ]
    1. O::=optional
    2. N::=one or more of
    3. L::=list of

      Java imports C!

      Much of the Java Syntax is based on the syntax of C and/or C++.
    4. 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 some names in the C syntax to those used in the Java syntax.

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

      Lexemes

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

      Comments in Java


      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.

    10. comment::= C.comment | C++.comment | Doc_Comment.
    11. Doc_Comment::= "/**" documentation "*/", a documentation comment, a compiler with extract these to build HTML documentation of the program.

      Compilation Units

      A Java program consists of one or more compilation units.
    12. 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:

    13. Compilation_Unit::= O(Package_Statement) #(Import_Statement) #(Type_Declaration).
    14. Package_Statement::= "package" Package_Name ";".
    15. 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

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

    17. Class_Declaration::= O(Doc_Comment) Possible_Modifiers "class" Identifier O(Class_Extension) O( Implements) Set_Of_Inner_declarations.

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

    19. extension::gloss=The addition and replacement of fields and the redefinition, addition, and implementation of methods in an existing class or interface.
    20. implement::gloss=To provide detailed code that satisfies a particular interface.

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

    22. Set_Of_Inner_declarations::="{" #(Inner_Declaration) "}".

    23. interface::gloss=A description of how to use a set of classes, without definition of how they work.
    24. Interface_Declaration::= O(Doc_Comment) Possible_Modifiers "interface" Identifier O( Interface_extension) Set_Of_Abstract_Method_declarations, -- an interface never implements anything but can extend other interfaces.
    25. Interface_extension::= "extends" L(Interface_Name), -- interfaces can only extend interfaces.

    26. Inner_Declaration::= O(Doc_Comment) ( Method_Declaration | Constructor_Declaration | Field_Declaration | Nested_Class_Declaration ) | Static_Initializer | ";".

    27. Method_Declaration::= Possible_Modifiers Returned_Type_Description Identifier "(" O(Parameter_List) ")" Possible_Array_Indicators ( Block | ";" ).

    28. Set_Of_Abstract_Method_declarations::="{" #Abstract_Method_declaration "}".
    29. Abstract_Method_declaration::= Possible_Modifiers Returned_Type_Description Identifier "(" O(Parameter_List) ")" Possible_Array_Indicators ";".

    30. Returned_Type_Description::== void | Type.
    31. void::lexeme= "void", indicating that nothing is returned and that the method may not be invoked as part of an expression.

    32. Constructor_Declaration::= Possible_Modifiers Class_Identifier "(" O(Parameter_List) ")" Constructor_Body.
    33. Constructor_Body::="{" O( "super" "(" L(Expression) ")" ";" ) Statements "}". A Constructor can call the constructor of the parent class before starting to construct this object.

    34. Class_Identifier::=Identifer & the name of the class of object being constructed.

    35. Field_Declaration::= Possible_Modifiers Type L(Field_Declarator) ";". A field can be either variable or constant. A constant field has "final" as a modifier. A variable field does not.

    36. Field_Declarator::= Identifier Possible_Array_Indicators O("=" Field_Initializer).

    37. Field_Initializer::= Expression | "{" O( L(Field_Initializer) O(",") ) "}".

    38. Static_Initializer::= "static" Block

    39. Parameter_List::= L(Parameter).

    40. Parameter::= O("final") Type_Specifier Identifier Possible_Array_Indicators. In Java 1.1 and upwards parameters can be constants and described as final, but in Java 1.0 they were always variable.

    41. Nested_Class_Declaration::= Static_Nested_Class | Member_Nested_Class. -- In Jav 1.1 and later.

    42. Member_Nested_Class::=Class_Declaration. -- when inside another class and with no static modifier. -- Java 1.1 and above only.

    43. Static_Nested_Class::=Class_Declaration. -- when inside another class and with a static modifier. -- Java 1.1 and above only.

      Statements

      Java Statements follow rules very like those of C [ Statements in c.syntax ] For example
    44. if_statement::= See http://www.csci.csusb.edu/dick/samples/c.syntax.html#if_statement.
    45. for_statement::= See http://www.csci.csusb.edu/dick/samples/c++.syntax.html#if_statement.

    46. Statement::=C.statement(statement=>Statement, expression=>Expression) ~ C.goto_statement | Non_C_Statement. -- Java reserves goto as a word but has no goto statement.


    47. (above)|-Statement = Local_Field_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.

    48. Local_Field_Declaration::= Possible_Modifiers Type L(Field_Declarator) ";". Since Java 1.0 a local field can be either variable or constant. A constant field has "final" as a modifier. A variable does not. In Java 1.0 local fields were always variable and were called variables.

    49. Non_C_Statement::=Try_block | Synchronized_statement | Throw_statement | Local_Class_Declaration, -- Local Class Definitions were not permitted in Java 1.0 but are allowed in Java 1.1.

    50. Try_block::="try" Block #("catch" "(" Parameter ")" Block) O("finally" Block),

    51. Synchronized_statement::="synchronized" "(" Expression ")" Block. -- makes sure that only one thread at a time executes the block.

    52. Throw_statement::="throw" Expression ";" .

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

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

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

    56. Local_Class_Declaration::= Class_Declaration, -- a feature added in Java 1.1.

      Expressions

      Expressions follow rules very like those of C [ Expression in c.syntax ]
    57. Expression::= C.expression(expression=>Expression) | Non_C_Expression.

      Here is a quick description of a Java Expression E ignoring the precedence of operators:

    58. Abstract_syntax::=
      1. E::=C.expression. All Java expressions have one of the following forms:
      2. (C)|-E==>Field | Literal | E Infix E | Prefix E | E Postfix | Conditional_E | Other_E.

      3. Infix::= "+" | "-" | "*" | "/" | "%" | "^" | "&" | "|" | "&&" | "||" | "<<" | ">>" | ">>>" | "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" | "&=" | "|=" | "<<=" | ">>=" | ">>>=" | "<" | ">" | "<=" | ">=" | "==" | "!=" | "." | "," .
      4. (Infix, C)|-Infix = C.infix ~ "->" | "<<<" | "<<<=". Java has a left circular shift operator but no explicit dereferencing operator.

      5. Prefix::= "++" | "--" | "-" | "~" | "!" .
      6. (Prefix, C)|-Prefix = C.infix ~ "*". -- no explicit de-reference in Java.

      7. Postfix::= "++" | "--".

      8. Conditional_E::=E "?" E ":" E.

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

      10. Arg_List::= L(E).
      11. Literal::= Boolean | Object | Identifier | Number | String | Character.


    59. Non_C_Expression::=run_time_type_test_expression | object_creation_expression | anonymous_class_object,

    60. run_time_type_test_expression::=E "instanceof" ( Class_Name | Interface_Name ),
    61. object_creation_expression::="new" Class_Name "(" O(Arg_List)")" | "new" Type_Specifier N( "[" E "]" ) Possible_Array_Indicators | "new" "(" E ")".

    62. anonymous_class_object::= "new" ( Anonymous_Class | Anonymous_Interface), -- added in Java 1.1 these permit creation of a single object that is an instance of a class described in the same statement by extending an existing class or interface.

    63. Anonymous_Class::= Class_Name "(" Optional_List_of_Expressions ")" Set_of_Inner_Declarations. Note: the form is a constructor plus a set of extensions to the class.
       	ClockWatcher timer = new ClockWatcher( clock1 ){ int count; void tick(){count++;} };

      Anonymous_Interface= Interface_Name "()" Set_Of_Abstract_Method_declarations.

    64. Optional_List_of_Expressions::= O( L( E ) ).

    65. Boolean::="true" | "false".
    66. Object::="null" | "super" | "this".

      Types

    67. Type::= Type_Specifier Possible_Array_Indicators.
    68. Possible_Array_Indicators::=#("[" "]").

    69. Type_Specifier::= Primitive_data_type | Class_Name | Interface_Name.
    70. Primitive_data_type::="boolean" | "byte" | "char" | "short" | "int" | "float" | "long" | "double". Note: these all have defined lengths and default initial values. For example, a char is a 16-bit international UNICODE character and a byte is an 8-bit ASCII character.

    71. Possible_Modifiers::=#(Modifier).

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

      Compound Names

    73. Package_Name::= Identifier | Package_Name "." Identifier.

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

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

    76. Field::= Identifier | Package_Name "." Identifier | Class_Name "." Identifier | Field "." Identifier. A field can be either variable or constant. A constant field is declared with a "final" as a modifier. A variable is declared without a "final".
    77. Variable::=Field, a variable is a field that has no "final" in its declaration.
    78. Constant::=Field, a constant is a field that has a "final" modifier in its declaration.

      Glossary

    79. TBA::="To Be Announced".
    80. ASCII::= See http://cse.csusb.edu/dick/samples/comp.text.ASCII.html. -- "America Standard Code for Information Interchange".

    81. UNICODE::= See http://www.unicode.org.

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

End