.Open Java Language Grammar This syntax is based on the 1.0 and 1.1 specifications: .Source The Java Language Specification(1.0Alpha3) .See http://java.dnx.com/1.0alpha3/doc/javaspec/javaspec_11.html#HEADING75 I'm extending it to include the small changes made in Java2.0. For more general information follow these pointers: .See http://www/dick/samples/java.html .See http://www/dick/samples/java.glossary.html For information on semantics and pre-defined classes follow these pointers: .See http://www/dick/samples/java.semantics.html , .See http://www/dick/samples/java.html , .See http://www/dick/samples/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 .See http://www/dick/maths/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::=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. C::=$raw_C(expression=>$Expression, statement=>$Statement). . Lexemes Quoted text signifies literal terminals. The following are defined as in $C .See http://www.csci.csusb.edu/dick/samples/c.syntax.html#Lexemes Identifier::=$C.identifier. Number::=$C.integer_constant | $C.float_constant. --?? String::=$C.string_constant. Character::=$C.character_constant. . Comments in Java .Box // text .As_is All characters from // to the end of the line are ignored. /* text */ .As_is All characters from /* to */ are ignored. /** text */ .As_is These comments are treated specially when they occur .As_is immediately before any declaration. They should not be used any .As_is other place in the code. These comments indicate that the .As_is enclosed text should be included in automatically generated .As_is documentation as a description of the declared item. .Close.Box 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: .As_is 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 .As_is 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_Inner_declarations. Class_Extension::="extends" $Class_Name. -- the default is to extend the class called Object. extension::gloss=`The addition and replacement of fields and the redefinition, addition, and $implementation of methods in an existing class or interface`. implement::gloss=`To provide detailed code that satisfies a particular interface`. 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_Inner_declarations::="{" #($Inner_Declaration) "}". interface::gloss=`A description of how to use a set of classes, without definition of how they work`. 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. Interface_extension::= "extends" $L($Interface_Name), -- interfaces can only extend interfaces. Inner_Declaration::= $O($Doc_Comment) ( $Method_Declaration | $Constructor_Declaration | $Field_Declaration | $Nested_Class_Declaration ) | $Static_Initializer | ";". Method_Declaration::= $Possible_Modifiers $Returned_Type_Description $Identifier "(" $O($Parameter_List) ")" $Possible_Array_Indicators ( $Block | ";" ). Set_Of_Abstract_Method_declarations::="{" #$Abstract_Method_declaration "}". Abstract_Method_declaration::= $Possible_Modifiers $Returned_Type_Description $Identifier "(" $O($Parameter_List) ")" $Possible_Array_Indicators ";". 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) ")" $Constructor_Body. Constructor_Body::="{" O( "super" "(" L($Expression) ")" ";" ) $Statements "}". A Constructor can call the constructor of the parent class before starting to construct `this` object. Class_Identifier::=$Identifer & `the name of the class of object being constructed`. 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. Field_Declarator::= $Identifier $Possible_Array_Indicators $O("=" $Field_Initializer). Field_Initializer ::= $Expression | "{" $O( $L($Field_Initializer) $O(",") ) "}". Static_Initializer::= "static" $Block Parameter_List::= $L($Parameter). 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. Nested_Class_Declaration::= $Static_Nested_Class | $Member_Nested_Class. -- In Jav 1.1 and later. Member_Nested_Class::=Class_Declaration. -- when inside another class and with no static modifier. -- Java 1.1 and above only. 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 .See http://www/dick/samples/c.syntax.html#Statements For example if_statement::=http://www.csci.csusb.edu/dick/samples/c.syntax.html#if_statement. for_statement::=http://www.csci.csusb.edu/dick/samples/c++.syntax.html#if_statement. Statement::=$C.statement(statement=>$Statement, expression=>$Expression) ~ $C.goto_statement | $Non_C_Statement. -- Java reserves goto as a word but has no goto statement. ()|- $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. 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. 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. Try_block::="try" $Block #("catch" "(" $Parameter ")" $Block) $O("finally" $Block), Synchronized_statement::="synchronized" "(" $Expression ")" $Block. -- makes sure that only one thread at a time executes the block. Throw_statement::="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. Local_Class_Declaration::= $Class_Declaration, -- a feature added in Java 1.1. . Expressions Expressions follow rules very like those of $C .See http://www/dick/samples/c.syntax.html#Expression Expression::= $C.expression(expression=>$Expression) | $Non_C_Expression. Here is a quick description of a Java Expression E ignoring the precedence of operators: Abstract_syntax::= .Box E::=$C.expression. All Java expressions have one of the following forms: (C)|- E==>$Field | $Literal | E $Infix E | $Prefix E | E $Postfix | $Conditional_E | $Other_E. Infix::= "+" | "-" | "*" | "/" | "%" | "^" | "&" | "|" | "&&" | "||" | "<<" | ">>" | ">>>" | "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" | "&=" | "|=" | "<<=" | ">>=" | ">>>=" | "<" | ">" | "<=" | ">=" | "==" | "!=" | "." | "," . (Infix,C)|- Infix = $C.infix ~ "->" | "<<<" | "<<<=". Java has a left circular shift operator but no explicit dereferencing operator. Prefix::= "++" | "--" | "-" | "~" | "!" . (Prefix, C)|- Prefix = $C.infix ~ "*". -- no explicit de-reference in Java. Postfix::= "++" | "--". Conditional_E::=E "?" E ":" E. Other_E::= E "[" E "]" | "(" E ")" | "(" $Type ")" E | E "(" $O($Arg_List) ")". Arg_List::= $L(E). Literal::= $Boolean | $Object | $Identifier | $Number | $String | $Character. .Close.Box Non_C_Expression::=$run_time_type_test_expression | $object_creation_expression | $anonymous_class_object, 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 ")". 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. 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. .As_is ClockWatcher timer = new ClockWatcher( clock1 ){ int count; void tick(){count++;} }; Anonymous_Interface= Interface_Name "()" $Set_Of_Abstract_Method_declarations. Optional_List_of_Expressions ::= $O( $L( $E ) ). Boolean::="true" | "false". Object::="null" | "super" | "this". . Types Type::= $Type_Specifier $Possible_Array_Indicators. Possible_Array_Indicators::=#("[" "]"). Type_Specifier::= $Primitive_data_type | $Class_Name | $Interface_Name. 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. Possible_Modifiers::=#($Modifier). Modifier::= "public" | "private" | "protected" | "static" | "final" | "native" | "synchronized" | "abstract" | "threadsafe" | "transient". .See http://www/dick/samples/java.glossary.html#Modifiers . Compound Names Package_Name::= $Identifier | $Package_Name "." $Identifier. Class_Name::= $Identifier | $Package_Name "." $Identifier. Interface_Name::= $Identifier | $Package_Name "." $Identifier. 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". Variable::=$Field, a variable is a field that has no "final" in its declaration. Constant::=$Field, a constant is a field that has a "final" modifier in its declaration. . Glossary TBA::="To Be Announced". ASCII::=http://www/dick/samples/comp.text.ASCII.html. -- "America Standard Code for Information Interchange". UNICODE::=http://www.unicode.org. .Close Java Language Grammar